Professional Documents
Culture Documents
Commands Complete
Cursor movements
h or [back space] Move one character left from current cursor position
l or [spacebar] Move one character right from current cursor position
k or ^p Move to previous line
j or ^n Move to next line
+, - Move to first non-white space character of next, previous line
^ or _ Move to first non-white space character on current line
0 or |, $ Move to first, last position in a line
n[enter] Move to nth line in a file
n| Move to nth character from current position in a line
w W Move to next word, move to next word(ignore punctuation)
e E Move to end of the word, move to end of the word(ignore punctuation)
b B Move to previous word, move to previous word(ignore punctuation)
H M L Move to top, middle, bottom of screen
z[enter] z. z- Place the current line to top, middle, bottom Use: /<pattern>/z
:0, :$ or :% or G Move to 1st line, Move to last line in the file
'' `` Acts like toggle, used to move from current line <-> beginning of file
:n or nG :+n :-n Move to nth line, n lines from current line, n lines back from current
) ( line to next, previous sentence
Move
]] [[ Move to next, previous section
} { Move to next, previous paragraph
% Go to matching parenthesis () {} []
^f ^b Scroll forward, backward one full screen
^d ^u Scroll forward, backward half a screen
^e ^y Show one more line at bottom, top of the screen
Misc Examples
:s/str1/str2/g (alternate for :%s/str1/str2/gc) Search and replace first occurrence of str1 with str2 in current line
Press n to move the cursor for next occurrence of str1
& will replace the str1 with str2 in current line (or) repeat last
replace
:n& do replacement on nth line
:m,n& replacement on set of lines from m to n line
/str1 Search for string str1 and place the cursor over there
cwSTR2[ESC] Change the str1 with STR2 (Note: cw is command for change
word)
n Repeat search
. Repeat the step 2
This often turns out to be faster than using a global substitution with
confirmation.
Insertion we can enter into Insert mode by pressing any one of following characters a A i I o O c C s S r R
a A or $a or $s Append at cursor, append at beginning of line
i I Insert at cursor, insert at beginning of line
o O Open new line after current line, before current line
^i Insert tab in insert mode
^j Create new line in insert mode
^h Backspace in insert mode
^t Move to next shift width
^w Move back to beginning of previous word in insert mode
n>> n<< Format n lines with indention (i.e tab at beginning of each line)
>% <% Format set of line with indention Note: % match with corresponding '('
'[' '{'
Eg: >G shift all lines to right with one tab space at beginning
Change Text
cw ce cb Change word, change word by ignoring space, change till beginning of word
c0 Change from current position to beginning of line
c$ C Change from current position till end of line
r To replace single character
s cl Substitute one or more character for one character
cc S Replace whole line
R Replace till we press [Esc]
ctP Change till first occurrence of P
cnc[esc] Delete n lines from current line and open a empty line for inserting
Eg: cj or c1j - delete current and below 1 line and open a empty line
for inserting
Deletion
x or d<spacebar> X Delete character under current cursor, character before current cursor
position
dG or d]] or cG[esc] Delete from current cursor till end of file
dd or :.d Delete current line
d$ or D Delete from current cursor position till end of line
dn Delete n lines from current line
:nd To delete nth line in a file
USAGE
dtP - delete till character P in current line
d3j - delete current and below 3 lines
d) - delete till next sentence
:.-2d - delete 2nd line before current line
:.,3d - if you are in first line, this will delete 3lines including
current line
db - delete from current character till beginning of word
:m,nd - delete lines from line number m to n
:/<char or pattern>/ Delete first occurrence of pattern in a file
:g/<char or pattern>/d Delete all lines that contain pattern
d/pattern Delete upto next occurrence of pattern
Copy/Move
:m co n or :m t n Copy lines from line m to n
:m co $ or :m t $ Copy lines from line m to end of file
:m,n co dest or :m,n t dest Copy lines from line m to n to dest
Cut/Copy/Paste
mp To mark a position using m and assign it to buffer p. At most you can use
26 buffer and assign it to any character from a - z (lower case)
`p To move to marked position p
'p To move to first non-white space character of marked(p) line
d`p To delete from current position to marked(p) position
y`p Yank/copy from current position to marked(p) position
"zy`p Copy up to marked(p) position and put it into named(z) buffer
"zd`p Delete up to marked(p) position and put it into named(z) buffer
"zp Paste from named(z) buffer
"xdd Delete the current line and copy into buffer(x)
"xndd Delete n lines from current line and copy to buffer(x)
"xdw Delete current word and copy to buffer(x)
"xndw Delete n words from current word and copy to buffer(x)
"xyw Yank/copy a word and put it into buffer(x)
yy or Y Yank/copy current line
y$ Yank/copy from current cursor position till line of the end
y0 or y^ Yank/copy from current cursor position till beginning of the end
Eg: yw, y3w, y3b, y2l, yl, yk, yj, yH, Ym, YL
p P To paste copied lines from buffer after/before current line
We can use same character for marking and for naming buffer as both are
independent. if capital letter is used for buffer then new content is
appended to old one
We can recover last 9 deletions by doing Eg:"1p "2p as it stored in
automatic named buffer from 1-9 (1 contains most recent deletion).
Alternate way is you can use "1p u.u.u.u. to recover last 9 deletion
File RW
:w <file> To write buffer/opened_file into new file
:w %.<new file> To write buffer/opened_file into new file with specified prefix (used for
file backup)
:m,n w <file> Write into a new file having lines from m to n
:m,n w>> <file> Append into specify file having lines from m to n
:r <file> To read the file content and paste it in current cursor position
:nr <file> To read the file content and paste it in nth line
:r !cmd Read the output of shell command and paste it in current cursor position
:!cmd For temporary shell cmd execution Eg: :96,99!sort
:sh Exit from vi shell promt, to return back type exit in shell prompt
ZZ or :wq or :x Write and quit from the file
:q! Quit with discarding changes in file
Miscellaneous
:= Gives the total number of lines in a file
ctrl+u Undo last change
U Undo all changes made in the current line provided not to move other line
~ Reverse current character case
. Repeat last action
ctrl+g or :f Gives current line number and file name
vi -R <file> Open file in read only mode
in insert
mode