Chapter 3 Text Editors

Table of Contents
3.1 VIM
3.2 Emacs
3.3 Other Editors

Text editors are specialized programs which allow you to create or edit a file by typing in the contents using the keyboard (and sometimes the mouse). Most text editors have many special features which allow text to be created more easily and quickly using functions such as copying, deleting, and moving blocks of text in the file. There are numerous text editors available on the lab machines--of which two of the most frequently used are described in the following sections.

3.1 VIM

The VIM text editor on the PCs is an improved version of the editor "vi", one of the standard text editors on UNIX systems. VIM has many of the features that you should expect a programmer's editor to have: Unlimited undo, syntax coloring, split windows, visual selection, a graphical user interface (with menus, mouse control, scrollbars, text selection, and the like), and much more. To edit a file using the GUI version of VIM, type:

gvim [file...]

3.1.1 Entering Commands and Text

VIM is a mode-oriented editor. Initially, when VIM starts, it is in command mode. In command mode, every key typed is interpreted as a command, such as a command to delete a character or to move the cursor.

From command mode, pressing i (or a, or another insert command) puts VIM into insert mode. In insert mode, each character typed is inserted into the file. Pressing the backspace key deletes the previous character. From insert mode, pressing the ESC key puts VIM back into command mode. It's also possible to use the cursor keys to move the cursor around in insert mode. If you do not know which mode VIM is in, simply press the ESC key to make sure VIM enters the command mode. To exit VIM (and save the current file), type :wq.

3.1.2 Summary of Commands

Following is a summary of the commands VIM accepts while in command mode.

3.1.2.1 Moving Around the File

Pressing one of the arrow keys moves the cursor one position in the direction of the arrow. Pressing the PgUp key moves backward in the file by a full screen; pressing the PgDn key moves forward in the file by a full screen.

h -- Move cursor LEFT
j -- Move cursor DOWN
k -- Move cursor UP
l -- Move cursor RIGHT
   
b -- Move cursor back one word
w -- Move cursor forward one word
0 -- Move cursor to beginning of current line
$ -- Move cursor to end of current line
Enter -- Move cursor to beginning of next line
^ -- Move cursor to beginning of current line
H -- Move cursor to top line of screen
M -- Move cursor to middle line of screen
L -- Move cursor to last line of screen
   
CTRL-d -- Move down (forward) half a screen
CTRL-u -- Move up (backward) half a screen
CTRL-f -- Move forward a full screen
CTRL-b -- Move backward a full screen
{ -- Move to previous paragraph
} -- Move to next paragraph
1G -- Move to beginning of file
G -- Move to end of file
xxG -- Go to the xxth line of the file

3.1.2.2 Inserting Text

i -- Enter insert mode to insert text before cursor
I -- Enter insert mode to insert text at beginning of line
a -- Enter insert mode to append text after the cursor
A -- Enter insert mode to append text at end of line
o -- Open a line below the current line, and enter insert mode
O -- Open a line above the current line, and enter insert mode

3.1.2.3 Changing Text

J -- Join next line to current line
r -- Replace one character with a single character
R -- Replace string of text (overstrike)
x -- Delete one character
dw -- Delete one word forward
db -- Delete one word backward
dd -- Delete current line
12dd -- Delete 12 lines
D -- Delete from cursor to end of line

3.1.2.4 Copying Text

yy -- Yank the current line to a buffer (does not delete the line)
12yy -- Yank 12 lines (including current line) to the buffer
p -- Put lines from buffer below current line
P -- Put lines from buffer above current line
:r filename -- Read lines from file filename and insert at cursor position

3.1.2.5 Searching for (and Replacing) Text

/ABC -- Search for next occurrence of "ABC"
?ABC -- Search for previous occurrence of "ABC"
n -- Repeat last search
:%s/regexp/ABC/g -- Replace all text matching the regular expression regexp with ABC

3.1.2.6 Undoing (and Redoing) Commands

u -- Undo last command
CTRL-r -- Redo last command

3.1.2.7 Saving Files and Exiting VIM

:w -- Save the current file without leaving (do this periodically)
:wq -- Replace old file and exit VIM
ZZ -- Shorthand for :wq
:q -- Exit VIM. If the file has been modified and not subsequently saved, VIM will prompt to save the file.
:q! -- Exit VIM without replacing the old file