vim
A gentle guide to vim
Vim is an improved version of vi editor. Its interface can be scary at the beginning but after understanding its modes and a few commands, it can be very productive.
Vim modes
Vim has two important modes to know:
command mode: this is the default mode, you can go to other modes from the command.
From the command mode type
ito go to insert mode.
insert mode: this is for editing a file as you are expecting, i.e., what you type will be inserted where the cursor is blinking.
Press
ESCto go back to command mode.
Navigating into a document
h,J,kandlmove the cursor one character to left, down, up and right. Arrow keys also work.0(zero) and$move the cursor to the beginning and end of the line. Home and End keys also work.wandbmove the cursor to the next and the previous word. Using Ctrl + left and right arrows move from words separated by spaces.ggandGmove the cursor to the beginning or end of file. Ctrl+Home and Ctrl+End also work.
The commands can also be executed multiple times when preceded by a number, e.g., 3w will move the cursor 3 words forward.
Changing a document
ddelete from cursor, e.g.,dhdeletes the character on the left,d0deletes until the beginning of the text.uandCtrl+rundo and redo.
Selecting
v,VandCtrl+vgo to visual from the cursor, entire line and by columns
Copying and pasting
ycopypandP: paste after current line and on the current line
Searching and replacing
/textand?textsearch "text" going forward and backwardnandNgo to next occurrence going forward and backward:%s/text/new text/gcand:%s/text/new text/greplace "text" by "new text" asking to confirm on every occurrence and replacing without asking.
Saving and quitting
:q!discard changes and quit:wsave changes:wqsave and quit
Last updated
Was this helpful?