Fine knacks for ladies:
Love, marriage and the
promiscuous text editor
(with inconspicuous snide remarks pluck'd from the lyrics of John
Dowland's lute songs.)
Sue Blake
So, this is what Unix looks like, eh! You've installed *BSD, read
through the tutorial,
and it's time to do some tweaking.
Maybe you have to finish setting up ppp, or networking, or make some
quick improvements to the shell dot files, or reconfigure the kernel, or fix
one of those mysteriously powerful files in /etc that the installation
has dutifully configured according to your typos.
Forget it. You're not going anywhere for a while. That
complex powerful machine you've created there is little more than a
disk full of text files. Before you can talk to it sweetly you'll need
a good relationship with at least one text editor that'll never let
you down. A couple of others on the side is a good idea too. Whatever
your plans, whatever man pages you've been admonished to study, your
first task will be to find a text editor and get up to speed with its
basics.
Most people have a favourite that they believe
should be recognised as the best editor of all time. Arguments over
the relative merits of different editors are common, and the
expression of strong preferences is often compared to religious
zealotry.
I prefer to think of it as a marriage, or, with less commitment, a
love affair. Choose well, invest some effort in establishing a good
relationship, and your preferred editor will be ready to make merry
whenever the urge strikes.
But if you make a total commitment to one editor to the
exclusion of all others, what happens when it's not around and your
need is great? What if you're missing out on something better for a
particular task? It doesn't hurt to shop around a bit first, and
unless you already have some experience, in the beginning there's
going to be much experimentation.
Ideally you'll know which editor you want and install it from the
packages. But how do you choose? What if you need to edit some file to
configure something (like ppp) before you can get and install a
package? How can you edit the files you need right now?
There are a few text editors already installed with the operating
system. If you haven't used Unix before, their workings will
be a mystery and their man pages overkill. We're going to take a
sneak preview of the basic editing options, just enough to let you
decide which way to proceed and to make sense of the man pages
when you do. Think of it as a stroll through King's Cross. We're
looking for something to spend tonight with, not necessarily a
whole lifetime, though with Unix anything could be possible.
First up is a few of the many ways to work on a text file without
getting into a text editor. Then there's ed, the one true editor of
the bad old days, then ex and vi, the editor that everyone is expected to
know, and ee, the easy editor with a more human face that
is the initial default editor with FreeBSD. Finally, many other
editors can be installed as packages or ports, including the popular
Emacs.
Some of your small editing jobs could be done from the shell prompt,
without firing up a real text editor. It's handy to be able to do
this when using an unfamiliar system, when in a hurry, or when
showing off. The drawbacks are that you need to know how to use
quite a few tools to do this well, memorise a bit of syntax and type
with great care. It's easy to accidentally destroy
the file you're trying to improve. While some tasks are quick and simple
and others are frustratingly tedious, this method will never fail to impress.
Let's have a quick look at editing
from the commandline now, while nobody's watching.
This is more of a preview than a tutorial. Check with the
documentation before doing any of this for real, but a few examples
should make the documentation easier to understand as well as showing
the method's strengths and limitations.
The one editor that you'll find on every Unix machine is ed.
It is the simplest and most frustrating editor you're ever likely to
encounter, but it is the one that will be available when others are not,
even when only the root partition is mounted. Win the affection of
ed and all other editors will grovel at your feet.
Most of the time you're in ed it feels like doing brain
surgery in the dark. The file's contents are only displayed when
requested, your typing is most often read as ed commands
rather than additional text, and those commands act only on the current
line because ed, being a line editor, works on one line at a
time. Sometimes it's hard to know what the current line is unless you
ask. Any kind of error is reported with a simple "?", and there is no
"help" apart from the man page.
ed is a life saver when your terminal settings are mucked
up and other editors go berzerk, and since it doesn't ask you to
stretch out of position to use control keys it is very gentle on
over-worked wrists.
Everyone should try ed at least once. It might not be the
editor you decide to spend the rest of your life with, but it is the
only one that is sure to be there when all others have deserted you.
The next two editors, ex and vi, are basically
enhancements of ed. They are more versatile but require much
more learning to master all their tricks.
A short introduction to ed is guaranteed
not to hurt your brain and will provide enough practice to make sense of the man
page, either to learn more or to revise the essentials when
unexpectedly forced to use it later on. More details can be found in
many introductory Unix books (especially the musty-smelling leather-bound
type).
ex is another line-oriented text editor, basically an extension
of ed with as many similarities as differences.
vi is a screen-oriented text editor which looks and feels quite
different. ex and vi are really different interfaces to
the same program, which is usually started as vi so that both
interfaces will be available.
You'll find vi on just about any Unix system. It is old, light
weight, and surprisingly powerful once its secret incantations are mastered.
It is called a visual editor, because the cursor can be moved around the
file as it is displayed on the screen. If you know Unix, you're expected to
know vi.
In fact, many people use vi all the time with passionate
devotion, and it's easy to see why. There is little you could ask of a
text editor that vi doesn't do quickly and efficiently using
minimal resources. The full benefits are only apparent when a deeper
than normal level of intimacy has been reached, some time after the
second honeymoon, if you can keep it together that long.
In one sense vi is easier than the previous editors, but it
is a lot more complex. It is possible to use vi without
knowing a lot about it, but you will need to work through a tutorial before
starting. You'd never guess, for example, that you have to type
:q! to get out of vi without altering
the file.
When you're working in vi you'll be in one of three modes:
command mode, text input mode, or ex mode. Command mode is your home base.
In command mode, anything you type is either an editing or navigation command.
Press the Esc key any time to return to command mode (e.g. so you can type
:q! to quit without saving).
In text input mode you can type into the file,
and ex mode lets you use powerful ex commands.
Forget the man page for now. (At least it has the sense to warn you not to use
it to learn from.) Fortunately there are many vi tutorials available.
The new user
tutorial explains the basics, and the vilearn package is a
great way to practise the essentials while learning them.
At first sight, ee the easy editor, has everything going for it.
Here we have a full screen editor that can be worked by intuition and gusswork
rather than learning anything new. The commands are listed at the top of the
screen, and navigation is a snack.
ee is the default editor for root on FreeBSD systems. That's good
and it's bad. Love it or hate it, everyone can see how to use it, but pretty
soon you're going to find it too limiting for serious work. When you're
comfortable with another editor, change over. Meanwhile there are a few little
gotchas to be aware of.
To exit you press Esc for the menu (yes, it tells you that) and then
select from a list of options. At the first menu simply press enter to
leave editor, and at the second you can press enter or move the cursor
arrow down once to select "leave without saving". Simple, huh? The menus
can take a couple of seconds to come up, so be patient and don't worry.
The big problem with using ee, especially as root, is that
by default it wraps long lines. This is great for email but death to
configuration files. The wrapped part of the line will be read as
a new command causing all sorts of unexpected results. To turn wrapping
off, press Esc to get the menu, select "settings", and... hey, where does
it say anything about wrapping lines? The trick is (can you guess?) to
turn "margins observed" to OFF. Aha, of course, I should have known
that :-) When the menu says "Press Esc to cancel" what it really means
is that if you press Esc your settings changes will stick for this
editing session, but not be saved for future sessions. There's another menu
option down the bottom for saving settings.
(More recent versions of ee do not wrap by default, but it's
best to check for each editor. For example, if you use pico
the man page advises pico -w to disable word wrap.)
If ee is available on your system, it's probably the easiest
editor to start working on text files right away. If you treat it right
it'll get you out of a tight spot in the short term, but it was never
intended to be the marrying kind. If you haven't used ee a
lot and the files are important, always check that you're not leaving with
more than you came for.
Emacs is the opposite extreme from ee. Don't
even think of a one night stand here; Emacs will be lifelong
passion. The introduction is easy (a tutorial is presented to you when
it runs for the first time), the courting period prolonged but
rewarding, and full intimacy is an ideal that few ever achieve. Get to
know a tenth of what Emacs has to offer and you might never
want for more. It's up to you which of the features you want to explore.
Emacs is installed as an extra package, but is so popular that
it is likely to be installed on most systems you encounter.
If you're used to big hefty word processors with oodles of features,
Emacs will blow your socks off. Your wildest fantasies will
be fulfilled with a few gentle strokes of the keys. The only feature
you're going to miss here is the GPFs (don't ask, it's rude). Help is
never more than a ^H away. If you're using the X window system, you can
mouse about with menus until you learn the keys (they're written
against the menu items) which will let you work quicker.
Hey, don't go to sleep, I haven't finished yet. Apart from editing
you can use Emacs to read and write email and news, play
games, it will check your syntax in various programming languages,
report sunrise and sunset times and phases of the moon, remind you of
appointments, find lost files, and it even comes with a built-in
psychotherapist. And if you don't like the way it does something you
can change it; if there's something missing (unlikely) you can add it on.
Why would anyone not use Emacs? Because they can't. Compared
to more modest editors (like vi or joe) Emacs uses more than a
hundred times the amount of disk space, and great slabs of memory. If it
would fit on my 386, I still wouldn't have enough RAM and swap space to
run it properly. Also, as with any editor, the more features you want
to exploit the more commands you have to learn, and Emacs
is a law unto itself. If you are prepared to make the commitment, it
will certainly pay off. On the down side, you could easily find
yourself so committed to the Emacs way of doing things that
you dread the thought straying outside.
People who have reached this stage walk around with a permanent grin,
and at the sound of their beloved editor's name their eyes sparkle.
Listen closely and you might hear the little sighing noises that they
try to drown out with keyboard clattering.
And there's the other reason people don't use Emacs: because
they're afraid. Afraid of slipping too deeply into commitment, afraid
of getting hooked, afraid that they'll never be able to look
vi in the face again, afraid that one day when the system is
sick Emacs won't be there and they'll have no way to edit
files, read mail, check appointments, or even relieve stress by playing
games and talking to the good doctor.
This kind of fear comes from the erroneous belief that having a close
relationship with a text editor is like having a duty towards a
religion, or a single spouse that must be served and defended above all
others at all times. Try letting go. If passion and commitment are
based on true love, they will not restrain personal freedom to explore.
If you really have made the best choice, regular dalliance with other
editors will strengthen your relationship, rather than threaten it.
Emacs is just one of dozens of editors that can be
installed as packages or ports. Some, like vim, are
enhancements of one of the basic text editors. Others, like
pico, are part of another package (pine in this case), while
some are designed to be especially good for a particular task such as
programming or HTML. There are small trifiling editors, small powerful
editors, large powerful editors, those with a snazzy look and feel and
those whose real power is only revealed to the truly devoted.
Past experience has a bearing on an individual's preferences. For
example, I started using joe under DOS and OS/2 a year or so
before moving to FreeBSD, because I needed a small free editor with
advanced features. Now I use the FreeBSD port without any more
learning. Joe does most things anyone could want, but many people find
it unpleasant to use, it just doesn't feel right. Once I used to
believe that an editor should impress me out of the box, but now I
demand to be able to reconfigure them to my liking. Your position might
be somewhere between these extremes.
As we saw in the case of ee, an editor that is easy to use
without any training is not necessarily the editor that will do everything
you want now or in the future. In the long term, you might find several
different editors are required to answer your needs, for example:
- an easy editor that doesn't require much investment of effort
- a small, quick editor with a good range of features
- an editor designed for a particular purpose
- a large full-featured editor that rewards effort
- a basic editor, or equivalent command line tricks, that will
do the job when all others are absent
Never feel guilty about spending time with more than one. Play the
field a while and you'll be in a better position to make a choice when
it's time to settle down to a relationship based on respect rather than
empty admiration.
It's your needs that are important, nobody else's. Once you know a few of the
basic text editors you can play around with the others, make
your own choices, and change them without looking cheap.
Have fun! And be careful.
This article was originally typed in HTML using joe,
with final editing courtesy of ed. No small furry mammals
were molested during its production.
-- Copyright (C) 1998 Sue Blake --