CSC128: The vi editor
CSC128, : Introduction to UNIX
Text Editor: vi
There are many programs to edit a text file. Some are:
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.
- vim (vi Improved)
- vi (vim elvis ni)
- emacs (xemacs uemacs)
- and many, many others.
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.
Start vi by typing:
To exit vi type:
To exit vi without saving changes, type:
To save your changes without exiting vi , type:
To quickly exit vi and save your changes, type:
(esc)ZZ (capital letters), that's escape-shift-zz
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)
move to the last line of the file
move to the first line of the file
move one character left (arrow keys work too)
move one line down(arrow keys work too)
move one line up(arrow keys work too)
move one character right(arrow keys work too)
move forward one screen
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.
vimtutor which is a good interactive
learning tool for vi
switches to insert mode
writes the text file to filename
reads text from filename
deletes the current character
deletes the current line
joins two lines
replaces old with new
replaces all occurances on the line of old with new
replaces all occurances everywhere of old with new
undo last command