As a sysadmin, and especially as a Linux/Unix sysadmin, you're going to have to get used to
not only editing text files, but also efficiently making your way around the command line.
Both of these tasks require you to get familiarized and comfortable with the two most common
text editors in this space: vi
and emacs
Back when UNIX was just starting to appear on computers that had actual input/output systems (e.g. teletype), interaction with the computer was strictly a text-based affair. The computer would send some data in the form of ASCII characters over a slow telephone link. The user would then hit some keys on the keyboard, which would send back more ASCII characters to the computer, again over the slow telephone link.
Because communication was so slow, UNIX programmers and users wrote their internal tools to
be as efficient as possible. Early editors, such as ed
, would only let you see one
line of text in the file at a time, since the commonly used interface at the time, a teletype,
printed on paper and thus there was no concept of "redrawing" or even of a "screen". These
early tools later evolved into the more advanced editors like vi
and emacs
in use
today. But these tools retain their UNIX heritage of lean efficiency. Commands are sent
usually in single characters, there's no "toolbar", and the editor provides powerful tools
to quickly shuttle the cursor around the file quickly, without having to display unrelated
text/context in the file.
vi
was created as an extension to ed
. When "modern" terminals (e.g. electronic
displays, not teletypes) became common, it made sense to use a "visual" editing mode,
where you could see the context of the line you were editing. From ed
, you would
enter "visual" mode by running :vi
-- thus VI was born.
Since then, vi
has been written and re-written many many times. But inside, you
will still find good ol' ed
. In Text Edting 201
, you will see how you can
still access the very powerful ed
features by using the colon (:
) command.
There are many versions of vi
out there, but the most popular is vim
(VI iMproved).
Virtually every Linux distribution out there provides vim
either by default, or
as an easily installable package. vim
extends the feature set provided by the
POSIX-compliant vi
utility in many ways. In the examples below, features that
are only present in vim
will be noted.
The first thing to understand about how vi
works is that it operates in two modes.
Insert mode is when you are actually typing in text. The keys you type while
in insert mode are simply dumped into the editor. You exit insert mode by pressing
the Esc
key.
Command mode is when vi
is not in insert mode. When in command mode, the
keys you type are interpreted by vi
as commands, not as text you wish to put
into the file. For example, moving the cursor around the screen is a series of
commands, as is writing to a file.
Before learning anything else, you'll need to know how to do two basic operations, which are best described by working through these steps.
Start vi
and edit a new file:
vi helloworld.txt
The editor will start up in command mode. The first thing we want to do is add
some text to the file. Press i
to enter "insert" mode. You will notice
that the editor begins to let you start typing text into the file. Enter
some text into the file:
Hello, World!
Now you want to save the contents, so follow these steps:
- Press
Esc
to exit insert mode and enter command mode - Press
:
and thenw
, thenEnter
. This will write out to the file. - The editor remains in command mode, waiting for your next command.
Now you want to exit the editor:
- Press
Esc
to exit insert mode (hittingEsc
while in command mode does nothing, so it's safe to just hit it for good measure) - Press
:
and thenq
(quit), thenEnter
. You will exitvi
.
Now that you know how to open a file, put some text into it, and then exit the editor. The next most important thing to learn
is how to move around in the file. While using the arrow keys might work, it's far from optimal, and may not be implemented in
strictly POSIX-compliant versions of vi
.
When in command mode, you use the h
, j
, k
, and l
(ell) keys to move the cursor around:
h
- leftj
- downk
- upl
- right
Seems confusing and awkward at first, right? It's actually not. You will quickly learn the motions without having to think about it, and you will be able to move around very fast in the file, without ever having to move your hands from the home row of the keyboard.
In addition to the simple cursor motion commands, there are a few other cursor motion commands that are extremely helpful to know:
w
- move forward one wordb
- move back one word0
- move to the beginning of the line$
- move to the end of the line/some text
- move forward to the next instance ofsome text
?some text
- move backward to the next instance ofsome text
n
- repeat the previous search operation:number
- Go directly to linenumber
(example::20
goes to line 20,:0
goes to the top of the file)G
- Go directly to the bottom of the file
vi
gives you several options for how you actually want to insert text when you enter insert mode.
i
- insert text at the position of the cursor. The first character you type will appear to the left of where the cursor isa
- append text at the position of the cursor. The first character you type will appear to the right of where the cursor isI
- Same as insert, but first moves the cursor to the beginning of the line (equivalent to0i
.A
- Same as append, but first moves the cursor to the end of the line (equivalent to$a
.o
- Open a new line under the cursor and begin inserting text thereO
- Open a new line above the cursor and begin inserting text there
When you delete text in vi
, you are actually performing a "cut" operation into a buffer. But you don't need to know that
at the moment. You will notice that while in insert mode, you can use the backspace and delete keys as expected. This
makes insert mode easiy to use, but it's not particularly efficient if you're trying to eliminate a whole paragraph or something
from your document. When in command mode, you can issue some commands that remove whole chunks of text:
x
- Delete the character under the cursordd
- Delete the line under the cursordG
- Delete the current line, and everything else to the bottom of the filed:0
- Delete the current line, and everything else to the top of the filedw
- Delete from the character under the cursor to the beginning of the next wordNUMdd
- Delete NUM lines of text, ex:10dd
deletes 10 lines
Now that you know how to add and remove text, you'll inevitably end up making a mistake.
Luckily, vi
lets you undo the last command or insertion, by going back to command mode and hitting
the u
key.
In vim
(but not strict POSIX vi
), you can also press R
to redo the last thing
you un-did.