Text Editing with VI and Emacs

Text Editing with VI and Emacs

Text Editing

There are too many text editors to choose from to document them all, so we document, here, two of the most popular: emacs and vi. A few other popular choices are: gedit and kate, however you need to be doing this locally or have X running on your system. VI and Emacs will always work through an ssh connection

Emacs (and XEmacs)
emacs is a self-documenting, real-time display editor. To use emacs to edit a file called <filename>, type emacs <filename><return>. To edit the buffer contents you should use the control characters given in the table below.

Char-Sequence Function Char-Seq Function
C-f Move forward one character C-b Move backward one character
Meta-f Move forward one word Meta-b Move backward one word
C-a Move to beginning of current line C-e Move to end of current line
Meta-a Move to beginning of sentence Meta-e Move to end of current sentence
C-n Move to next line C-p Move to previous line
C-v View next page Meta-v View previous page
Meta-[ Move to next paragraph Meta-] Move to previous paragraph
Meta-< Move to beginning of buffer Meta-> Move to end of buffer
C-@ Mark this place in the buffer C-u C-@ Move to previous mark
DEL Delete last character C-d Delete current character
Meta-d Delete word Meta-z x Zap to char x in text
Meta-Del Delete backward one word C-Del Backward delete hacking tabs
C-k Kill rest of line Meta-k Kill sentence
C-y Yank out what is in the buffer C-w Wipe out from marked place to here
C-y Yank previous kill C-x u Undo the most recent change
Meta-h Mark paragraph Meta-q Fill paragraph
Meta-t Transpose two words Meta-Space Just one space
C-o Open line Meta-x center Center line/region/paragraph
C-i Indent according to mode C-j Terminate paragraph
C-s Search forward (C-g or Meta to end) C-r Reverse search (C-g or Meta to end)
Meta-c Capitalize initial letter of word C-l Clear screen, redisplay buffer
Meta-u Capitalize whole word Meta-l Downcase whole word
C-h Emacs help system C-g Quit/Stop whatever emacs is doing
C-x i Insert a file C-x C-c Exit from emacs
C-x C-f Get (Find) file C-x a Append to buffer
C-x C-v Visit a file C-x C-s Save the buffer
C-x 2 Split window horizontally C-x 5 Split window vertially
C-x o Move to another window C-x 0 Kill the current window
Meta-x Named command (help for help) Meta-w Copy from marked place
Meta-! Start up a new subshell Meta-x spell Check and correct spelling
C-x b Switch buffers C-x k Kill the current buffer

Vi

vi is a screen oriented (visual) editor. To invoke vi to edit a file called <filename>, type vi <filename>. There are two basic (there are others, e.g. replace) modes in vi, the command mode and the input mode. In command mode the characters you type are interpreted as commands. In input mode the characters you type are taken as text, except the DEL key which lets you delete previous characters and the ESC key which lets you go back to the command mode.

Char-Seq Function Char-Seq Function
i (I) Insert before the cursor (line) a (A) Append after the cursor (line)
o (O) Open a new line below (above) J Join next line to current line
l (h) Move right (left) one character k (j) Move up (down) one line
w (b) forward (backward) word c/E go to end of the word
0 ($) move to the beginning (end) of line ^ to first visible char. of line
fx Find char. x in the line tx Go to char. x in line
( [)] To previous [next] sentence { [}] To previous [next] paragraph
nG (Go) to line n G to last line in buffer
H (L) to top (bottom) of screen M to middle of screen
C-f/C-F Forward screen C-b/C-B Backward screen
C-d/C-D Down half screen C-u/C-U Up half screen
z<return> Put line on top of screen z. Put line on middle of screen
`<a> Move to mark <a> % Move to matching bracket symbol
m<a> Mark this place as <a> C-g Show status
r Replace one character R Replace mode
cw Replace (Chage) a word dw Delete one word
dd Delete one line D Delete to the end of line
u Undo the most recent change U Undo all changes on the current line
nyy (ynw) Copy n lines (n words) into buffer p Paste the buffer
/<text> Search forward for <text> n Repeat previous search
:r! <cmd> Take <cmd>'s output as input text . Repeat previous change
:r! <cmd> Invoke system command <cmd> :sh Invoke shell
:r <file> Insert <file> :w <file> Write buffer to <file>
:c <file> Edit another file named <cmd> :q Quit from vi
!}fmt Fill the following paragraph :q! Quit without saving
:set <arg> Set parameter <arg> :set all Set all the parameters
:1,5s/<string1>/<string2>/ Replace <string1> with <string2> for 1st occurence on each line from lines 1 to 5 :1,$s/<string1>/<string2>/g Replace <string1> with <string2> for all occurences within the file

An easy way to move text around (in Vi and Emacs) is to delete it (by dd, dw, etc.), move the cursor, then put it back (by p). To copy text, you can use yy and p. Finally, you can give a count in front of each command to indicate the number of repetitions.