Skip to content

Vi Introduction

Vi is the free and open-source editor that is present on most UNIX systems.

Vi is a modal editor. It has three main modes (ways to interact with the editor).

[[https://en.m.wikibooks.org/wiki/Learning_the_vi_Editor]] is a good resource for an introduction.

Starting Vi

vi [[files to edit]]##ENDCODEBLOCK##
Examples:
* Without any arguments, ##STARTCODEBLOCK##
 vi ##ENDCODEBLOCK##
 will just start a blank new file.
* ##STARTCODEBLOCK##
 vi a.txt ##ENDCODEBLOCK##
 will open ##STARTCODEBLOCK##
 vi ##ENDCODEBLOCK##
 with the file ##STARTCODEBLOCK##
 a.txt ##ENDCODEBLOCK##
## Insertion Mode
In insert mode, you can type in text
 i ##ENDCODEBLOCK##
 switches to insert mode and ##STARTCODEBLOCK##
 a ##ENDCODEBLOCK##
 switches to insert mode moving the
cursor one character forward.
 <Escape> ##ENDCODEBLOCK##
 can be used to exit insert mode.
## Command Mode
Command mode is used to send commands to the text editor. User ##STARTCODEBLOCK##
 : ##ENDCODEBLOCK##
 to enter command mode.
### Saving and Opening Files
 :w ##ENDCODEBLOCK##
 saves a files
 :w [filename] ##ENDCODEBLOCK##
 Saves the file as ##STARTCODEBLOCK##
 filename ##ENDCODEBLOCK##
 :q ##ENDCODEBLOCK##
 exits the editor
 :wq ##ENDCODEBLOCK##
 saves and exits
 :q# ##ENDCODEBLOCK##
 exits without saving
 :e [filename] ##ENDCODEBLOCK##
 opens file ##STARTCODEBLOCK##
 filename ##ENDCODEBLOCK##
 for editing
 :e# [filename] ##ENDCODEBLOCK##
 discards changes in current buffer and opens file ##STARTCODEBLOCK##
 filename ##ENDCODEBLOCK##
 for editing
## Normal Mode
By default ##STARTCODEBLOCK##
 vi ##ENDCODEBLOCK##
, opens in Normal Mode.
Normal Mode allows you to cut, copy, paste, etc.
To come back to Normal Mode from any mode, press ##STARTCODEBLOCK##
 Escape ##ENDCODEBLOCK##
.
 [num]command ##ENDCODEBLOCK##
 will execute ##STARTCODEBLOCK##
 command ##ENDCODEBLOCK##
 ##STARTCODEBLOCK##
 num ##ENDCODEBLOCK##
 times.
### Movement
Vi uses ##STARTCODEBLOCK##
 h, j, k, l ##ENDCODEBLOCK##
 for movement.
It allows for faster editing as the keys are present in the home row.
* ##STARTCODEBLOCK##
 h ##ENDCODEBLOCK##
 moves one character left
* ##STARTCODEBLOCK##
 j ##ENDCODEBLOCK##
 moves one line down
* ##STARTCODEBLOCK##
 k ##ENDCODEBLOCK##
 moves one line up
* ##STARTCODEBLOCK##
 l ##ENDCODEBLOCK##
 moves one character right
             k (up)
 h (left)            l (right)
             j (down)


 w ##ENDCODEBLOCK##
 moves to the beginning of the next word while
 b ##ENDCODEBLOCK##
 moves to beginning of the previous word.
 e ##ENDCODEBLOCK##
 moves to the end of the next word.
 0 ##ENDCODEBLOCK##
 moves to the beginning of the sentence while ##STARTCODEBLOCK##
 $ ##ENDCODEBLOCK##
 moves to the end
of the sentence.
 G ##ENDCODEBLOCK##
 moves to the end of the file
 [num]G ##ENDCODEBLOCK##
 moves to the line number ##STARTCODEBLOCK##
 num ##ENDCODEBLOCK##
Examples:
* Initial
Hello# I like Vi.
^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
 w ##ENDCODEBLOCK##
 will move one word forward to ##STARTCODEBLOCK##
 # ##ENDCODEBLOCK##
Hello# I like Vi.
     ^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
 2w ##ENDCODEBLOCK##
 will move two words forward to ##STARTCODEBLOCK##
 l ##ENDCODEBLOCK##
Hello# I like Vi.
         ^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
 e ##ENDCODEBLOCK##
 will move to the end of the word
Hello# I like Vi.
            ^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
 $ ##ENDCODEBLOCK##
 will move to the end of the line.
Hello# I like Vi.
                ^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
 3b ##ENDCODEBLOCK##
 will move three words back
Hello# I like Vi.
       ^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
 0 ##ENDCODEBLOCK##
 will move to the beginning of the line
Hello# I like Vi.
^##ENDCODEBLOCK##
### Cutting and Pasting
 yw ##ENDCODEBLOCK##
 copies a word, ##STARTCODEBLOCK##
 yy ##ENDCODEBLOCK##
 or ##STARTCODEBLOCK##
 Y ##ENDCODEBLOCK##
 copies a line.
 dw ##ENDCODEBLOCK##
 cuts/deletes a word, ##STARTCODEBLOCK##
 dd ##ENDCODEBLOCK##
 cuts a line, ##STARTCODEBLOCK##
 D ##ENDCODEBLOCK##
 cuts a line from the cursor till the end (same as ##STARTCODEBLOCK##
 d$ ##ENDCODEBLOCK##
).
 cw ##ENDCODEBLOCK##
 changes a word, ##STARTCODEBLOCK##
 cc ##ENDCODEBLOCK##
 changes a line, ##STARTCODEBLOCK##
 C ##ENDCODEBLOCK##
 changes a line from the cursor till the end (same as ##STARTCODEBLOCK##
 c$ ##ENDCODEBLOCK##
).
 y, d and c ##ENDCODEBLOCK##
 can be combined with $0, $, f, t, etc.$
 p ##ENDCODEBLOCK##
 is used to paste after the cursor while ##STARTCODEBLOCK##
 P ##ENDCODEBLOCK##
 is used to paste before
the cursor.
### Searching
 / ##ENDCODEBLOCK##
 is used to search forwards.
 ? ##ENDCODEBLOCK##
 is used to search backwards.
 n ##ENDCODEBLOCK##
 goes to the next occurrence while ##STARTCODEBLOCK##
 N ##ENDCODEBLOCK##
 goes to the previous occurrence.
 f[char] ##ENDCODEBLOCK##
 goes to the first occurrence (after the cursor) of ##STARTCODEBLOCK##
char##ENDCODEBLOCK##
. ##STARTCODEBLOCK##
 F[char] ##ENDCODEBLOCK##
 does the same thing but backwards.
 t[char] ##ENDCODEBLOCK##
 goes to the position just before the first occurrence (after the cursor) of ##STARTCODEBLOCK##
char##ENDCODEBLOCK##
. ##STARTCODEBLOCK##
 T[char] ##ENDCODEBLOCK##
 does the same thing but backwards.
Examples:
* Initial
Hello# I like Vi.
^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
fl##ENDCODEBLOCK##
 goes to the third character in the line
Hello# I like Vi.
  ^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
tI##ENDCODEBLOCK##
 goes to the seventh character in the line
Hello# I like Vi.
      ^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
Fl##ENDCODEBLOCK##
 goes to the fourth character in the line
Hello# I like Vi.
   ^##ENDCODEBLOCK##
* ##STARTCODEBLOCK##
TH##ENDCODEBLOCK##
 goes to the second character in the line because ##STARTCODEBLOCK##
 e ##ENDCODEBLOCK##
 is the character just before ##STARTCODEBLOCK##
 H ##ENDCODEBLOCK##
 backwards.
Hello# I like Vi.
 ^##ENDCODEBLOCK##