Vi /

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]]

Examples:

  • Without any arguments, vi will just start a blank new file.
  • vi a.txt will open vi with the file a.txt

Insertion Mode

In insert mode, you can type in text

i switches to insert mode and a switches to insert mode moving the cursor one character forward.

<Escape> can be used to exit insert mode.

Command Mode

Command mode is used to send commands to the text editor. User : to enter command mode.

Saving and Opening Files

:w saves a files

:w [filename] Saves the file as filename

:q exits the editor

:wq saves and exits

:q! exits without saving

:e [filename] opens file filename for editing

:e! [filename] discards changes in current buffer and opens file filename for editing

Normal Mode

By default vi , opens in Normal Mode. Normal Mode allows you to cut, copy, paste, etc. To come back to Normal Mode from any mode, press Escape .

[num]command will execute command num times.

Movement

Vi uses h, j, k, l for movement. It allows for faster editing as the keys are present in the home row.

  • h moves one character left
  • j moves one line down
  • k moves one line up
  • l moves one character right
             k (up)
 h (left)            l (right)
             j (down)

w moves to the beginning of the next word while b moves to beginning of the previous word.

e moves to the end of the next word.

0 moves to the beginning of the sentence while $ moves to the end of the sentence.

G moves to the end of the file

[num]G moves to the line number num

Examples:

  • Initial
Hello! I like Vi.
^
  • w will move one word forward to !
Hello! I like Vi.
     ^
  • 2w will move two words forward to l
Hello! I like Vi.
         ^
  • e will move to the end of the word
Hello! I like Vi.
            ^
  • $ will move to the end of the line.
Hello! I like Vi.
                ^
  • 3b will move three words back
Hello! I like Vi.
       ^
  • 0 will move to the beginning of the line
Hello! I like Vi.
^

Cutting and Pasting

yw copies a word, yy or Y copies a line.

dw cuts/deletes a word, dd cuts a line, D cuts a line from the cursor till the end (same as d$ ).

cw changes a word, cc changes a line, C changes a line from the cursor till the end (same as c$ ).

y, d and c can be combined with $0, $, f, t, etc.$

p is used to paste after the cursor while P is used to paste before the cursor.

Searching

/ is used to search forwards.

? is used to search backwards.

n goes to the next occurrence while N goes to the previous occurrence.

f[char] goes to the first occurrence (after the cursor) of char. F[char] does the same thing but backwards.

t[char] goes to the position just before the first occurrence (after the cursor) of char. T[char] does the same thing but backwards.

Examples:

  • Initial
Hello! I like Vi.
^
  • fl goes to the third character in the line
Hello! I like Vi.
  ^
  • tI goes to the seventh character in the line
Hello! I like Vi.
      ^
  • Fl goes to the fourth character in the line
Hello! I like Vi.
   ^
  • TH goes to the second character in the line because e is the character just before H backwards.
Hello! I like Vi.
 ^