VI - Visual Editor(The king of all editors)
Commands Complete Cursor movements h or [back space] l or [spacebar] k or ^p j or ^n +, ^ or _ 0 or |, $ n[enter] n| w W e E b B H M L z[enter] z. z:0, :$ or :% or G '' `` :n or nG :+n :-n ) ( ]] [[ } { % ^f ^b ^d ^u ^e ^y Search fm Fm tm Tm /w ?w
Examples /^str /str$ /\<str
Move one character left from current cursor position Move one character right from current cursor position Move to previous line Move to next line Move to first non-white space character of next, previous line Move to first non-white space character on current line Move to first, last position in a line Move to nth line in a file Move to nth character from current position in a line Move to next word, move to next word(ignore punctuation) Move to end of the word, move to end of the word(ignore punctuation) Move to previous word, move to previous word(ignore punctuation) Move to top, middle, bottom of screen Place the current line to top, middle, bottom Use: /<pattern>/z Move to 1st line, Move to last line in the file Acts like toggle, used to move from current line <-> beginning of file 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 () {} [] Scroll forward, backward one full screen Scroll forward, backward half a screen Show one more line at bottom, top of the screen Characters $ . * [ ] ^ \ should be escaped using \ while search pattern containing above special characters Move forward/backward to character m Move forward/backward to character before m To repeat the above search use ;(for forward) ,(for backward) Search forward for w Search backward for w To repeat search in downwards use n or // To repeat search in upwards use N or ?? Match words starting with str (Word should present as 1st word in a line) Match words ending with str (Word should present as last word in a line) Match words starting with str
/str\> /[a-d]str /[^a-d]ind /a.u /a* /a.* /\
/a.[123] Search & Replace :s/oldstring/newstring/flags
Examples :s/str1/str2/g :m,n s/str1/str2/ :1,. s/str1/str2/g :.,$ s/str1/str2/g :%s/str1/str2/g or :1,$ s/str1/str2/g :%s/str1/str2/gc :g/^WARNING/s/\<not\>/NOT/ :%s/^/#/g :g/str1/command
/pattern/+n ?pattern?-n :/pat1/,/pat2/d :.,/pat/m23
Misc Examples :s/str1/str2/g (alternate for :%s/str1/str2/gc)
Match ending with str Match all words having a to d as 1st character and end with str Match all words not having a to d as 1st character and end with str Match 3 character word start with a end with u (Note: '.' match single character) Match all words starting with a (Note: * match zero or more char) Match all 2 or more character words starting with a (Note: '.' ensure at least one character present after a) Match all 3 or more character words starting with a and ending with u Match all 3 character words starting with a, followed by any single character, end with character 1 or 2 or 3
ssubstitute gglobal (used for replacing all occurence in current line) flags- 'c' ask for confirmation while replacing, 'y' for yes, 'n' for no Replace all occurrence of str1 with str2 in current line Replace 1st occurrence of str1 with str2 from lines m to n Replace all occurrence of str1 with str2 from 1st line till current line Replace all occurrence of str1 with str2 from current line till end of file as above but act in whole file Same Same as above but ask for user confirmation to replace str1. y-for yes nfor no This will search for all lines that begin with WARNING and change the first word not on those lines to NOT comment all lines :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 Place cursor n lines after line containing pattern Place cursor n lines before line containing pattern Deletes from the next line(after current line) contains pat1 to the line that contains pat2 Move all lines to line number 23 from current line(.) till lines containing pat
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 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.
/str1
Insertion a A or $a or $s i I o O ^i ^j ^h ^t ^w n>> n<< >% <%
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 Append at cursor, append at beginning of line Insert at cursor, insert at beginning of line Open new line after current line, before current line Insert tab in insert mode Create new line in insert mode Backspace in insert mode Move to next shift width Move back to beginning of previous word in insert mode 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 c0 c$ C r s cl cc S R ctP cnc[esc]
Deletion x or d<spacebar>
Change word, change word by ignoring space, change till beginning of word Change from current position to beginning of line Change from current position till end of line To replace single character Substitute one or more character for one character Replace whole line Replace till we press [Esc] Change till first occurrence of P 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
dG or d]] or cG[esc] dd or :.d d$ or D dn
X
Delete character under current cursor, character before current cursor position Delete from current cursor till end of file Delete current line Delete from current cursor position till end of line Delete n lines from current line
:nd
:// :g//d d/pattern Copy/Move :m co n or :m t n :m co $ or :m t $ :m,n co dest or :m,n t dest :m m n :m m $ :m,n m dest Cut/Copy/Paste mp `p 'p d`p y`p "zy`p "zd`p "zp "xdd "xndd "xdw "xndw "xyw yy or Y y$ y0 or y^
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 Delete first occurrence of pattern in a file Delete all lines that contain pattern Delete upto next occurrence of pattern
Copy lines from line m to n Copy lines from line m to end of file Copy lines from line m to n to dest Move lines from line m to n Move lines from line m to end of file Move lines from line m to n to dest
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) To move to marked position p To move to first non-white space character of marked(p) line To delete from current position to marked(p) position Yank/copy from current position to marked(p) position Copy up to marked(p) position and put it into named(z) buffer Delete up to marked(p) position and put it into named(z) buffer Paste from named(z) buffer Delete the current line and copy into buffer(x) Delete n lines from current line and copy to buffer(x) Delete current word and copy to buffer(x) Delete n words from current word and copy to buffer(x) Yank/copy a word and put it into buffer(x) Yank/copy current line Yank/copy from current cursor position till line of the end 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 We can use same independent. if appended to old
lines from buffer after/before current line character for marking and for naming buffer as both are capital letter is used for buffer then new content is 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 :w %. :m,n w :m,n w>> :r :nr :r !cmd :!cmd :sh ZZ or :wq or :q! Miscellaneous := ctrl+u U ~ . ctrl+g or :f vi -R
:x
To write buffer/opened_file into new file To write buffer/opened_file into new file with specified prefix (used for file backup) Write into a new file having lines from m to n Append into specify file having lines from m to n To read the file content and paste it in current cursor position To read the file content and paste it in nth line Read the output of shell command and paste it in current cursor position For temporary shell cmd execution Eg: :96,99!sort Exit from vi shell promt, to return back type exit in shell prompt Write and quit from the file Quit with discarding changes in file
Gives the total number of lines in a file Undo last change Undo all changes made in the current line provided not to move other line Reverse current character case Repeat last action Gives current line number and file name Open file in read only mode
in insert mode