CSC128: The vi editor

CSC128, : Introduction to UNIX

Text Editor: vi


Text Editors

There are many programs to edit a text file. Some are:

  • vim (vi Improved)
  • vi (vim elvis ni)
  • emacs (xemacs uemacs)
  • pico
  • joe
  • xedit
  • kedit
  • gedit
  • and many, many others.
The text editor vi is the only one that is guaranteed to be on any POSIX-compliant UNIX system. We need to learn vi, because it is always there, while other editors may or may not be. In addition there are several utilities used by regular users and the root user such as vipw, vigr, crontab among others that use a vi interface thus requiring at least a basic understanding of vi.

A Short History of vi

Long, long ago, when UNIX users typed at teletypes (electric typewriters connected to computers), they used ed. ed allows the user to edit a text file, one line at a time.

Later, when terminals had become common, with screens instead of paper, it made more sense to edit a text file using the entire screen, rather than be restricted to the old way that assumed that everything was being typed on paper. Thus was born ex, an extension of ed. It had the same features as ed, but also had new ones, including a visual mode.

People grew so much more fond of the visual mode than the old line-editor mode, that they wanted it to start up that way. vi is ex that starts in visual mode right away. (You can still go back to ex mode by typing the command Q.)

vi is an editor that has modes, the escape key (esc) takes you to command mode, enter also takes you to command mode. Colon : takes you to what's called Last-Line mode and enter at last-line mode will return you to command mode. To insert text you push escape to ensure that you are in command mode and then press i for insert text.

Starting vi

Start vi by typing:

vi filename

or just:

vi

Exiting vi

To exit vi type:

(esc) :q

To exit vi without saving changes, type:

(esc):q!

To save your changes without exiting vi , type:

(esc):w

To quickly exit vi and save your changes, type:

(esc)ZZ (capital letters), that's escape-shift-zz

Modes

When vi is started, you are placed in command mode. In command mode, vi waits for key combinations (commands) to move the cursor around the text, etc.

To switch from command mode to input mode, use any of the commands to insert text, including i, which inserts text before the cursor position.

To switch back to command mode from input mode, press the ESC key.

Last line mode allows you to enter extended commands, such as search and replace, etc. To get to Last line mode from command mode, press : (colon). To return to command mode from Last line mode, press the Enter (or Return) key. Usually, Last line mode commands are represented with the : (colon) first, like the command to quit without saving, :q!.

Moving Around (We will rarely use the hjkl since the pageup/down keys now work)

G
move to the last line of the file

1G
move to the first line of the file

h
move one character left (arrow keys work too)

j
move one line down(arrow keys work too)

k
move one line up(arrow keys work too)

l
move one character right(arrow keys work too)

Ctrl-F
move forward one screen

Crtl-B
move backward one screen

0 -or ^
move to beginning of line

$
move to end of line

A Few Commands

Also read pp. 201-227. A good summary of vi commands can be found on pp. 237-242.

Also run vimtutor which is a good interactive learning tool for vi

i
switches to insert mode

:w filename
writes the text file to filename

:r filename
reads text from filename

x
deletes the current character

dd
deletes the current line

J
joins two lines

/
search

:1,$s/old/new
replaces old with new

:1,$s/old/new/g
replaces all occurances on the line of old with new

:%s/old/new/g
replaces all occurances everywhere of old with new

u
undo last command