The MSRI Computing Handbook

Editors



Easy Editing: NEdit

Don't have time or interest to learn a Unix editor? ...then nedit is for you.

nedit belongs to the family of WYSIWYG (What You See Is What You Get) editors. Wysiwyg editors tend to be mouse-based and are easy to transition to if you have a stronger history with Macintosh or Windows editors. You can add text wherever the cursor is positioned and a swift hit of the delete key will remove any text that is highlighted. nedit offers extensive customization, even to the point of establishing automatic backups. There is a low- to no-learning curve with nedit. You can literally open a file and start working immediately with this editor.

The Essentials of Emacs

Emacs (pronounced ``ee-max'') is the most powerful of the native Unix editors. Before the idea of Open Source software, most Unix users ended up having to learn either vi or emacs. Neither program is particularly user-friendly, but emacs is certainly more broad in its usefulness. For example, emacs supports mouse based editing, multiple windows, and understands the syntax of several high-level languages, like TeX. Moreover, emacs has extensive online documentation, including a tutorial which should make this section superfluous; when in emacs just type h while holding down the control key. If you take some time to learn the program, you will find it more useful than vi in the long run.

Starting the Emacs Program

To start emacs, you can simply type the command name emacs. This opens a separate window that looks much like the sample Emacs window shown below. If you want to open an existing file, or even an empty file, just include the file name on the command line:
     emacs file1.txt
The name of the file is arbitrary.

If you are working remotely, the spawning of an external window may be problematic. If this is the case, you can startup emacs without generating an external window using:

     emacs -nw file1.txt

The Emacs Screen



At the top is the menu bar. Note especially the Help menu on the left. On the right is the Buffers menu. Buffers are basically things that you can edit, which need not even be files, but could be complex editing commands, or directories, for example. The joke is that emacs is an editor that wishes it were an operating system. That menu also gives you access to the list of Frames, which is what emacs calls windows. The Tools menu lists various tools for manipulating your file in high-powered ways.

Warning Unless you know what you are doing, avoid the Read Mail menu item in the Tools menu. This will allow you to read your email, but will save your mail in a file format indecipherable by any other known mail reader. Once in, this format, tedious human intervention is required to return your email to a more conventional format.

If you are editing a file in a complex language, for example TeX, there will also be menus specific to that language. Do explore this with a TeX file. You will be surprised at how well emacs becomes an integrated environment for previewing and printing, as well as editing, TeX.

Across the bottom of the screen is a black bar - the status bar. On the left side of the status bar is the name of the file you are editing. Next is the an indication of what sort of file emacs thinks you are editing (see the discussion of major and minor modes in the emacs online documentation for more on this. Emacs understands TeX, HTML, C, and a host of other languages besides plain text.)

Above the status bar is the edit pad - it contains the text of the file you are creating/editing.

Keyboard commands, the cornerstone of emacs' power, will appear in the command buffer, located below the status bar. This is one of the most important regions of the emacs window.

Entering Text

Entering text is easy---just type!

Basic Emacs Commands

Many emacs commands use the mouse to select and move regions of text, extending the basic X-windows facilities. For these it is hard to beat the explanation in the online tutorial.

All other emacs commands are entered via Control and Meta keys. For example, the command to save a file is Control-x Control-s (also written as C-x C-s). While holding down the Control key with your left hand, tap the x key (after a second or two, you will see C-x under the status bar). Then, while holding down the Control key with your left hand, tap the s key. While most keyboards have a Control key, most don't have an explicitly labeled Meta key. Often the Meta key is is the Alt key, which can be held down, but hitting the escape (labeled Esc) key briefly will always work. For example, to type Meta-x (also written M-x), you should either hold down the Alt key and tap the x key, or, if this doesn't work, tap the Esc key and then the x key.

Getting Help:

C-h
Enter the emacs Help system
C-h t
Enter the tutorial
C-h i
Enter the GNU info documentation system
C-h a
Apropos - find out about commands
Moving the Cursor:
C-b -or- / C-f -or-
Move one character backward / forward
M-b / M-f
Move one word backward / forward
C-p -or- / C-n -or-
Move one line backward / forward
M-a / M-e
Move one sentence backward / forward
C-v / M-v
Move one screen backward / forward
M-> / M-<
Move to the end / beginning of the file
Deleting Text:
DEL / C-d
Delete the previous / next character
M-DEL / M-d
Delete the previous / next word
Manipulating Files:
C-x C-s
Save this file
C-x C-f
Close this file and open another file instead
C-x i
Insert another file into this one
Quit emacs:
C-x C-c
Quit emacs permanently
C-z
Suspend emacs temporarily (type fg to resume
Search and Replace:
C-s
Incremental search forward
C-r
Incremental search backward
Esc
Exit search
M-%
Query search and replace
Miscellaneous Commands:
C-_
Undo (That's an underscore, i.e shift -)
C-g
Abort current command

Reference Material

Copies of both the emacs manual and the O'Reilly Learning GNU Emacs book are available for checkout in the library.





The Essentials of vi

vi (pronounced vee-eye - short for the word visual) - was the first Unix full-screen editor. Most UNIX users end up having to learn either vi or emacs. Neither program is particularly user-friendly, but vi is handy for quick editing sessions. Here is a brief lesson:

Starting the vi Program

Suppose you wanted to edit a file called file1.txt. You would type:
   vi file1.txt

Sometimes, you may find yourself looking at a window which looks like:

in which case, you have entered vi by mistake. Don't despair, all is not lost. Read on to find out how to quit, or even proceed productively.

Fundamentals

There are two mutually exclusive modes in vi:
Command mode
here you give the text editing commands
Input mode
here you you type your text

Command Mode

There are several groups of commands, listed below. Most commands will take a numerical prefix; for example, 7w means ``jump ahead seven words'', and 3dd means ``delete the next three lines''. Note that you cannot enter any text until you switch to input mode. (This is not an exhaustive list of commands.)

Moving the Cursor:

h -or-
Move one character to the left
l -or-
(small ``ell'') Move one character to the right
j -or-
Move up one line
k -or-
Move down one line
w
Jump to the next word
b
Jump to the previous word
nG
Jump to line number n
$
Jump to the very end of the line
^
Jump to the very beginning of the line
G
Jump to the very end of the document
Editing Text:
x
Delete this character
dw
Delete this word
dd
Delete this line
D
Delete the remainder of this line
r char
Replace this character with the character char
cw word
Replace this word with the word word
Switch to Input Mode:
i
Input text in front of this character
a
Append text after this character
I
Input text in front of this line
A
Append text after this line
o
(small ``oh'') Open a new line underneath the current line
O
(capital ``oh'') Open a new line above the current line
R text
Replace the current text with text (i.e., obliterate current text)
Yank and put: (More commonly, but less mnemonically, known as Cut and Paste)
Nyy
Yank (copy) N lines into a buffer
Ndd
Cut (remove) N lines into a buffer
p
Put the text from the buffer after this line
P
Put the text from the buffer before this line
Examples of Search & Replace:
:3,5s/foo/bar/g
Search lines 3-5 for all occurrences of foo and change them to bar. Without the trailing g this would only replace the first occurrence of foo on each line.
:1,$s/foo/bar/g
The same, but search through the whole document.
:3,5s/$/xyz/
Add the text xyz to the end of lines 3-5
:1,$s/^/xyz/
Add the text xyz to the beginning of every line in the document
Save & Quit:
:w
Save the current document
:wq
Save the current document, then quit vi
:q
Quit vi
:q!
Quit vi (without saving changes), even though you have changes to save
:e foo
Close the current document, and edit the file foo instead
Handy miscellaneous commands:
u
Undo the last action
.
Repeat the last command action
/text
Jump ahead to the next occurrence of the string text in your document
?text
Jump back to the previous occurrence of the string text in your document
n
Repeat the last / or ? search

Input Mode

Once you are in input mode, type your text. Make sure you hit the Return key (called Enter on some keyboards) at the end of every line.

To switch back to command mode, press: Esc (escape).

Warning One of the most common errors in vi is to type a command while still in input mode. If you do this, the command will show up in the text! Remember, to type a command you must first return to command mode by pressing the Esc key.

Reference Material

Copies of the O'Reilly Learning the vi editor book are available for checkout in the library.



Last modified by Joe Christy on $Date: 1997/09/09 14:09:27 $