Vi and Vim Basics

From WikiMLT

When you run vi or vim it is start­ed in com­mand mode. In or­der to switch to edit/​​​insert mode press i. Now you can ed­it the cur­rent line. To go back to the com­mand mode press Esc. In­sert modes in­clude: i, I, a, A, o, and O.

To quit type :q. To save the changes and quit type :wq. To dis­card the changes and quit type :q!. To save the file at the cur­rent stage type :w.

Save the file and close the vi ed­i­tor us­ing any one of the fol­low­ing meth­ods that saves changes:

Com­mand Func­tion / Keys
:x Will save and close the file.
:wq Will write to file and quit.
:wq! Will write to a read-on­ly file, if pos­si­ble, and quit.
ZZ Will save and close. No­tice that no colon : is used in this case.
:q! Ex­it with­out sav­ing changes
:e! Dis­card changes and re­load file
:w! Write to read-on­ly, if pos­si­ble.

To get help type :help or :help <topic-name>, or :helpgrep <phrase>. To go back to the ed­i­tor type :q – note you nay need press : more than one time to reach the com­mand line at the bot­tom (ref­er­ence).

With­in the com­mand mode you can nav­i­gate via the file's con­tent and do some changes by the fol­low­ing keys / com­mands.

Key / Com­mand Func­tion
j Moves cur­sor down one line (same as down ar­row).
8j 2j Move 8 or 2 lines down.
k Moves cur­sor up line (same as up ar­row).
8k 2k Move 8 or 2 lines up.
l Moves cur­sor to the right one char­ac­ter (same as right ar­row).
8l 2l Move 8 or 2 char­ac­ters to the right.
h Moves cur­sor to the left one char­ac­ter (same as left ar­row).
8h 2h Move 8 or 2 char­ac­ters to the left.
w Shift+W Moves cur­sor to be­gin­ning of next word. Move one word for­ward.
2w 5w Move 2 or 5, or n words for­ward.
e Moves cur­sor to end of word
b Moves cur­sor to be­gin­ning of pre­vi­ous word
$ End Moves cur­sor to end of cur­rent line (same as End key)
0 (zero) Home Moves cur­sor be­gin­ning of cur­rent line (same as Home key)
3G Jumps to third line (nG jumps to the nth line)
1G Jumps to first line
Shift+G Jumps to the last line
dw Delete Word.
2dw 3dw Delete 2 or 3, or n words.
u Undo.
2u 5u Un­do last 2 or 5, or n com­mands.
x Delete a char­ac­ter.
xx xxxx Delete 2 or 4 char­ac­ters.
2x 7x Delete 2 or 7, or n char­ac­ters.
X Delete a char­ac­ter to the left of the cur­sor.
XX XXXX Delete 2 or 4 char­ac­ters to the left of the cur­sor.
2X 5X Delete 2 or 5, or n char­ac­ters to the left of the cur­sor.
dd Delete a line.
2dd 3dd Delete 2 or 3 or n lines.
p Paste the delet­ed line(s) be­low the cur­rent line.
Shift+D d$ Delete the char­ac­ters from the cur­rent cur­sor po­si­tion to the end of the line.
Shift+J Join two lines, the cur­rent and the next.
3J 5J Join 3 or 5, or n lines
yw Copy (or “yank”) the cur­rent word. More pre­cise­ly from the cur­rent cur­sor po­si­tion to the end of the word.
Shift+P Paste (or “put”) the copied word be­fore the cur­rent cur­sor po­si­tion.
Shift+` ~ Change the next let­ter to up­per case if it is low­er­case and vice ver­sa.
o Open a blank line be­low (to the cur­sor po­si­tion) and start edit/​​​insert mode. Low­er­case "o".
O Open a blank line above (to the cur­sor po­si­tion) and start edit/​​​insert mode. Up­per­case "O".
cw Over­ride. When you are at the be­gin­ning of a word when press cw you will be in in­sert mode and you will be able to type over a word.
i Add text at the cur­rent cur­sor po­si­tion.
I Add text at the be­gin­ning of a line.
a Start edit/​​​insert mode to ap­pend text to the right of the cur­sor.
A Add text at the end of a line.

Search for and re­place the word text with TEXT:

:%s/text/TEXT/g

Search for the next oc­cur­rence of the word line:

/line
  • To search for the next in­stance of the same word (pat­tern) press n.

Search back­ward for the word line:

?line
  • To search for the next in­stance of the same word (pat­tern) press n.