It has 3 modes:
Ex mode <----> Command mode <-----> Insert mode.
You can never go from Insert to Ex mode directly or vice versa. You always
first need to go to command mode. Enter command mode by typing Esc and enter Ex mode by typing :.
For cursor movement:
Cursor movement commands: {motion} commands:
k
h l
j
G - Last line in fileSome more cursor movement commands:
gg - First line in file
% - Jump to matching brace (()}{[])
m{1etter} - example ma to mark a line. There can be at
max 26 marks in the file.
`{mark} - example `a to jump to the named mark.
`` - Jump to the last line where you jumped or searched from.
/expr - forward search where expr = is a regex.
?expr - backward search.
n - repeat last search in the same direction.
N - repeat last search in the opposite direction.
w - move cursor forward by one word.
b - move cursor backward by one word.
Cntl + F - move one screen forward.
Cntl + B - move one screen backward.
f{char} - position cursor at the matching {char} to the right.
t{char} - position cursor just before the matching {char} to the right.
F{char} - position cursor at the matching {char} to the left.
T{char} - position cursor just before the matching {char} to the left.
Basic insert commands:
i - enter insert mode.
a - enter insert mode after character under cursor.
I - enter insert mode starting before first non blank character on the line.
A - enter insert mode starting after last non blank character on the line.
o - enter insert mode starting on a new line after current cursor line.
O - enter insert mode starting on a new line before current cursor line.
Change commands:
C - delete everything from cursor to end of line.
c{motion} - delete everything from cursor position as determined by the motion (one of the cursor movement commands).
Example:
c5{h} - deletes 5 chars to the left (as h is for
left movement).
c2w - deletes 2 words and puts in insert mode.
S - Substitute/change current line entirely. Delete all text on current line and put in insert mode.
Patterns for using change commands:
2cw - change 2 words.
cta - delete everything upto next occurrence of letter a.
5cta - delete everything upto 5th occurrence of letter a.
5S - delete current line and subsequent 4 lines.
Miscellaneous commands:
u - undo
Cntl + R - redo
Cntl + N/Cntl + P - autocompletion with text that already exists in the in the document.
. - repeat the last change.
zt - redraw the screen placing current line to top of screen.
zz - redraw the screen placing current line to the center of screen.
zb - redraw the screen placing current line to the bottom of screen.
Delete, cut and paste commands:
D - Delete from current cursor position to the end of line.
d{motion} - delete from current curson position to the position determined by {motion} command. Example,
d5w - delete next 5 words.
dn - delete till next matching search pattern.
d`a - delete till marked line named a.
y{motion} - yank or copy from current cursor position to the position
determined by {motion} command. Example,
y5w - copy next 5 words.
yG - copy till the end of file.
p - put the character in registry/clipboard after current character under cursor.
P - put the character in registry/clipboard before current character under cursor.
dfa - delete until character first character a from current cursor position.
dta - delete upto character first character a from current cursor position.
yy4p - copy current line and paste it 4 times.
dG - delete everything from current line till end of file.
y50G - copy everything from current line till line 50.
d5l - delete 5 characters to the right (l is for right motion).
5dd - delete 5 lines including current one.
Ex mode:
Ex mode commands are of the form:
:[firstline][,lastline]command
Example:
:25,30d - delete lines from 25 to 30.Line numbers are optional.
If no line number is specified, command applies to current line.
If only one line number is specified, command applies to that line.
To affect a range of lines, we need to specify both firstline and lastline.
Shortcuts for specifying line numbers in Ex mode:
. - current line number. Example- :.,50d - delete from current to line 50.
$ - last line number in file.
% - entire file - same as specifying 1,$.
`a - line number of bookmarked line a.
/expr/ - next line that matches expr.
?expr? - previous line that matches expr.
\/ - next line that matches the most recent expr.
\? - prev line that matches the most recent expr.
\& - next line that matches the most recent substitution.
Examples:
:/Begin/,/End/d - deletes lines from first line that has word Begin till
last line which has word End.
10,40w bkup.dat - saves the lines 10 to 40 in a file bkup.dat.
Essential Ex Commands:
:w {filename} - save the current buffer to filename.
:q - quite the current buffer.
:x - same as :wq - quite and save unsaved data.
:e {filename} - open named file for editing in a new buffer.
:d - delete current line or range of lines
:map {a} {b} - remap the keys used in command mode.
:set {arguments} - configure vim (mostly to be used in .vimrc)
:help - vim help.
Search and Replace:
:s/search/replacement/flags
If search or replacement string contains / character then either we can
escape it with a \ or use a different delimiter (like #) other than / for
eg:
:s#/usr/share/file1#/usr/share/file2/#
Usage examples:
first occurrence single line :s/bash/perl/ all occurrences single line :s/bash/perl/g first occurrence between line range: :23,100s/bash/perl/ all occurrences between line range: :23,100s/bash/perl/g first occurrence in whole text: :%s/bash/perl/ all occurrences whole text: :%s/bash/perl/g
Run ctags as follows in the source directory and it generates an index file
called tags of source files in current directory and its subdirectories.
ctags -R
Now in vim we can use each source file as a web page with each symbol
(variable, function) having hyperlinks, such that, if you do a cntl + ] on
the current symbol then it will take you to the definition of the symbol.
Following are commands for browsing code in vim:
Cntl + ] - jump to the tag under cursor (ie go to definition of function,
class, declaration of variable).
Cntl + T - Return to current tag from most recent jump off point.
:pop - Same as Cntl + T except that we can specify count to go back
multiple levels.
:tn - Jump to next match (when tag produces more than one match in
case of C++ overloaded functions).
:tp - Jump to previous match (in case of multiple matches).
:ts name - Shows a list of matching tags you can select from.
:tags - Show current tag stack with one line for each tag.
Lastly, to build c/c++ code:
:make {args}
:set makeprg=ant
One nice .vimrc can be found at:
http://research.iiit.ac.in/~masatran/config/.vimrc
Use
wget http://research.iiit.ac.in/~masatran/config/.vimrcto get it.