13. vi survivors guidehere is the minimum you need to survive, be lucky.
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 |
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.
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.
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.
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.
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.
The shift key (combined with a letter) usually modifies the same command, to move by a bigger word, or search in the opposite direction.
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.
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.
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).
(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.
delete an AREA of text
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-^
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.
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.
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.
!G sort
(then undo)
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)
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.