< Unix: Editing

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 editors 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. Usually, nano is also included.

It is a good idea to have a working knowledge of vim, vi or nano, since these are the only screen editors will initially be available on a computer running Unix or Gnu/Linux.

As for the the other screen editor introduced in this ebook: emacs, it will have to be installed before you can use them. If you know it already, you may prefer to use emacs instead of vim, vi or nano.

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).

Nano

This is a lean, free and friendly editor similar to pico.

The version number is shown top left. With Ubuntu 14.04, the version is 2.2.6, with Ubuntu 18.04, the version is 2.9.3, with Ubuntu 20.04, the version is 4.8. with RHEL8, the version is 2.9.8.

To get rid of the braindamaged syntax highlighting that makes text unreadble, edit /etc/nanorc as follows (works on all versions).

The /etc/nanorc file contains the default settings for nano. It should use Unix line endings. During startup, nano will first read its system-wide settings from /etc/nanorc. By default, this contains the following line that inludes all the files that specify syntax highlighting.

include /usr/share/nano/*.nanorc

The quick fix to avoid syntx highlighting is to comment out the above line.

Another setting you may want to change is to see line numbers. To do so globally, uncomment this:

set linenumbers

In addition to the global files, you can edit user-specific settings in ~/.nanorc. For instance, to disable syntax highlighting:

syntax "disabled" "."
color black,white "."

Source: SO.

The editor displays one screen help in two lines at the bottom. Here is some much used keystrokes:

Key Meaning
Ctrl+K Delete current line (kut).
Ctrl+U Paste cut text (uncut).
Ctrl+6 Ctrl+K, Ctrl+U Cut & paste (kut, uncut).
Ctrl+W Search for text (where is …)
Ctrl+\ Find and replace text.

See alsoThe above table only lists the most essential nano keys. For more documentation, visit Virginia.edu: cheat-sheet (pdf) nano-editor.org: manual (html) or gentoo.org: Nano basic guide (html).

Emacs

The default, when running under X11, is to use the X11 interface. This enables nice things, such as cut & paste between systems, but the response is rather sluggish. For a quicker start, use the --no-window-system or -nw which tells emacs not to use the X11 interface. I.e.:

$ emacs -nw filename
$ emacsnw filename

To use the latter version, you need to alias this option in .bashrc.

Source: Superuser.com.

tipTo convert a HTML-file to utf-8, just change the meta element in the header to specify this charset, and save.

noteThe default spelling program used by emacs (ispell) does not support UTF-8. There is a replacement (hunspell) that does. For setting up, see StackExchange.

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.

However, it does not support UTF-8, which is a serious drawback in 2020. Do not bother with this.

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 that comes with Ubuntu.
emacs The real thing.
zile An Emacs-like screen editor.
tr [TBA]
sed [TBA]
iconv Character set conversion