Vi Commands Cheat Sheet



These vi commands help with searching for, and replacing text inside your plain text files. /pattern?pattern (backward) Find next search result:%s/old/new/g Replace all old with new (with prompt):%s/old/new/gc Replace all old with new (no prompts) Multi-file Editing These commands help navigating multiple files in vi. VI “Cheat” Sheet ACNS Bulletin ED–03 February 1995 File management commands:w name Write edit buffer to file name:wq Write to file and quit:q! Quit without saving changes ZZ Same as:wq:sh Execute shell commands (d) Window motions d Scroll down (half a screen) u Scroll up (half a screen) f Page forward b Page backward.

The vi editor is a command-line, interactive editor that you can use to create and modify text files. The vi editor is also the only text editor that you can use to edit certain system files without changing the permissions of the files. The Vim editor is an enhanced version of the vi editor.

How to use vi

Accessing the vi Editor

To create, edit, and view files in the vi editor, use the vi command. The vi command includes the following three syntaxes:

If the system crashes while you are editing a file, you can use the -r option to recover the file.

The file opens so that you can edit it. You can then save the file and exit the vi editor, by using the following command:

The file opens in read-only mode to prevent accidental overwriting of the contents of the file.

The vi Editor Modes

Free print artist platinum. The vi editor provides three modes of operation:
1. Command mode – The command mode is the default mode for the vi editor. In this mode, you can run commands to delete, change, copy, and move text. You can also position the cursor, search for text strings, and exit the vi editor.

2. Input mode – You can insert text into a file in the input mode. The vi editor interprets everything you type in the input mode as text. To invoke input mode, press one of the following lowercase keys:

  • i – Inserts text before the cursor
  • o – Opens a new blank line below the cursor
  • a – Appends text after the cursor

You can also invoke the input mode to insert text into a file by pressing one of the following uppercase keys:

  • I – Inserts text at the beginning of the line
  • O – Opens a new blank line above the cursor
  • A – Appends text at the end of the line

3. Last line mode – You can use advanced editing commands in the last line mode. To access the last line mode, enter a colon (:) while in the command mode. Entering the colon (:) character places the cursor at the bottom line of the screen.

Note: The last line mode is actually the ex mode. The vi editor is essentially a visual extension to the ex editor, which in turn is an extended version of the ed editor.

Switching Between Modes

– The default mode for the vi editor is the command mode.
– To switch to the input mode, press i, o, or a.
– To return to the command mode, press the Escape key.
– In the command mode, you can save the file and quit the vi editor, and return to the shell prompt.

Viewing Files in the Read-Only Mode

The view command enables you to view files in the read- only mode.

Vi commands cheat sheet

The view command invokes the vi editor in the read-only option, which means you cannot save changes to the file. For example, to view the data.txt file in the read-only mode, enter the following command:

The data.txt file appears. Enter the :q command to exit the file, exit the vi editor, and return to the shell prompt.

Moving the Cursor Within the vi Editor

The table shows the key sequences that move the cursor.

Key SequenceCursor Movement
h, left arrow, or Backspace Left one character
j or down arrow Down one line
k or up arrow Up one line
l, right arrow, or space bar Right (forward) one character
wForward one word
bBack one word
eTo the end of the current word
$To the end of the line
0 (zero)To the beginning of the line
^To the first non-white space character on the line
Return Down to the beginning of the next line
GGoes to the last line of the file
1GGoes to the first line of the file
:nGoes to Line n
nGGoes to Line n
Control + F Pages forward one screen
Control + D Scrolls down one-half screen
Control + B Pages back one screen
Control + U Scrolls up one-half screen
Control + L Refreshes the screen
Control + G Displays current buffer information

Inserting and Appending Text

The table describes the commands to insert and append text to a new or existing file by using the vi editor.

CommandFunction
aAppends text after the cursor
AAppends text at the end of the line
iInserts text before the cursor
IInserts text at the beginning of the line
oOpens a new line below the cursor
OOpens a new line above the cursor
:r filenameInserts text from another file into the current file
Note: The vi editor is case-sensitive. Use the appropriate case for the input commands. Also, most of the input commands and cursor movements can be preceded by a number to repeat the command that many times.

Text-Deletion Commands

The table shows commands that delete text in the vi editor.

CommandFunction
ROverwrites or replaces characters on the line at and to the right of the cursor. To terminate this operation, press Escape.
CChanges or overwrites characters from the cursor to the end of the line
sSubstitutes a string for a character at the cursor
xDeletes a character at the cursor
dwDeletes a word or part of the word to the right of the cursor
ddDeletes the line containing the cursor
DDeletes the line from the cursor to the right end of the line
:n,ndDeletes lines n -n (For example, :5,10d deletes lines 5-10.)
Note: Output from the delete command writes to a buffer from which text can be retrieved.

You can use numerous commands to edit files by using the vi editor. The following sections describe basic operations for deleting, changing, replacing, copying, and pasting. Remember that the vi editor is case-sensitive.

Edit Commands

The table describes the commands to change text, undo a change, and repeat an edit function in the vi editor.

CommandFunction
cwChanges or overwrites characters at the cursor location to the end of that word
rReplaces the character at the cursor with one other character
JJoins the current line and the line below
xpTransposes the character at the cursor and the character to the right of the cursor
~Changes letter casing to uppercase or lowercase, at the cursor
uUndoes the previous command
UUndoes all changes to the current line
.Repeats the previous command
Note: Many of these commands change the vi editor into the input mode. To return to the command mode, press the Esc key.

Search and Replace Commands

The table shows the commands that search for and replace text in the vi editor.

CommandFunction
/stringSearches forward for the string
?stringSearches backward for the string
nSearches for the next occurrence of the string. Use this command after searching for a string.
NSearches for the previous occurrence of the string. Use this command after searching for a string.
:%s/old/new/g Searches for the old string and replaces it with the new string globally

Copy and Paste Commands

The table shows the commands that cut, copy, and paste text in the vi editor.

Sheet
CommandFunction
yyYanks a copy of a line
pPuts yanked or deleted text under the line containing the cursor
PPuts yanked or deleted text before the line containing the cursor
:n,n co n Copies lines n -n and puts them after line n (For example, :1,3 co 5 copies lines 1-3 and puts them after line 5.)
:n,n m n Moves lines n -n to line n. For example, :4,6 m 8 moves lines 4-6 to line 8, line 6 becomes line 8, line 5 becomes line 7, and line 4 becomes line 6.
Sheet

Save and Quit Commands

The table describes the commands that save the text file, quit the vi editor, and return to the shell prompt.

How to use vi
CommandFunction
:wSaves the file with changes by writing to the disk
:w new_filenameWrites the contents of the buffer to new_filename
:wqSaves the file with changes and quits the vi editor
:xSaves the file with changes and quits the vi editor
ZZ Saves the file with changes and quits the vi editor
:q!Quits without saving changes
ZQQuits without saving changes

Session Customization

You can customize a vi session by setting variables for the session. Sd gundam capsule fighter private server. When you set a variable, you enable a feature that is not activated by default. You can use the set command to enable and disable variables. The set command variables include displaying line numbers and invisible characters, such as the Tab and the end-of-line characters.

To create an automatic customization for all your vi sessions, perform the following steps:

  1. Create a file named .exrc in your home directory.
  2. Enter any of the set variables into the .exrc file.
  3. Enter each set variable without the preceding colon.
  4. Enter each command on one line.

The vi editor reads the .exrc file located in your home directory each time you open a vi session, regardless of your current working directory.

Vi Help Sheet

Note: The same steps apply for customizing a session in the Vim editor. Except that, instead of creating an .exrc file, you need to create a .vimrc file.

Session Customization Commands

The table describes some of the variables of the set command.

Vi Commands Cheat Sheet

CommandFunction
:set nuShows line numbers
:set nonu Hides line numbers
:set ic Instructs searches to ignore case
:set noic Instructs searches to be case-sensitive
:set noic Displays invisible characters, such as ^I for a Tab and $ for end-of-line characters
:set nolist Turns off the display of invisible characters
:set showmodeDisplays the current mode of operation
:set noshowmodeTurns off the mode of operation display
:set Displays all the vi variables that are set
:set all Displays all vi variables and their current values