Unix: Editing

by Gisle Hannemyr

A large number of editors exists for the Unix family of operating systems. This chapter only introduces the most common ones.

Table of contents

Introduction

There are two basic types of editors that you'll encounter: Screen editors and stream editors.

Screen editors are interactive editors that let you edit text on a screen. Stream editors are working on streams of text, usually reading from the standard input and outouting to standard output. For simple tasks, you use the CLI to interact with a stream editor. But you may also use stream editors inside scripts to alter text that is streamed through the script.

Screen editors

The standard screen editor that comes with GNU/Linux and Unix is vi or vim. The latter is just an improved version of the former. The version described below is vim, but vi is very similar.

It is a good idea to have a working knowledge of vim and vi, since these are the only screen editors you can rely on being available on any computer running Unix or Gnu/Linux.

As for the two other screen editors introduced in this ebook, nano and zile, these may have to be installed before you can use them. Their main attraction is that they are very similar to other screen editors (i.e. pico and emacs). If you know one of these already, you may prefer to use nano or zile instead of vim or vi.

Standard screen editor (vim)

The default text editor on many GNU/Linux and Unix sytems is vim. It is a modal editor similar to vi, which was the text editor that shipped with BSD Unix (a version of Unix developed at the University of California, Berkeley in the 1970ies).

In vim, you have four modes:

  1. Normal mode: most keys om the keyboard are interpreted as a specific command. This is the mode vim will start in.
  2. Command mode: longer commands may be typed after the prompt (:) at the bottom of the screen. To switch from the normal mode to the command mode type :.
  3. Insert mode: most keystrokes are inserted as text (leaving out those with modifier keys). To switch from the normal mode to the insert mode type i.
  4. Visual mode: helps to visually select some text, may be seen as a submode of the normal mode. To switch from the normal mode to the visual mode type v.

The command mode reverts to normal mode when the command has completed (unless the command was to quit vim). To go back to normal mode from the insert or visual mode, type [Esc].

To quit vim when nothing has been changed, type :q while in normal mode. To quit vim and save the changed file, type :wq. To force close all windows and quit without saving, type :qa!.

The editor starts in normal mode. Below is a table with some the most common keys you may use in normal mode:

Key Meaning
: Enter command mode.
i Enter insert mode.
v Enter visual mode.
/exp Search for expression exp.
nG Go to line number n.
x Delete character under cursor.
X Delete character before cursor.
D Delete to end of line.
dd Delete current line.
J Join current line with next.

See alsoThe above table only lists the most essential vim keys. For a much longer list of keystrokes, download and print one of these versions of the vim quick reference card: Adnan Aziz (html), Minglong Shao (pdf), or UTools (pdf).

Pico-like screen editor (nano)

[TBA]

Emacs-like screen editor (zile)

The text editor zile (Zile Is Lossy Emacs) is a light-weight text editor belonging to the emacs family that originated at MIT around 1976.

The keybindings of Zile are designed to resemble those of emacs. It incorporates many standard Emacs features, including:

Stream editors

Simple substitutions (tr)

[TBA]

Advanced stream editor (sed)

[TBA]

Character set conversion (iconv)

The iconv converts converts text from one encoding to another. You specify the encoding to convert from with the -f, and you specify the encoding to convert to with the -t. The converted text goes to the standard output.

To see the list of character set encondings iconv recognises, type:

$ iconv -l

For example, to convert from ISO Latin 1 (ISO-8859-1) into UTF-8, type:

$ iconv -f ISO-8859-1 -t UTF-8 < infile.txt > outfile.txt
$ iconv -f ISO-8859-1 -t US-ASCII < infile.txt > outfile.txt

Summary

Command Meaning
vim The standard screen editor for Gnu/Linux and Unix.
nano A Pico-like screen editor.
zile An Emacs-like screen editor.
tr [TBA]
sed [TBA]
iconv Character set conversion