Quick Guide to Editing in Unix

with Vi, Emacs, and PICO

Which Editor to Use?

Pico is the easiest editor to learn, emacs is the most powerful editor and has a built-in tutorial, and vi is present on essentially all Unix systems. These editors are all text-mode editors and do not require an X-server to be running on your PC.  

If you happen to have an X-server running, there are several editors which you can expect to find. These are: dtpad, textedit, and xedit.  Use of these editors is through pull-down menus and using your mouse and will not be discussed here.

Notation Abbreviations

Special Notes:

Unix IS case sensitive, and as such treats an uppercase H differently from a lower case h. All letters of the alphabet are treated the same - uppercase is not the same as lower case.

The first two sections of this guide have exactly the same commands in the same order to make comparisons between vi and emacs easier.

Your default editor depends upon when you got your account. It should be pico, but you can change it at will.  Email Chad for details.

Keystroke Indicators:

^ the carat symbol preceding a character means to press and hold the control key while taping the indicated key. Thus, ^x means press and hold the control key while tapping the 'x' key once (quickly).

<M> Used in emacs, the <META> symbol preceding a character indicates that you should tap the escape key (marked "Esc" on your keyboard), and then tap the next keystroke. This is two distinct and separate actions, rather than one connected action, as with the control key.

Used in Vi, the <M> key toggles one into the command mode. In Vi, it is used in isolation, rather than in conjuntion with other keystrokes.

a..z The a through z keys on the keyboard

A..Z The A through Z keys on the keyboard

How do I tell what Editor I'm in??

Vi     vi is distinguished by a line of tilde symbols (~) in column 1 below your entered text.

Emacs     emacs has a solid bar across the bottom of the screen which contains the word "emacs"

Pico     pico contains on-screen help in 2 rows across the bottom of the screen, and it says "PICO(tm)" in a solid bar along the top of the screen.

Vi -

This is the Original Unix Default Editor for e-mail and news articles

Vi is a modal editor; that is, when using vi, you are in an edit mode, or in a command mode. When in the command mode, all keystrokes are interpreted as commands. When in the edit mode, these same keystrokes are interpreted as characters to enter into the text. On mutant, vi provides no visual clue as to which mode you are in. In order to ensure that you are in the command mode, press the <M> key. If you are in doubt as to which mode you are in, press the <M> key a couple of times. This will ensure that you are in the command mode, and then you can go to the edit mode if you wish.

Vi Command Mode Commands:

Cursor Movement:

h     Cursor left
j     Cursor down
k     Cursor up
l     Cursor right
0     Cursor to beginning of line
$     Cursor to end of line
w     Cursor to next word
e     Cursor to end of current word
b     Cursor to beginning of current word
H     Cursor to top of screen
M     Cursor to middle of screen
L     Cursor to last line on screen

^F     Scroll forward 1 screen
^D     Scroll down 1/2 screen
^U     Scroll up 1/2 screen
^B     Scroll back 1 screen
:0     Move cursor to top of file
:$     Move cursor to last line of file

Deletion Commands

x         Delete character under cursor
dw     Delete word under cursor
dd     Delete current line
D     Delete from cursor position to end of line

Editing Commands

a     Append text after cursor
A     Append at end of line
i     Insert text at current cursor position
I     Insert at beginning of line
r     Replace character under cursor
o     Open a new line above current line and enter edit mode

While editing, the backspace key works as a destructive backspace (delete). Backspacing over characters automatically deletes them, and they must be retyped.

Exiting from Vi

The Vi write commands, listed below, always write over the file if it already existed. They do not make any backups.

:wq Write file (save), then quit
ZZ Write file (save), then quit :x Write file (save), then quit
:q! Quit without saving

Saving your File

:w Write/save current file

Getting a new file

:n filename Read in the file called filename

Extra Commands

ncommand A number n followed by a command will repeat the command n times. For example 9dd will delete the current line and the 8 subsequent lines.

Emacs - this editor is available and preferred

The emacs editor is always in the edit mode, in a similar manner to the normal PC word processors like Word Perfect, Ami Pro, and Microsoft Word. Control of other functions is always accomplished by pressing either the control key along with another key, or by preceding another keystroke with the escape key (symbolized in this handout by <M>). Emacs has an on-line tutorial, which can be accessed by pressing ^H^H^Ht (control H, 3 times, followed by a lower case t). Note that each time one presses ^H, there is help information appearing on the bottom line of the screen, and that that reminder may be sufficient for the current situation.

Note that some of the Vi commands do not exist in emacs. These commands have no command keystrokes preceding the command description. For example, there is no command to move the cursor to the end of the current word.

Emacs Commands

Cursor Movement:

^b     Cursor left (Backward)
^n     Cursor down (Next line)
^p     Cursor up (Previous line)
^f     Cursor right (Forward)
^a     Cursor to beginning of line
^e     Cursor to end of line (End of line)
 
<M>f Cursor forward to next word
<M>b Cursor to beginning of current word

There is no emacs command equivlaent to these next 3 commands, but the same effect can be had using a numeric prefix for the number of lines up or down you which to move the cursor. See the Extra Commands section for a more complete explanation.

Cursor to top of screen
Cursor to middle of screen
Cursor to last line on screen

^v         Scroll forward 1 screen
            Scroll down 1/2 screen (Control-D in vi - no emacs equivalent)
            Scroll up 1/2 screen (Control-U in vi - no emacs equivalent)
<M>v     Scroll back 1 screen

^l Repaints the screen and centers it around the line containing the cursor.

<M><     Move cursor to top line of file
<M>>     Move cursor to last line of file

Deletion Commands

^d Delete character under cursor
<M>d Delete word under cursor
^k Delete from cursor position to end of line
^k^k Delete current line of characters and the remaining blank line

Editing Commands

Note that the next group of commands is not needed in emacs, since one is always in the edit mode.

Append text after cursor
Append at end of line
Insert text at current cursor position
Insert at beginning of line
Replace character under cursor

^o Open a new line above current line.

Exiting from Emacs

^x^s     Save the current buffer
<M>x     save-buffer Alternative method if the ^S key is blocked by your terminal emulator.
^x^c     Exit from emacs. If you have changed the buffer, you will be asked if you want to save the changes. If you do not want to save the changes, you will also need to type yes in response to the are-you-sure prompt.

Saving Files

^x^s Save the current buffer

<M>x save-buffer     Alternative method if the ^s key is blocked by your terminal emulator (see note under Extra Commands, for a more complete explantion).

Getting a new file

^x^f     Read a file. You will be prompted for the filename.

Extra Commands

<M>nn Where nn stands for some number, <M>nn causes the next command or keystroke to be repeated nn times. Thus, <M>9 ^p causes the cursor to go up 9 lines, and <M>9 a causes 9 'a's to be put into the text.

<M>x This causes emacs to provide you with a command line from which other commands can be entered as words. Thus <M>x save-buffer causes the current buffer to be saved. This is the same as ^x^s, but is useful when connected through a modem or through a program such as CrossTalk (Synapse) which does not allow a ^s to be transmitted to the computer at the other end of the connection.

<M>x apropos provides help on any topic in the help index. It will prompt you for a single word topic, and attempt to find anything relevant to that topic. It may present you with a list of multiple topics from which you will have to decide which one is what you are looking for.

<space> On the <M>x command line, if you have typed the beginning of a command, and press the space bar, emacs will attempt to figure out the command you were trying to enter, and will fill in the appropriate text until it finds an ambiguity, at which point it will show you a list of possibilities.

PICO -

Pico is a simple editor with on-screen help prompting and limited capability

Many of the PICO cursor movement commands are the same as the Emacs commands for the same purpose. Those commands marked with a ! are the same as the equivalent emacs command.

These commands are displayed on the Pico editing screen at all times.

^G    Get Help
^O     WriteOut
^R    Read File
^Y     Prev Pg
^K     Cut Text
^C    Cur Pos
^X     Exit
^J    Justify
^W    Where is
^V     Next Pg
^U    UnCut Text
^T     To Spell

This is a list of the other Pico commands available, and is displayed using the ^G help facility.

^F     move Forward a character. !
^B     move Backward a character.
!
^P     move to the Previous line.
!
^N     move to the Next line.
!
^A     move to the beginning of the current line.
!
^E     move to the End of the current line.
!
^V (F8)     move forward a page of text.
!
^Y (F7)     move backward a page of text.
^W (F6)     Search for (where is) text, neglecting case.
^L     Refresh the display.
!
^D    Delete the character at the cursor position. !
^^     Mark cursor position as beginning of selected text. Note: Setting mark when already set unselects text.
^K (F9)     Cut selected text (displayed in inverse characters).

Note: The selected text's boundary on the cursor side ends at the left edge of the cursor. So, with selected text to the left of the cursor, the character under the cursor is not selected.


^U (F10)    Uncut (paste) last cut text inserting it at the current cursor position.
^I    Insert a tab at the current cursor position.
^J (F4)     Format (justify) the current paragraph. Note: paragraphs delimited by blank lines or indentation.
^T (F12)     To invoke the spelling checker
^C (F11)     Report current cursor position
^R (F5)     Insert (Read) an external file at the current cursor position.
^O (F3)    Output the current buffer to a file, saving it.
^X (F2)     Exit pico, saving buffer.