Quick and Dirty vi
Getting Out of vi
In case you are only here because you're stuck in vi with a messed-up file on your screen, you probably just want to kill it without saving the file. Relax.
- Hit "Escape" a couple of times to insure that you are in command mode. (If escape is not working, try "ctrl [" - that's the control key, plus the left square bracket.)
- Type ":q!" (Without the quotes.)
(I put that in first, because before I got to know vi, I would sometimes get stuck in exactly that situation.) Now on to the real article...
If you're not familiar with the vi editor, it may seem like a bit of a dinosaur. Some creaky anachronism from the days of punch cards and dumb terminals.
In a way, it is, yet it continues to have a following of people who actually prefer vi to other editors. (Especially to emacs, but that's a whole other story.)
You see, it has certain advantages over other editors:
It is available on nearly every unix and unix-like operating system.
It works beautifully over the slowest of connections.
It works on just about any kind of terminal you are likely to encounter, from the little terminal program that comes with Windows, to an Xterm, or even on strange hardware such as a Palm Pilot or an antique printer terminal.
(A few years ago, I picked up a Wyse terminal that had a great keyboard and 'Paper White' screen. I hooked it to a 9600 baud modem and would use it to dial into my shell account. It was actually quite nice to use, even at that speed. I didn't bring it to Japan with me, but sometimes wish I had.)
Sometimes you have a file on a remote machine that you need to change, such as a web page on a unix system. You could download the file, edit it locally, FTP it back up to the site and re-load it in your browser, to see the changes. Or, you can edit it right there on the server, using vi.
(On a beginner-friendly system, you might have another editor available, such as 'pico'. Pico is much easier for someone to use the first time, but as unix shell accounts become more rare, pico seems to get installed less and less. You might just be stuck with vi.)
Sure, the clue hurdle is a bit higher at first, but with about a half hour's practice, you can be productive in vi.
The vi editor is a mode editor - what that means is that in different 'modes' pressing keys will have different results.
When you first open up vi, (or are tossed into it unwillingly by another program, such as a unix mail program,) you are in 'Command Mode'. You can't just start typing your text. Command mode is for doing everything but typing text; moving around the screen, deleting text, copying, pasting, saving your document, or sometimes more importantly, just closing the file without saving anything.
To get into command mode, you hit the escape key. (Or Ctrl+[ if that doesn't work.)
If you're not sure which mode you are in, just hit the escape key a couple of times. Your terminal may beep, but it doesn't do any harm
One of the most common mistakes beginners make while using vi, is to just start typing, without knowing which mode you are in. If you want to start typing, you have to get into 'Insert' mode.
From command mode, type 'i' to go to insert mode. If you're just getting started with vi, get in the habit of typing Escape+i to get into insert mode. This first puts you back in command mode, then into insert mode. Why would you want to do this? Well, often times, you don't know which mode you are in - Some vi commands leave you in different modes. Think of command mode as a kind of 'home base' that's always an escape key away.
Getting In and Out of vi
Opening a file
From a command line, type "vi filename".
If the file doesn't already exist, it will be created.
To open multiple files, simply type "vi file1 file2". For now, though, just open one file at a time.
Saving and Closing your File
To save a file, go to command mode by hitting Escape, then type ":w" without the quotes. (Think of the w as standing for 'write'.).
To save and close, type ":wq" from command mode, or simply "ZZ".
(You'll want to save frequently - Especially before attempting something new, such as a replace command.)
To save to a new filename, type ":w newfilename"
Sometimes, you've been editing a file for a while, go to save it and find that you don't have the proper permissions to write the file in that directory. Don't worry, you can save it somewhere else, then deal with it later. Simply type ":w ~/filename" (Again without the quotes.) This will save it to your home directory as 'filename'. "~/" is a way of referring to your home directory in unix, not just in vi.
Sometimes, you don't want to save the file - you just want to quit. If you haven't made any changes, simply type ":q" from command mode.
If you have
made changes, but don't want to save them, type ":q!". This abandons your changes from the last save.
Other Useful Concepts
With many of the commands in vi, you can tell the editor to do them several times. For instance, in command mode, to delete a character, you type "x". So to delete 5 characters, type "5x".
Another example is "yy" which copies a line into the buffer, for later pasting. To copy 10 lines, type "10yy". To paste it, move to the location above where you want to paste and type "p". (Or type "10p" to type 10 copies of the buffer.
Where to Go from Here
That's probably enough to get you started.
For a list of the most useful commands, also take a look at vi Command Reference
. You may want to print it out or bookmark it in your browser. I keep mine on my palm pilot, for quick reference. I've formatted it to be easily accessible from palm or lynx.
The web is full of vi references like this, as well as resources for more advanced editing.
I'd recommend you start with Purdue University's
vi tutorial and reference.
Vigor (pronounced like 'Igor') is truly the worst thing I have ever seen on Unix. It started as a joke on the UserFriendly comic strip last year, in which they decide to release a version of VI with 'Clippit' the Microsoft paperclip assistant. If you're not familiar with Clippit, consider yourself quite lucky. Recent vestions of Microsoft Word came bundled with an officious little program in the form of an animated paperclip that would misinterpret your actions and annoy you as you worked.
Some sick soul decided to make this a reality.
I just downloaded this atrocity and yes, it is real. See for yourself here
I'd be remiss if I didn't mention 'ED'. ED is an editor even more barebones than VI. It's probably on my system, but I've never tried it.
Why? Frankly, it scares me.
I will, however, offer the following post that appeared on Usenet, 10 years ago:
From: email@example.com (Patrick J. LoPresti)
Sender: firstname.lastname@example.org (News system)
Subject: The True Path (long)
Date: 11 Jul 91 03:17:31 GMT
Organization: Massachusetts Institute of Technology
Xref: ai-lab alt.religion.emacs:244 alt.slack:1935
When I log into my Xenix system with my 110 baud teletype, both vi
*and* Emacs are just too damn slow. They print useless messages like,
'C-h for help' and '"foo" File is read only'. So I use the editor
that doesn't waste my VALUABLE time.
Ed, man! !man ed
ED(1) UNIX Programmer's Manual ED(1)
ed - text editor
ed [ - ] [ -x ] [ name ]
Ed is the standard text editor.
Computer Scientists love ed, not just because it comes first
alphabetically, but because it's the standard. Everyone else loves ed
because it's ED!
"Ed is the standard text editor."
And ed doesn't waste space on my Timex Sinclair. Just look:
- -rwxr-xr-x 1 root 24 Oct 29 1929 /bin/ed
- -rwxr-xr-t 4 root 1310720 Jan 1 1970 /usr/ucb/vi
- -rwxr-xr-x 1 root 5.89824e37 Oct 22 1990 /usr/bin/emacs
Of course, on the system *I* administrate, vi is symlinked to ed.
Emacs has been replaced by a shell script which
1) Generates a syslog message at level LOG_EMERG;
2) reduces the user's disk quota by 100K;
3) RUNS ED!!!!!!
"Ed is the standard text editor."
Let's look at a typical novice's session with the mighty ed:
eat flaming death
Note the consistent user interface and error reportage. Ed is
generous enough to flag errors, yet prudent enough not to overwhelm
the novice with verbosity.
"Ed is the standard text editor."
Ed, the greatest WYGIWYG editor of all.
ED IS THE TRUE PATH TO NIRVANA! ED HAS BEEN THE CHOICE OF EDUCATED
AND IGNORANT ALIKE FOR CENTURIES! ED WILL NOT CORRUPT YOUR PRECIOUS
BODILY FLUIDS!! ED IS THE STANDARD TEXT EDITOR! ED MAKES THE SUN
SHINE AND THE BIRDS SING AND THE GRASS GREEN!!
When I use an editor, I don't want eight extra KILOBYTES of worthless
help screens and cursor positioning code! I just want an EDitor!!
Not a "viitor". Not a "emacsitor". Those aren't even WORDS!!!! ED!
ED! ED IS THE STANDARD!!!
When IBM, in its ever-present omnipotence, needed to base their
"edlin" on a UNIX standard, did they mimic vi? No. Emacs? Surely
you jest. They chose the most karmic editor of all. The standard.
Ed is for those who can *remember* what they are working on. If you
are an idiot, you should use Emacs. If you are an Emacs, you should
not be vi. If you use ED, you are on THE PATH TO REDEMPTION. THE
SO-CALLED "VISUAL" EDITORS HAVE BEEN PLACED HERE BY ED TO TEMPT THE
FAITHLESS. DO NOT GIVE IN!!! THE MIGHTY ED HAS SPOKEN!!!