Friday, June 1, 2012

VI Editor: Cheat Sheet

In this post I am giving a cheat sheet of VI editor. I found it very useful, hope everyone finds it useful too.


Quitting
:x Exit saving changes 
:q Exit as long as there have been no changes 
ZZ Exit and save changes if any have been made
:q! Exit and ignore any changes 

Inserting Text
i  Insert before cursor 
I  Insert before line 
a  Append after cursor 
A  Append after line 
o  Open a new line after current line 
O  Open a new line before current line 
r  Replace one character 
R  Replace many characters 

Motion
h  Move left 
j  Move down 
k  Move up 
l  Move right 
w  Move to next word 
W  Move to next blank delimited word 
b  Move to the beginning of the word 
B  Move to the beginning of blank delimted word 
e  Move to the end of the word 
E  Move to the end of Blank delimited word 
(  Move a sentence back 
)  Move a sentence forward 
{  Move a paragraph back 
}  Move a paragraph forward 
0  Move to the begining of the line 
$  Move to the end of the line 
1G  Move to the first line of the file 
G  Move to the last line of the file 
nG  Move to nth line of the file 
:n  Move to nth line of the file 
fc  Move forward to c 
Fc  Move back to c 
H  Move to top of screen 
M  Move to middle of screen 
L  Move to botton of screen 
%  Move to associated ( ),  { }, []

Deleting Text
Almost all deletion commands are performed by typing d followed by a motion. For example,  dw deletes a word. A few other deletes are:
x  Delete character to the right of cursor 
X  Delete character to the left of cursor 
D  Delete to the end of the line 
dd  Delete current line 
:d  Delete current line 


Yanking Text
Like deletion, almost all yank commands are performed by typing y followed by a motion. For example, y$ yanks to the end of the line. Two other yank commands are:
yy  Yank the current line 
:y  Yank the current line 


Putting text
p  Put after the position or after the line 
P  Put before the poition or before the line 

Search for strings
/string  Search forward for string 
?string  Search back for string 
n  Search for next instance of string 
N  Search for previous instance of string 

Replace
The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command (below).
:s/pattern/string/flags Replace pattern with string according to flags. 
g Flag Replace all occurences of pattern 
c Flag Confirm replaces
& Repeat last :s command 
:.=  Shows current line number 
:=  Shows number of lines in file 


VI Settings
Note: Options given are default. To change them, enter type :set option to turn them on or :set nooption to turn them off.To make them execute every time you open VI, create a file in your HOME directory called .exrc and type the options without the colon (:) preceding the option Set Default Description
:set ai noai  Turns on auto indentation 
:set all  Prints all options to the screen 
:set ap aw  Prints line after d c J m :s t u commands 
:set aw noaw  Automatic write on :n ! e# ^^ :rew ^} :tag 
:set bf nobf  Discards control characters from input 
:set dir=tmp dir = /tmp  Sets tmp to directory or buffer file 
:set eb noed  Precedes error messages with a bell 
:set ed noed  Precedes error messages with a bell 
:set ic noic  Ignores case when searching 
:set lisp nolisp  Modifies brackets for Lisp compatibility. 
:set list nolist  Shows tabs (^l) and end of line ($) 
:set magic magic  Allows pattern matching with special characters 
:set mesg  mesg  Allows others to send messages 
:set nooption   Turns off option 
:set nu nonu  Shows line numbers 
:set opt opt  Speeds output; eliminates automatic RETURN 
:set prompt prompt  Prompts for command input with : 
:set re nore  Simulates smart terminal on dumb terminal 
:set ro noro  Changes file type to "read only" 
:set scroll=n scroll = 11  set n lines for CTRL-d and z 
:set showmode nosm  Indicates input or replace mode at bottom 
:set sm nosm  Show matching { or ( as ) or } is typed