17.4. Unix Editors

Typically, there are often several different editing programs installed on Unix systems to create and edit text files. Text files are commonly used for program source code, program data, and documentation. Unix does not interpret file suffixes to mean anything about a file’s content. However, users typically use suffix naming conventions for many of their files. For example, a .c suffix is the naming convention for C code source files (e.g., myprog.c), and a .h suffix is the naming convention for C header files (e.g., stdio.h).

While it is not important to learn every editor program, users should learn at least one to create and edit files on Unix systems.

Vim (vi improved) and Emacs are two examples of editors that are available on most systems, and vim (or vi) is always available on every Unix system. These editors have a text-based interface rather than a GUI-based interface (although both have GUI-based versions as well). In general, text-based editors require a bit more effort to learn to use than GUI-based editors that use menus and point-and-click interfaces. However, text-based editors typically use fewer computer resources to run, and they are particularly useful when remotely connected to systems on which you want to edit files.

There are many online resources for learning both Vim and Emacs. It is not important to learn both, and there are some strong opinions about which of Emacs and Vim is best (https://en.wikipedia.org/wiki/Editor_war). However, because Vim (or vi) is available on every Unix system, we present a bit more details on the basics of the vim editor and how to learn using Vim (and vi). We also include some information about the basics of Emacs and Nano, including further references for all of three.

17.4.1. The Vim Editor

The vim (and vi) editor is the most widely available editor on Unix systems. It is an efficient, lightweight, and powerful text editor that is easy to use after learning a few basic commands, which you can learn by running through the vimtutor tutorial. vim is particularly useful when working remotely over an ssh connection. It also has many advanced features and is very configurable through settings in a .vimrc config file.

It is not necessary to learn all of vim’s features; a few basic commands will get you started with vim and allow you to perform most editing tasks.

Vim operates in two modes:

  1. insert mode: keystrokes are interpreted as insertions into the file contents at the point of the cursor.

  2. command or escape mode: keystrokes are interpreted as vim commands that allow a user to do such things as saving, exiting, searching, or move the cursor’s location in the file.

To switch from insert mode to command mode, press the ESC key.

There are many ways to switch from command mode to insert mode. One way is to press the i key. One of the best ways to learn i`vim` is with vimtutor, the Vim tutorial.


The best way to learn the vim editor is to run vimtutor:

$  vimtutor

To get started in vim, we recommend that you focus on the vimtutor sections listed below (the other sections cover more obscure features that are not necessary). It takes about 20 minutes to run through all of these lessons.

  • All of Lesson 1 (moving around, x, i, A, :wq)

  • Lesson 2.6 (dd)

  • Lesson 2.7 (undo)

  • Lesson 3.1 (p) and 3.2 (r)

  • Lesson 4.1 (G) and 4.2 (searching)

  • Lesson 6.2 (a), 6.3 ( R ), and 6.4 (y and p)

Vim Command Reference

Here is a list of common vim command. You may find it helpful to refer back to this as you are learning vim.

  • Switching between modes


    switch to insert mode: what you type shows up in file at the cursor


    switch command mode, what you type is interpreted as vim commands

  • Commands for moving around:


    move cursor up one line (can also use arrow keys)


    move cursor down one line


    move cursor right one char


    move cursor left one char


    move cursor up one page


    move cursor down one page


    move cursor to end of file


    move cursor to start of line number <num> (e.g., :1 moves cursor to start of file

  • Commands for saving and quitting:


    save changes (write) to file


    quit vim


    quit without saving changes

  • Commands for switching to insert mode:


    insert: what you type shows at the cursor


    append: what you type shows up after the cursor


    open: starts a new line in file below the cursor


    open: starts a new line in file above the cursor

  • Commands to copy, paste, delete:


    copy current line (line cursor is on)


    copy <num> lines starting with the one the cursor is on (e.g., 8yy copies 8 lines)


    paste what was copied after line cursor is on


    paste what was copied before line cursor is on


    remove the char at the cursor


    remove <num> chars starting with one at cursor (e.g., 4x remove 4 chars)


    delete the current line (p will paste it back)


    delete <num> lines starting with one at cursor


    undo action, can undo multiple times


    redo undone action, can redo as many times as undo’ed

  • Commands to Search, Replace


    search for <pattern> in file, finds first occurrence from cursor (e.g., /hello search for hello in file)


    find next occurrence of search pattern moving forward


    find next occurrence of search pattern moving backward


    replace NEXT occurrence of <pattern> with <replacement>


    /g: replace ALL occurrences of <pattern> with <replacement>

  • Visual Selection Commands:


    visually select line (use arrow keys to select more lines)


    reformat visually selected text (auto-indent C code!)


    reformat visually selected text to 80 char max line length

  • Misc. Commands:

    :set number

    show line numbers

    :set nonumber

    don’t show line numbers


    join line under cursor with line on cursor

    :help <cmd>

    help documentation about a specific vim command, :q to quit help documentation (e.g., help v_) help info visual selection. Use <tab> (: help v_<tab>) to cycle through different visual commands' help

Configuring vim

Optionally, you can configure vim in all kinds of ways. While you’re not required to configure vim to use it, you may find it helpful to customize the color scheme or adjust some other settings.

In your home directory add a configuration file for vim, named .vimrc. On start-up, vim examines this file to set different configuration options for the vim session, including setting a color scheme and default window size.

Run ls -a in your home directory to list all your dot files and to determine if a .vimrc file already exists:

$ cd
$ ls -a ~/       # ~/ is shorthand for /home/you/

If the .vimrc file exits, you can open it in vim and edit it like any other file:

$ vim  .vimrc

If not, you can create a .vimrc in your home directory and copy an existing example .vimrc file of a friends or that you find online into it. For example:

$ pwd
$ vim  .vimrc

There are many on-line resources information about configuring vim settings.

17.4.2. The Emacs Editor

Emacs is another editor that is almost always available on Unix systems. It is not necessary to learn and use both Vim and Emacs, but it is useful to know one, and perhaps to know just a few basics about the other (at least how to exit each one).

Unlike Vim, Emacs is a single-mode editor, meaning that it runs in a single insertion mode where commands, like saving the file, are issued through special key combinations with the CTRL, ALT, and SHIFT keys. The main trade-off between Vim and Emacs is that Emacs has more complicated key sequences than Vim for commands, but it doesn’t have the two modes that the user needs to switch between like Vim does. Emacs is like a swiss army knife in that it has a rich assortment of macros and commands that can be used for a wide assortment of tasks (including features that are not commonly used). For this reason, Emacs is typically not installed on memory-constrained systems as it usually requires more memory than an application like Vim.

To start emacs, just run from the command line on the file you want to edit. For example, to edit myprog.c in emacs:

$ emacs myprog.c

When the user types in regular keystrokes, the characters entered are added to the file after the cursor.

Emacs Command Reference

Here is a list of common Emacs commands. You may find it helpful to refer back to this as you are learning Emacs.

  • File Control (these involve multi-sequences): CTRL-x CTRL-s is hold down the CTRL key and type x followed by s

    CTRL-x CTRL-s

    save the file edits

    CTRL-x CTRL-c

    exit Emacs

  • Moving the cursor


    move the cursor forward one line


    move the cursor backward one line


    move the cursor back one character


    move the cursor forward one character

  • Deleting Content


    deletes a character


    deletes a line.

  • Copying Content


    copy a line.



  • Search


    search for a pattern

  • Misc. Commands:


    quit out of emacs command sequence (useful if you type in something wrong)


    close the current buffer window


    close all other buffers except the current one


    open buffer window below


    open buffer window to the right


    switch to another buffer


    enters meta mode (in Emacs documentation and in this table forward, it is listed as M-x)

    M-x goto-line n

    go to line n in the file

    M-x linum-mode

    turn on line numbering

    M-x ansi-term

    open a bash shell in the current buffer

Like Vim, Emacs has a rich set of commands that support complicated editing tasks. It is also configurable via a configuration file, ~/.emacsrc, in which you can customize functionality and the look of emacs.

17.4.3. The Nano Editor

Nano is another text editor that is useful for making smaller edits on files---it is easy to use, but lacks some of the features of Emacs and Vim that make them useful for larger, more complicated editing tasks. Nano is also a single-mode editor like Emacs that uses special key combinations for commands like moving the cursor or saving the file.

Nano is run similar to Vim and Emacs; to open a file for editing type nano and the file to edit at the command line:

$ nano myprog.c

When you run nano it lists some of its most common commands at the bottom. In its listing, M- usually means the ALT (meta) key plus another key, and C- is the CTRL key. In general, nano’s control sequences for commands are simpler than in emacs. For example, CTRL-x is used to exit nano.

17.4.4. Setting your default editor

Your default editor program is the one that is automatically opened by other programs that start an editing session for some of their functionality. For example, revision control software such as Git starts an editor for adding comments to a commit. You can see your default text editor on Linux systems by echo’ing your EDITOR and VISUAL environment variables:

$ echo $EDITOR
$ echo $VISUAL

To set your default editor, add or edit the definitions for these two environment variables to your .bashrc file, and then source ~/.bashrc to see the changes. Here is an example to set both to vim:

$ vim ~/.bashrc

  export EDITOR=vim
  export VISUAL=vim

$ source ~/.bashrc
$ echo $EDITOR
$ echo $VISUAL

In the dot files section we talk about environment variables and .bashrc in more detail.

17.4.5. References

There are many resources for learning Vim, Emacs, or Nano on the web, including several tutorial videos. Here are just a few links to references and tutorials: