13. vi survivors guide

here is the minimum you need to survive, be lucky.

  • vi filename
  • Cursor Keys (h-j-k-l)
  • x
  • i ... ESC
  • :w
  • :q
  • :q!
  • :e!

vi(1) -aka- visual is an outdated editor, from when visually oriented editors were new (ie you can see a screen full of text rather than a single line). Personally, I still use it, mostly because it is everywhere. Particularly on boot-disks, mini-environments and all UNIX flavours. You can even find it on DOS.

There are several variations on vi, and each one has it's own strangeness (missing features, changed keys, function keys that do/don't get handled, etc), and can be frustrating. If you are a newbie, it's a bit like hell.

Currently I use elvis(1), it's a good vi clone, with lots of sensible features, and you can even use it as a text based HTML browser (see below).

If you are a newbie from DOS-land, (or not at all!) and you need to edit a file to make your system boot, then this guide will help you. An alternative is the use the excellent Midnight Commander mc program, and configure it (F9 / Options / Configuration / Use Internal Edit). The newer version(s) has a usable editor, with displayed function keys. It makes those first steps a bit less daunting.

13.1 Every letter is a command!
13.2 ESC
13.3 Cursor-Keys (h-j-k-l)
13.4 Insert mode
13.5 Motions
13.6 SHIFT (theme)
13.7 COMBINATION (theme)
13.8 x
13.9 u -- undo
13.10 . -- dot repeat ...
13.11 d -- delete
13.12 y -- yank
13.13 R -- Replace mode
13.14 :q -- quit
13.15 .exrc -- the initialisation file
13.16 Others
13.17 helpers
13.18 Strange ones
13.19 elvis as an HTML browser
13.20 More information

13.1

Every letter is a command!

The key to vi, is knowing that almost every key is a command that does something, and that includes every letter of the alphabet and the puctuation keys!

Basically, if you forget to go into insert mode, or press ESC, don't realise and continue typing, you are typing commands, and they will move about or effect your document in a way that may irritate you or even upset you.

13.2

ESC

The ESC key takes you out of "insert mode" back into command mode. If you press it twice it beeps. If you press it over a serial line, you might have to wait 1 second for the key-handler to realise that it is a plain ESC, not the start of an ESC-sequence (eg F1 generates ESC-OP).

CTRL-&91; happens to generate an ESC byte, useful for VT220 keyboards that don't have one. CTRL-C or <INTR> also has a similar effect.

13.3

Cursor-Keys (h-j-k-l)

Most modern vi's, on properly configured terminals (terminfo) recognise the cursor arrow keys, maybe even page-up (etc).

For old terminals without cursor keys, and for scripts, the h-j-k-l keys do the same (left-down-up-right). Like word-star but one row. This is of course the legacy of one persons bad typing habits, where those 4 keys were the "resting position", though for you it may be strange, becuae it's offset one to the left.

Also SPACE moves one char to the right.

13.4

Insert mode

This is where the letters you type, actually appear in the document, not acting as command letters. You get into insert mode using the i command. You get out using the ESC key.

Ways to get into insert mode are:

You also get into insert mode when you cw (change word) which effectively deletes a word and goes into insert mode.

13.5

Motions

You move around the document using the cursor keys, or maybe using a faster method, here are a few:

You can also combine moving with a command, such as 'delete 3 words', or yank the next 3 lines into buffer-z.

This is where vi becomes useful, and actually quite logical. Unfortunately it doesn't read as well as it pronounces, so try saying the above out loud until you see the acronymns.

13.6

SHIFT (theme)

The shift key (combined with a letter) usually modifies the same command, to move by a bigger word, or search in the opposite direction.

13.7

COMBINATION (theme)

vi builds up command 'phrases' from individual commands. For example to delete the next 3 lines, and put them into buffer-z you type " z 3 dd (no spaces).

That's pronounced: using quote buffer zed, times three, dee dee. Where dee dee deletes a single line.

13.8

x

The x key deletes the character that the cursor is on. Along with ESC and i it's all you need to edit a file to make your system boot.

13.9

u -- undo

Undo the last change

Some vi clones, have multiple level undo, and extra commands to redo what you've undone. I don't like them, it's easier to not make mistakes, or save the file every minute and use :e! to reload the file. u also undoes the last undo (to one level of depth), which is why I don't like multiple undo's.

If you are a two finger typist and your eyes leave the screen, then you look up and you are somewhere strange, not sure what you've just done. The u command will toggle what you've just done (after an ESC), and probably take you back in the file to where you were (ie the last change you made, hopefully before you looked away).

13.10

. -- dot repeat ...

(dot) does 'the same again' (changes not motions), so after deleting one word, "." will delete the next. Inserts can also be repeated, as can pipeline filters.

13.11

d -- delete

delete an AREA of text

13.12

y -- yank

Yank and Put, are like copy+paste

Delete also keeps the deleted text in the yank buffer. The next delete overwrites that buffer, but named buffers last longer.

If you switch between files, the anonymous buffer gets dropped, but the named buffers are kept. You have to switch between files within the same editing session, using :n or CTRL-^

13.13

R -- Replace mode

Lower-case r replaces a single letter, rr replaces it with an 'r'. Upper-case R goes into Replace mode, or overtype mode. Beware of TABS that look like several spaces, but are a single char.

13.14

:q -- quit

Underneath vi(1) is ex(1), a line based editor. You can access it using the colon commands. There are several commands, refer to the manual pages.

:q (followed by Return) quits the editor. If you have changed the file, :q will complain and fail. Then you can either save the document or use :q! to really quit.

13.15

.exrc -- the initialisation file

At startup time, vi reads it's initialisation file. Here's mine.

map #1 !}fmt -60^V^M}
map #2 }}{
map #3 :e ~/.exrc^V^M

map ^W :w^V^M
map ^X :w^V^M:!%^V^M^V^M
map ^N :n^V^M
set showmode
set wm=4
set sw=8
set ai

Note that ^M is actually CTRL-M (carriage return), displayed as a "picture". You put them into a file by prefixing them with CTRL-V (otherwise vi thinks you want to start a new line). .exrc holds text that will be used LATER (when you press the F1 key, you will get a reformatted paragraph), and that includes a CTRL-M typed later. So .exrc needs a CTRL-V during reading, which means during editing you type CTRL-V CTRL-V , CTRL-V CTRL-M

Confused ? Just edit it and make it look like the above, but using control chars, not pictures.

13.16

Others

13.17

helpers

13.18

Strange ones

13.19

elvis as an HTML browser

Elvis can only browse local files, and it's text only, but it can follow relative URL's, simply by pressing Return when the cursor is over a URL. Use CTRL-T to return, or go back (you can alias it in .exrc if you like). You may also wish to call elvis as view, so that it starts in read-only mode, and make sure that the files belong to root so you don't modify them by accident. (This is similar to how it follows CTAGS)

13.20

More information

This is a survivors guide, so I won't scare you with any more details, however, you already know most things, and the rest leads on from what you've learned above. There are facilities to automate tasks, and the ex is itself a configurable environment.