vi - come on, beep me
14 April 2005
(Introducing Text Editors Part 2: Vi)
The next step up from nano is vi. Vi is unlike any editor you have used before. Here I will just say enough to get you typing a simple file.
All programs have to balance between power for regular users and simplicity for new users. This is because the two groups have different needs. If it is too simple then the program will soon become boring and useless, if it is too complicated then new users will be put off.
Vi chooses power over simplicity. With Vi you can edit text at an amazing speed. With a bit of experience you type and edit faster any than any other program. However the learning curve is quite steep and it will take you a few attempts to get used to it.
The original vi was written in 1976. The name was short for visual. Nowadays there are lots of different versions and clones.
Today, more often than not, when people talk about vi they will mean the program vim; which stands for V for vi and IM for improved. It is many times more powerful than the original vi. Like Emacs, Vim will run on more or less any computer and any operating system (gvim is the graphical version of vim, but there is no real advantage in that ;). Vim itself goes back almost a decade and a half so has lots of features and extensions. It has also raised lots of money for poor African children! It is one of the first known 'Charityware' programs, because vim is free software, the developers give any excess cash after development to one particular charity.
Other popular versions include: elvis, nvi, vigor, vile, bvi …. you get the drift, they are all a pun on the name vi.
Vi is a modal program, which means it has modes! The two main modes are text insert mode and beep mode. In text mode you can type text in. In beep mode you can type commands in.
This means you can type very fast because you are always using keys, you never have to take your fingers physically off the keys but more importantly you also you don’t have to mentally switch from typing to using a mouse, menus or cursor keys. Also unlike other editors you don’t have to hold down control or alt all the time. With a little touch typing skill you never have to look at the keyboard since almost everything is done using the normal letter keys.
To switch from insert to command mode you just whack the escape key. Command mode is nicknamed ‘beep mode’ because if you get confused as to which mode you are in then you can end up typing sentences into the command mode, to which the program will respond by beeping at you. To return to insert text mode, you have to hit the i key.
I half remember that someone once explained it to me using the metaphor of a word processor. In bloated word processor such as Word or openoffice, you move your mouse cursor out of the editing space into the menu space: alternatively you can type say Alt-e. When you have activated a menu then the keys work differently now, if you type say p then it will paste.
If you keep in your head that whacking the escape key in vi is like moving your mouse to the menu then it all starts to make sense. In modern versions of vim you can use menus or even a mouse in say gvim, this however removes the speed that you get from the modes. Also there is no program as beautiful looking as vim run under the real command line; it is completely hidden, you see nothing but your text and the command you are running.
After pressing escape as violently as possible (preferably while thinking of that fairground game with a hammer and the pop up moles), you are then in beep/command mode.
If will allow me tp complicate things a little more. There are now basically two types of commands. The first type of command is achieved by typing a single key.
Single Key Commands
These are your bread and butter commands such as cursor movement and cut and paste and so on. You cannot really work with vi unless you know these (or have a printed key or ‘cheatsheat’ close by). I will explain a few examples which happen to come into my head.
The home keys under your fist three finders of your right hand (on a bog- standard Qwerty keyboard these are jkl) along with the key left of your index finger (h), are your cursor keys. You can use these instead of moving your hand away to the cursor keys, culminately this may eventually add up to many hours of your life (or it may not, depending on how much you use vi!).
h=left, j=down, k=up, l=right
-- k -- h-- --l -- j --
v=visual, it 'sets the point' i.e. begins to highlight text d=deletes/kills/cuts text to the kill ring/clipboard (call it what you want). y=yank/copy text to the kill ring/clipboard p=put/paste the text into the file u=undo
Moving around words
e =move to the end of a word. b =move to the beginning of a word. 0 =move to the beginning of a line.
Moving around lines
$ =move to the end of a line. H=move to the highest line M=move to the middle line L=move to the last line
Colon Commands
Most of the other command start with a colon. This lets you type in a command which can be more than one letter. So if you hit escape then :q then you can quit vi, some emacs users joke that this is the only command you need to know!
:q Quit Vim. :q! Quit Vim without saving changes.
:e filename Open a new file with the name filename. Like with bash you can use tab to complete long names. :w Writes (i.e. saves) the current buffer under its normal name. :w filename Writes the current buffer to filename. :wq Write and exit.
More sources of information
There are hundreds of other commands that I do not know. Some of them are specialised for various tasks. Feel free to tell me any important ones that I have missed by using the comments below.
To get going fast with Vim, you need a cheat sheet, I will work on testing some out, but here are some already existing ones:
HTML http://www.fprintf.net/vimCheatSheet.html
PDF http://www.cs.uidaho.edu/~rinker/ed03.pdf PDF http://tnerual.eriogerg.free.fr/vimqrc.pdf
PNG Image of a hand-drawn vim cheat sheet http://www.ibiblio.org/web-gentoo/images/vicheat-final.png
Vim homepagehttp://www.oualline.com/vim-cook.html http://www.vim.org/
VIM Manual, HTML easy online viewing version: http://vimdoc.sourceforge.net/htmldoc/usr_toc.html
VIM Manual, HTML all in one page, useful for printing etc http://vimdoc.sourceforge.net/vimum.html
VIM Manual, PDF and Postscript versions for printing out http://www.eandem.co.uk/mrw/vim/usr_doc/index.html
Vim Cookbook by Steve Oualline, contains short recipes for doing many simple and not so simple things in Vim. http://www.oualline.com/vim-cook.html
Seven habits of effective text editing by Bram Moolenaar (Main Vim author) http://www.moolenaar.net/habits.html



1 Aaron says...
It would be nice if those links at the bottom of your post were clickable. :)
Posted at 5:03 a.m. on April 13, 2007