Vim (Vi IMproved) is a highly configurable text editor built to make creating and changing any kind of text very efficiently. It is included as "vi" with most UNIX systems and with Apple OS X.I attempted to learn Vim quite a few times before but could not cope up with it. But recently I thought I should at least know very basic usage of Vim. So I started learning the absolute basics. Here I am putting all the basic commands and usage of Vim for any absolute beginner like me. This can be helpful to get started with Vim I think. But there is a lot of things to learn about this handy tool that will take much time and practice. One will get proficient in using Vim only by using it consistently.
Vim has two basic modes:
- One is
INSERTmode, in which you write text as if in a normal text editor.
- Another is
NORMALmode which provides you efficient ways to navigate and manipulate text. This is also called
Commandmode cause we can do various vim commands on this mode.
To change between modes, use
ESC for normal mode and
i for insert mode.
|:e filename||Open filename for editon|
|:q!||Quit without saving|
|:x||Save and Exit|
|:sav name||Save current file as name|
|.||Repeat last change made in |
hto move cursor left (←)
lto move cursor right (→)
kto move cursor up (↑)
jto move cursor down (↓)
bmoves to the beginning of the word
emoves to the end of the word
wmoves to the beginning of the next word
Number Based Movements
number before each command can execute that command that many times. e.g
3w will move to the 3rd next word.
Insert Text Repeatedly
To insert the same text multiple times use
3<i>go<esc> will write
Find a Character
To find and move to the next (or previous) occurrence of a character, use
fo finds next 'o'. You can combine f with a number. e.g. you can find 3rd occurrence of 'q' with
Go to Matching Parenthesis
In text that is structured with parentheses or brackets,
% to jump to the matching parenthesis or bracket.
Start/End of Line
To reach the beginning of a line, use
0. For the end of line use
Find Word Under Cursor
Find the next occurrence of the word under the cursor with
*, and the previous with
Go to Line
gg takes you to the beginning of the file;
G to the end. To jump directly to a specific line, give its line number along with
5G will take you to the fifth line of the file.
Search for Text
Searching text is a vital part of any text editor. In Vim, press
/, and give the text to search for. The search can be repeated for next and previous occurrences with
N respectively. For advanced use cases, it's possible to use regexps that help to find the text of a particular form.
Insert New Line
To insert text into a new line after the current line use
o and to insert a new line before the current line use
After a new line is created, the editor is set to
Removing a Character
X delete the character under the cursor and to the left of the cursor, respectively.
Also adding a
X can perform the action that many times. e.g
5x will remove the next five characters including the character under the cursor and
5X will remove the previous five characters excluding the character under the cursor.
Replace a Character
r to replace only one character under the cursor.
d is the delete command. It can be combined it with movements, e.g.
dw deletes the characters on the right side of the cursor up to the beginning of the next word.
de deletes all the characters of the word on the right side of the cursor including the character under cursor.
db will delete the previous word if the cursor is under the first letter of a word or else it will delete the characters left to the cursor upto the beginning of the word.
It also copies the content, so that you can paste it with
p to another location.
dd will delete the whole line.
To repeat the previous command, just use
R to enter
REPLACE mode. In this mode characters under the cursor can be replaced.
v to enter the
VISUAL mode and
V to enter
VISUAL LINE mode. In this mode the text can be selected by the movement keys before deciding what to do with it.
Selected text can be deleted/cut using
d or copied using
y. It can be pasted after the cursor using
p or before the cursor using