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.

vimtutor

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

    i

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

    ESC

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

  • Commands for moving around:

    k

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

    j

    move cursor down one line

    l

    move cursor right one char

    h

    move cursor left one char

    CTRL-u

    move cursor up one page

    CTRL-f

    move cursor down one page

    SHFT-g

    move cursor to end of file

    :<num>

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

  • Commands for saving and quitting:

    :w

    save changes (write) to file

    :q

    quit vim

    :q!

    quit without saving changes

  • Commands for switching to insert mode:

    i

    insert: what you type shows at the cursor

    a

    append: what you type shows up after the cursor

    o

    open: starts a new line in file below the cursor

    O

    open: starts a new line in file above the cursor

  • Commands to copy, paste, delete:

    yy

    copy current line (line cursor is on)

    <num>yy

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

    p

    paste what was copied after line cursor is on

    P

    paste what was copied before line cursor is on

    x

    remove the char at the cursor

    <num>x

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

    dd

    delete the current line (p will paste it back)

    <num>dd

    delete <num> lines starting with one at cursor

    :u

    undo action, can undo multiple times

    :redo

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

  • Commands to Search, Replace

    /<pattern>

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

    n

    find next occurrence of search pattern moving forward

    N

    find next occurrence of search pattern moving backward

    :s/<pattern>/<replacement>

    replace NEXT occurrence of <pattern> with <replacement>

    :s/<pattern>/<replacement>/g

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

  • Visual Selection Commands:

    SHIFT-v

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

    =

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

    gq

    reformat visually selected text to 80 char max line length

  • Misc. Commands:

    :set number

    show line numbers

    :set nonumber

    don’t show line numbers

    J

    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
/home/sarita/
$ 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

    CTRL-n

    move the cursor forward one line

    CTRL-p

    move the cursor backward one line

    CTRL-b

    move the cursor back one character

    CTRL-f

    move the cursor forward one character

  • Deleting Content

    CTRL-d

    deletes a character

    CTRL-k

    deletes a line.

  • Copying Content

    CTRL-k

    copy a line.

    CTRL-y

    past

  • Search

    CTRL-s

    search for a pattern

  • Misc. Commands:

    CTRL-g

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

    CTRL-x-0

    close the current buffer window

    CTRL-x-1

    close all other buffers except the current one

    CTRL-x-2

    open buffer window below

    CTRL-x-3

    open buffer window to the right

    CTRL-x-o

    switch to another buffer

    ESC-x

    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
nano
$ echo $VISUAL
nano

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
vim
$ echo $VISUAL
vim

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: