VI commands

May 24, 2008

VI COMMANDS

I got this list somewhere on the net. I have not reviewed it completely, but at the first glance, this looks correct. Enjoy…

CURSOR & LINE POSITIONING
h  Move left one character.
i  Insert text move into insert mode before the current character.
j  Move down one line.
k  Move up one line.
l  Move right one character.
0  Move to beginning of line.
$  Move to end of line.
Backspace Move left one character.
Enter Move to beginning of next line.
(  Move backward one sentence.
)  Move forward one sentence.
+  Move down to beginning of next line.
-  Move upto the beginning of previous line.
G  Move to last line in file.
nG Move to line no. n in file.

WORD POSITIONING
B  Back up one space-delimited word.
b  Back up one word.
W  Move forward one space-delimited word.
w  Move forward one word.
e  Move to the end of the current word.

WINDOW POSITIONING
H  Move to first line on screen i.e. “Home”.
M  Move to middle line on screen.
L  Move to last line on screen.
^f Move forward one screen of text.
^b Back up one screen of text.
^d Move down half a page.
^u Move up half a page.

TEXT EDITING
a  Append text move into insert mode after the current character.
A  Append text at end of current line.
i  Inserts text at cursor.
I  Inserts text at beginning of current line.
Esc Leave insert mode, return to command mode.
R  Overwrite text from cursor position.
r  Replace the current character with the next pressed.
O  Open new line for insert above the current line.
o  Open new line for insert below the current line.
U  Undo replace current line if changed.
u  Undo the last change made to the file.
:e file   Edit a specified file without leaving vi.
C  Change text through the end of line.
c  Change text in the specified range cw changes the following word,
whereas c} changes the next paragraph.
{  Move backward one paragraph.
}  Move forward one paragraph.

DELETION
x  Delete a single character at current position.
X  Delete to left of cursor.
dw Delete a word from cursor to the next space/punctuation.
dd Delete a line.
nx,ndw,ndd   Delete n chars,n words,n lines.
d0  Delete line from cursor to beginning of line.
d$  Delete line from cursor to end of line.

MISCELLANEOUS
^g Show current line number and other information about the file.
^v Prevent vi from interpreting the next character.
^l Clears and redraws the current window.
:! Invoke specified UNIX command.
J  Joins the line below current line with current line.
~  Changes current char to Upper case from lower case and vice versa.
:sh Return to Shell. Type exit to get back to vi.
.  Repeats action performed by last command
!!  Replace current line with output of UNIX command.
!}  Replace current paragraph with the results of piping it through
the specified UNIX program or programs.

QUITTING
ZZ  Write buffer to file and quit.
:w  Write the edit buffer to the system.
:q  Quit vi if changes have been written.
:q! Quit vi throwing away any changes made.
:w file  Writes buffer to specified file.
: w! file  Overwrites specified file with contents of buffer.

SEARCHING
/pattern  Search forward for the next line using a specified pattern.
?pattern  Search backward for the next line using a specified pattern.
n  Repeats the last search command.
N  Repeats search command in opposite direction.

BLOCK COMMANDS
:r file  Read the contents of a specified file, including it in
the current edit buffer.
:m,n d  Deletes lines from m to n.
:m,n mo p Moves lines m to n after line p.
:m,n co p Copies lines m to n after line p.
:m,n w file  Writes lines m to n to specified file.
:m,n w>>file Appends lines m to n to specified file.
:r !command  Execute shell command and output of command is read
at current position.

FIND AND REPLACE
:s/str1/str2  Replaces first occurence of str1 with str2 in current line.
:s/str1/str2/g  Replaces all occurences of str1 with str2 in current line.
:m,n s/str1/str2/g range of command from m to n.
:1,$ s/str1/str2/g range of command from first to last line.
:.,$ s/str1/str2/g range of command from current line to last line.
:1,. s/str1/str2/g range of command from first line to last line.

DELETE AND PASTE
“nP  To Paste nth latest deletion after current line.n range is 1-9.
“xdd,xndd,xdw,xndw Deletes 1 or n lines, words and stores in buffer named x.
    (x can be any alphabet)
“xp  Pastes from buffer named x at current cursor position.

COPY(Yank) AND PASTE
yw  Yanks word from cursor position.
yy  Yanks line from cursor position.
y$  Yanks line from cursor position to end of line.
Y0  Yanks line from cursor position to beginning of line.
nP  Pastes nth latest yanked buffer. n range is 1-9.
“xyy,”xnyy  Yanks 1/n lines to buffer named x.
“xyw,”xnyw  Yanks 1/n words to buffer named x.
“xp  Pastes buffer x at current position.

ABBREVIATION
:abbr a bcd   Define abbreviation a for phrase bcd.
:abbr   Show current abbreviations, if any.
:una a Unabbreviates the abbreviation a.
:map a bcd    Map key a to the vi commands bcd.
:map   Show current key mappings, if any.

MULTIPLE FILES
vi fn1,fn2,fn3  Loads fn1,fn2,fn3 into vi for editing.
:n  Move to the next file in the file list.
:n! Move to next file in list without saving current file.
:rew  Move to first file in list.
:rew! Move to first file in list without saving current file.
:args Displays file list in buffer.
:f  Displays name of current file.

SET COMMANDS
:set nonu  Turn off line numbering.(Default)
:set nu    Turn on line numbering.
:set eb    Turn Speaker on.(Default)
:set noeb  Turn speaker Off.
:set ai    Turn auto indent on.
:set noai  Turn auto indent off.(Default)
:set ic    Ignore case in Pattern search.
:set noic  Do not Ignore case in Pattern search.(Default)
:set mesg  Permit reciept of message from other terminals.
:set nomesg  Do not recieve messages. (Default)
:set showmode  Display current working mode.
:set noshowmode  Do not display current working mode.(Default)
:set aw  Auto write buffer to disk before switching to next file.
:set noaw  Do not write buffer to disk before switching to next file.(Default)