Vim editor – Basic commands

Here is list of basic commands to work on Vim editor in unix environment. Hope this would help new learners and people preparing for interviews.
Best way to learn these commands is using -
vimtutor
go to unix shell and type command vimtutor. Follow instructions on this tutorial and practice it hands-on.

Start Vim
vim [filename]To start Vim from shell prompt

Exit Vim
<ESC> :q!exit without saving changes
<ESC> :wqsaving changes and exit

<ESC> key is used to return to normal mode. (you might be in editing mode)
Move Cursor
hleft
jdown
kup
lright

Motions
wfirst character of next word
elast character of next word
$last character of line
move faster -
0 (Zero)To move to start of line
nw e.g. - 2wMove forward by n words (e.g. - 2 words)
ggmove to start of the file
Gmove to bottom of the file

Undo
uundo last command
Uundo changes on whole line

Delete character, word, Line
xdelete character at cursor
dwdelete word, including last character (place cursor on first character of word)
dedelete word, excluding last character (place cursor on first character of word)
d$delete words from cursor position till end of line
dddelete line
delete more -
nd[motion] e.g. - 2dddelete n lines from cursor position (e.g. - delete 2 lines)

Insert and Append text
iinsert before cursor (press <ESC> after, to return to normal mode)
Aappend after the line (press <ESC> after, to return to normal mode)

Search
/[phrase]search pharse in file
ngo to next match
Ngo to previous match

Replace/substitute
s/old/newon current line, substitute first occurence of old with new
s/old/new/gsubstitute all old with new on current line
%s/old/new/gsubstitute all old with new in file
%s/old/new/gcsubstitute all old with new in file, ask for confirmation

These are few basic commands to begin and practice.
Please leave your comments here and let us know if this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *