Tutorials - Editing Files: nano, vim and emacs

Editing Files: nano, vim and emacs

There are three main text editors available on Linux systems. The first is nano, a very easy to use clone from the Pine email client that uses control keys with a the equivalent of a "shortcut bar". The hefty Emacs (Editor Macros) editor and environment is a feature-rich program that was first written in 1976 and is now up to version 22.1! Also deriving initially from 1976 is vim (Vi improved) which is a series of enhancements build on the "screen orientated" text editor vi. Vim is generally understood to be a modal editor, operating either in a insert mode (where typed text becomes part of the document) or command mode (where keystrokes are interpreted as commands that control the edit session). Vi or Vim are usually installed as the default text editor.

In "UNIX culture" Emacs and Vim are considered favourites among experienced users, with nano considered the best for beginners. There are also long-running, and largely tongue-in-cheek, "editor wars" with various proponents debating the relative merits of different editors.


With nano editing is very intuitive. Start with nano on the command prompt. One can type straight to the display and editing is a simple function of simultaneously using Cntrl and a keystroke. The most commonly used key combinations are available on the bottom of the screen, including cutting (^K) and pasting ("uncutting", ^U) lines of text, searching ("where is", ^W), opening ("read a file", ^R), saving files ("write out", ^O), scrolling up and down the text (^Y, ^V). Further commands can be displayed through invoking help ("get help", ^G) such as search and replace (M-R, ie., meta key, usually Esc and 'R').


As mentioned previously, vim is a modal text editor. When one starts vim (vim [filename] at the command prompt, the editor starts in command mode. Typing 'i' enters the insert mode, which allows text to be added and edited in the document. Pressing the Esc key switches vim back to the command mode, with Esc and 'i' back into insert again. In general in vim there are two types of commands; cursor movements and text modification.

Movement Commands:

Relative movement. Relative movement can be prefixed with a count, to tell vi to repeat the motion.

h j k l Move the cursor to the left, down, up, right, respectively. These also take a count: '8l' moves the cursor 8 letters to the right.
Return + - Move the cursor down (Return and '+') or up ('-').
w W b B Move forward ('w' and 'W') or back ('b' and 'B') by a word. 'w' and 'b' treat anything non-alphanumeric as a word delimiter; 'W' and 'B' honour only white space.
} { Move to end of current or previous paragraph, respectively.
) ( Move to end of current or previous sentence, respectively.

Absolute movement

G Go to (a specified line). E.g., "10G" moves the cursor to line 10. Without a count, vi goes to the last line of the buffer.
^ Move to first nonblank character on the current line.
$ Move to end of current line.
0 Move to beginning of current line.


Many text modification commands are known as operators. They can be prefixed with a count (to repeat the operator), and are suffixed with a motion command. The text between the current position and the final position (after the motion) is called a region.

These are examples of operators:

d delete a region. 'd10G' deletes lines from current line until line 10 (inclusive).
c change a region. 'cwnew'[Esc] - changes current word with the text 'new'.
indent a region. '<}' indents left all the lines from here until end of paragraph. '>/xxx' indents right lines from here until the line containing the pattern 'xxx'.

In some instances, if the motion command repeats the operator character (as in 'dd'), the region is a whole line (e.g., 'dd' deletes the current line. Prefixing it with a count repeats (or makes it apply to more than one), e.g., '10dd' deletes 10 lines.

Other Commands:

:/pattern Search for pattern and place the cursor at the start of the matched text. :%/s/r/g Global search for 's' and replace with 'r'. :?pattern Search in the reverse direction. % Move the cursor to the matching brace or bracket: {} [] (). . Repeat the last command which has modified the file. !program Filter a region through an external program. " :w filename Writes (saves) the current document. If the optional filename is provided, the text is saved in the specified file. :q Quits the editor. :e filename Loads the specified file into the editor. :n Loads the next file into the editor. Useful when vi was invoked from the command line with multiple file names. :r filename Reads (inserts) the content of the specified file into the current document. :set number Turns on line numbers.

These commands can take a line number or a range of line numbers, e.g., "20,35w fishdata.txt" will write lines 20 through 35 (inclusive) into the file fishdata.txt



With Emacs, in the normal editing mode, the character keys insert the appropriate characters, the arrow keys move the editing point, backspace deletes text etc. However, other commands can be invoked with modified keystrokes, pressing the control key and/or the meta key (usually Esc)/alt key in conjunction with a regular key. Some commands require multiple modified keystrokes. For example, C-x C-c means while holding down the control key, press x; then, while still holding down the control key, press c.

Emacs keeps text in objects called buffers. The user can create new buffers and dismiss unwanted ones, and several buffers can exist at the same time. Most buffers contain text loaded from text files, which the user can edit and save back to disk. Buffers are also used to store temporary text, such as the documentation strings displayed by the help library. Emacs is also able to split the editing area into separate sections known as "windows".

The minibuffer, normally the bottommost line, is where Emacs requests information. Text to target in a search, the name of a file to read or save and similar information is entered in the minibuffer. When applicable, tab completion is usually available.

Emacs largely runs through an implementation of the Lisp programming language called Emacs Lisp. As a result, a Emacs comes with a large number of libraries along with third party additions. Thus, although listed here as a text-editor, Emacs can also act as a calculator, a calender, a mail client, a personal information planner, an IRC client, a USENET news reader, a web browser and even text-adventure game and more.


More information can be found at the homepages of the three text editors mentioned.


Handy vim refences card for vim and emacs can be found here:


Top of Page