Вы находитесь на странице: 1из 10

VI - Visual Editor(The king of all editors)

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

Characters $ . * [ ] ^ \ should be escaped using \ while search pattern


Search containing above special characters
fm Fm Move forward/backward to character m
tm Tm Move forward/backward to character before m
To repeat the above search use ;(for forward) ,(for backward)
/w Search forward for w
?w Search backward for w
To repeat search in downwards use n or //
To repeat search in upwards use N or ??
Examples
/^str Match words starting with str (Word should present as 1st word in a line)
/str$ Match words ending with str (Word should present as last word in a line)
/\<str Match words starting with str
/str\> Match ending with str
/[a-d]str Match all words having a to d as 1st character and end with str
/[^a-d]ind Match all words not having a to d as 1st character and end with str
/a.u Match 3 character word start with a end with u (Note: '.' match single
character)
/a* Match all words starting with a (Note: * match zero or more char)
/a.* Match all 2 or more character words starting with a (Note: '.' ensure at
least one character present after a)
/\<a.*u\> Match all 3 or more character words starting with a and ending with u
/a.[123] Match all 3 character words starting with a, followed by any single
character, end with character 1 or 2 or 3

Search & Replace


:s/oldstring/newstring/flags s- substitute
g- global (used for replacing all occurence in current line)
flags- 'c' ask for confirmation while replacing, 'y' for yes, 'n' for no
Examples
:s/str1/str2/g Replace all occurrence of str1 with str2 in current line
:m,n s/str1/str2/ Replace 1st occurrence of str1 with str2 from lines m to n
:1,. s/str1/str2/g Replace all occurrence of str1 with str2 from 1st line till current line
:.,$ s/str1/str2/g Replace all occurrence of str1 with str2 from current line till end of
:%s/str1/str2/g or :1,$ s/str1/str2/g file as above but act in whole file
Same
:%s/str1/str2/gc Same as above but ask for user confirmation to replace str1. y-for yes n-
for no
:g/^WARNING/s/\<not\>/NOT/ This will search for all lines that begin with WARNING and change the
first word not on those lines to NOT
:%s/^/#/g comment all lines
:g/str1/command :g/str/ place the cursor at the last line having search string
:g/Start/d delete all lines containing Start
:g/Start/p displays all lines containing Start
:10,20 g/Start/p displays all lines containing Start between lines 10 and
20
:g!/Start/nu displays all lines containing Start with line numbers
/pattern/+n Place cursor n lines after line containing pattern
?pattern?-n Place cursor n lines before line containing pattern
:/pat1/,/pat2/d Deletes from the next line(after current line) contains pat1 to the line
that contains pat2
:.,/pat/m23 Move all lines to line number 23 from current line(.) till lines
containing pat

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

:m m n Move lines from line m to n


:m m $ Move lines from line m to end of file
:m,n m dest Move 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