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:
-
insert mode: keystrokes are interpreted as insertions into the file contents at the point of the cursor.
-
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
iswitch to insert mode: what you type shows up in file at the cursor
ESCswitch command mode, what you type is interpreted as vim commands
-
Commands for moving around:
kmove cursor up one line (can also use arrow keys)
jmove cursor down one line
lmove cursor right one char
hmove cursor left one char
CTRL-umove cursor up one page
CTRL-fmove cursor down one page
SHFT-gmove cursor to end of file
:<num>move cursor to start of line number <num> (e.g.,
:1moves cursor to start of file -
Commands for saving and quitting:
:wsave changes (write) to file
:qquit vim
:q!quit without saving changes
-
Commands for switching to insert mode:
iinsert: what you type shows at the cursor
aappend: what you type shows up after the cursor
oopen: starts a new line in file below the cursor
Oopen: starts a new line in file above the cursor
-
Commands to copy, paste, delete:
yycopy current line (line cursor is on)
<num>yycopy <num> lines starting with the one the cursor is on (e.g.,
8yycopies 8 lines)ppaste what was copied after line cursor is on
Ppaste what was copied before line cursor is on
xremove the char at the cursor
<num>xremove <num> chars starting with one at cursor (e.g.,
4xremove 4 chars)dddelete the current line (p will paste it back)
<num>dddelete <num> lines starting with one at cursor
:uundo action, can undo multiple times
:redoredo 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.,
/hellosearch for hello in file)nfind next occurrence of search pattern moving forward
Nfind 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-vvisually select line (use arrow keys to select more lines)
=reformat visually selected text (auto-indent C code!)
gqreformat visually selected text to 80 char max line length
-
Misc. Commands:
:set numbershow line numbers
:set nonumberdon’t show line numbers
Jjoin line under cursor with line on cursor
:help <cmd>help documentation about a specific vim command,
:qto 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-sis hold down theCTRLkey and typexfollowed bysCTRL-x CTRL-ssave the file edits
CTRL-x CTRL-cexit Emacs
-
Moving the cursor
CTRL-nmove the cursor forward one line
CTRL-pmove the cursor backward one line
CTRL-bmove the cursor back one character
CTRL-fmove the cursor forward one character
-
Deleting Content
CTRL-ddeletes a character
CTRL-kdeletes a line.
-
Copying Content
CTRL-kcopy a line.
CTRL-ypast
-
Search
CTRL-ssearch for a pattern
-
Misc. Commands:
CTRL-gquit out of emacs command sequence (useful if you type in something wrong)
CTRL-x-0close the current buffer window
CTRL-x-1close all other buffers except the current one
CTRL-x-2open buffer window below
CTRL-x-3open buffer window to the right
CTRL-x-oswitch to another buffer
ESC-xenters meta mode (in Emacs documentation and in this table forward, it is listed as
M-x)M-x goto-line ngo to line n in the file
M-x linum-modeturn on line numbering
M-x ansi-termopen 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:
-
Vim cheat sheet from CMU
-
Vim documentation from vim.org
-
GNU Emacs cheat sheet from gnu.org
-
Nano editor documentation from nano-editor.org