5.1. vi and Vim

[Note]Note

This section is nearly finished but is still under development.

[Tip]Tip

Only the commands that start with a colon (:) (which are from the ex line editor) require pressing Enter to be executed. All other commands in vi and Vim are executed immediately after they are entered.

In addition, all vi and Vim commands are case-sensitive.

Vi (pronounced VEE-EYE)[37] “was the first real screen-based editor for Unix systems.”[38] It is present on all Unix and Unix-like operating systems (including Linux). Another frequently used text editor, GNU Emacs (discussed in Section 5.2, “GNU Emacs”), is usually (but not always) installed as well. You can start vi by typing vi or vi file_to_edit (such as vi lab0.c) at the prompt.

Vi is closely connected to the even older line-based text editor ex, whose commands begin with a colon (:). [reference! Also something (very) short about age/origin of vi.] In fact, you can start either editor from the command line and can switch between the two while using them. As can be seen from the tables below, vi users must know a few essential ex commands. However, a thorough coverage of ex is beyond the scope of this guide.

Since the original vi was a closed-source project,[39] developers created their own clones of vi, adding new features along the way. One of the most popular of these clones is Vim (short for vi improved), created by Bram Moolenaar. Vim seems to be the standard version of vi on Linux machines, although other vi clones, such as elvis and vile, exist. However, the rest of this section will focus exclusively on Vim. Although Vim has much in common with the original vi and with the various vi clones, there are differences among them, including incompatibilities between Vim and vi. This guide will not cover those distinctions; anyone who is interested can check the references listed in the further reading section.

Vim is a modal editor that fundamentally has two modes: a normal (or command) mode for entering commands and an insert mode for entering and editing text. By comparison, modeless editors such as Emacs are always in insert mode, so that the characters you type always appear as text on the screen. As listed in Table 5.10, “Other modes in Vim”, Vim also includes a replace mode, which differs from insert mode in that any characters typed will overwrite existing characters in the buffer, as well as three types of visual modes, which allow for selecting text for cutting or copying.

[Tip]Tip

You can return to normal mode at any time by pressing the Escape key.

[TODO - Importance of normal/command mode, operators/commands and motions/text-objects, delete vs. change vs. yank (and put?), etc.]

When vi was invented, the H, J, K, and L keys doubled as arrow keys, since there was no separate set of arrow keys on the keyboard. Thus vi and its clones still use those keys as arrow keys in the format shown in Table 5.1, “The Arrow Keys in Vim” below. Although the now-standard arrow keys work in Vim as expected, you may wish to try using the HJKL keys in their place, as you may find it faster to use them rather than having to reach to the arrow keys to use them.

To type a command like C-r, press the R key while holding down the Ctrl/Control key.

References to further reading on vi and Vim can be found in Section A.4.1, “More on vi and Vim”.

Table 5.1. The Arrow Keys in Vim

 k (Up) 
h (Left)j (Down)l (Right)

Table 5.2. Basic Vim commands, Part 1

CategoryCommandAction
Opening, Saving, and Exiting---
~

:o filename

Open (or create) file
~

:w

Save current file
~

:w filename

Save current file as filename (will not overwrite)
~

:w! filename

Save current file as filename (will overwrite if necessary)
~:wq or ZZSave file and quit Vim
~:q! or ZQQuit Vim without saving
Getting Help---
~

:help

Vim help
~vimtutor from command line or :help tutor while in Vim Vim tutor
Keys Found in Other Editors---
~

Arrow keys

Move cursor by one character/line
~Delete (but not Backspace, except in insert mode)Delete one character at a time

Table 5.3. Basic Vim commands, Part 2

CategoryCommandAction
Commands on Commands---
~

:!<command>

Run <command> (for example, ls) in shell
~

Escape

Enter normal mode (also quits any command being typed)
Undo and Redo---
~

u

Undo previously typed command
~

U

Undo changes to most recently edited line
~

:e!

Revert file to last saved version
~

C-r

Redo most recently performed undo
~

.

Redo most recently typed non-movement command

Table 5.4. Moving through the buffer in Vim

CommandAction

C-g

Indicates your location in the file and other information
gg or 1GMove to start of buffer

G

Move to end of buffer

<number>G

Move to line <number>

C-b

Move up by one screen

C-f

Move down by one screen

Table 5.5. Moving through screens and lines in Vim

CategoryCommandAction
Moving Within the Screen---
~

H

Move to top of screen
~

M

Move to middle of screen
~

L

Move to bottom of screen
Moving Along a Line---
~

0

Move to start of line
~

^

Move to first non-whitespace character of line
~

$

Move to end of line
Moving Word by Word---
~

b

Move to start of (previous) word
~

B

Same as b, but ignore punctuation
~

e

Move to end of (next) word
~

E

Same as e, but ignore punctuation
~

w

Move to start of next word
~

W

Same as w, but ignore punctuation

Table 5.6. Vim actions

CommandAction
cChange (delete + insert mode)
dDelete
yYank (copy)

Table 5.7. Special text manipulation commands in Vim

CategoryCommandAction
On a Single Line---
~dd or DDelete current line
~cc or CChange current line
~yyYank (copy) current line
On a Single Character---
~

x

Delete current character
~

r <char>

Replace current character with <char>
~

~

Change case of current character
~

%

Find matching parenthesis, bracket, or brace

Table 5.8. Searching and substituting in Vim

CategoryCommandAction
Searching Text---
~

/

Search forward
~

n

Repeat search forwards
~

?

Search backward
~

N

Repeat search backwards
Substituting Text---
~

:s/old/new

Substitute new for old for the first occurrence of old on the current line
~

:s/old/new/g

Substitute new for old throughout the current line
~

:<line1>,<line2>s/old/new/g

Substitute new for old between lines numbered <line1> and <line2>
~

:%s/old/new/g

Substitute new for old throughout the entire buffer
~

:%s/old/new/gc

Substitute new for old throughout the entire buffer, with a prompt for each substitution

Table 5.9. Ways to enter insert mode in Vim

CommandMethod of Entering Insert Mode
iInsert text just before cursor
aInsert text just after cursor
IInsert text at start of current line
AAppend text to end of current line
OOpen a line just above current line
oOpen a line just below current line

Table 5.10. Other modes in Vim

CommandMode
RReplace mode
vVisual mode
VVisual line mode
C-vVisual block mode



[37] Arnold Robbins, Elbert Hannah, and Linda Lamb, Learning the vi and Vim Editors, 7th ed (Sebastopol: O'Reilly, 2008), Chapter 1.

[38] Matthias Kalle Dalheimer and Matt Welsh, Running Linux, 5th ed., Section 19.1: “Editing Files Using vi.

[39] An open-source version of the original vi is now available on SourceForge. Details can be found in Section A.4.1, “More on vi and Vim”.


Back to Guide main page