Chapter 5: Text, editors and the shell environment

Edit files with vim

Linux keeps its configuration in text files, so an administrator edits text all day. vim is the editor you can count on finding on every server, even a minimal one with no desktop. It feels strange for the first hour because keys mean different things in different modes; once that clicks, it is fast.

vi and vim

vi is the classic Unix editor; vim ("vi improved") is the version RHEL ships, in two packages. vim-minimal provides vi, a small build with the core editing features, installed even on minimal servers. vim-enhanced provides the full vim, with syntax colouring, built-in help and the vimtutor tutorial. Your lab has both, so type vim; everything in this lesson also works in vi.

[student@servera ~]$ vim notes.txt

If the file exists, vim opens it; if not, vim creates it when you save. On a server where vim isn't available, vi always is. For quick edits, nano (which you used in chapter 4) is also installed in the lab, but it is often missing from minimal servers, so learn vim.

Modes

In most editors, every key types a character. In vim, what a key does depends on the mode. Select a mode:

vim's modes: Select a mode to see what keys do there and how to get in and out.

vim starts in normal mode, where keys are commands: x deletes a character, dd deletes a line, u undoes. To type text, press i to enter insert mode, type, and press Esc to return to normal mode. : starts a command line at the bottom of the screen for commands such as saving and quitting.

The minimum you need

With only these, you can edit any file:

Keys (normal mode)Does
iStart inserting before the cursor
EscBack to normal mode
arrow keysMove
xDelete the character under the cursor
uUndo; Ctrl+R redoes
:wWrite (save)
:wqWrite and quit (:x and ZZ do the same)
:q!Quit, discarding every change since the last save

:q alone refuses to quit if there are unsaved changes, which protects you. :q! is the escape hatch: when an edit goes wrong, quit without saving and start again.

Work faster

Once the basics feel natural, these save a lot of time:

KeysDoes
h j k lLeft, down, up, right without leaving the home row
w / bNext / previous word
0 / $Start / end of the line
gg / G / :42First line / last line / line 42
a / AInsert after the cursor / at the end of the line
o / OOpen a new line below / above, in insert mode
dd / 5ddDelete (cut) a line / five lines
yy / p / PYank (copy) a line / put (paste) below / above
/text, n, NSearch forward, next match, previous match
:%s/old/new/gReplace every old with new in the file
v, V, Ctrl+VSelect characters / whole lines / a column block; then y to copy, d to cut

Many commands take a count: 3x deletes three characters, 2yy copies two lines.

Configure vim

Settings for every user go in /etc/vimrc; your own go in ~/.vimrc, which overrides them. Chapter 1 created yours:

[student@workstation ~]$ cat ~/.vimrc
set number
set autoindent
set tabstop=4
set shiftwidth=4
set expandtab
set hlsearch

Line numbers, automatic indentation, tabs typed as four spaces, and search matches highlighted. You can also try a setting in a running vim by typing it after a colon, for example :set nonumber.

Learn by doing: vimtutor

vimtutor opens a copy of a tutorial in vim, which teaches vim by having you edit the tutorial itself. Lesson 1 takes about ten minutes and covers everything above; the next exercise uses it.

Check your understanding