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


What do you have to say?

Show Editing Help

About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

Zeth

November 29, 2009
Hi Jordan, yes that URL is gone now. I have a new contact form on this site.
Python CGI contact forms

Jordan

November 29, 2009
Zeth attention! Your form, http://zeth.me.uk/contact/, is not working The explorer says connecting ..but nothing happens Sorry for my poor English: I am Spanish Regards
Python CGI contact forms

Jordan

November 26, 2009
Sorry: tell me , not tellme (I'm spaniard) And http://zeth.me.uk/contact/ don't work
You got the touch, you got the power

David Jones

November 25, 2009
Your mad skillz are too l33t! for me. I specifically switched to Google Reader so that I could show people what blogs I read. But I couldn't work out how ...
How to find the fashionable blogs quickly

Brian R. Hickey

November 20, 2009
Symantec picked it up too.
How to bring down Internet Explorer with six words

Zeth

November 17, 2009
Thanks djm, I am the moose here. Christian, assuming one actually does Internationalise the countries, it should still work I guess, as the gettext stuff will happen before the list ...
Countries in Django

Phillip Temple

November 17, 2009
Good start, but: a) wouldn't I want None back rather than 'ZZ'? b) why not add a 'shortcut' boolean, then prepend flagged fields (plus usual '-----' separator) to the actual ...
Countries in Django

djm

November 17, 2009
Am I being a moose or did you mean: from whatever.countries import CountryField instead of from whatever.countries import CharField ? Good post though, cheers.
Countries in Django

Christian Joergensen

November 17, 2009
Wouldn't the ordering get messed up after i18n?
Countries in Django

Steve - Electronic Cigarettes Fan

November 17, 2009
Very well done. Is your blog just you writing? Nicely done, Steven.
Blogger vs Wordpress

vetetix

November 15, 2009
Sorry to bother you nearly two years after you wrote this blog article, but I can't manage to find how to modify an existing field. I am trying to change ...
Three Useful Python Bindings - ClamAV, Apt and Evolution

Manju

November 4, 2009
I am transferring some files using psftp to other device's FAT partition. But the filestamp of the file being transferred is modified to that of FAT device, after the transfer. ...
PuTTY Series: Using PSFTP

iki

November 2, 2009
or simpler: socket.gethostbyname_ex(socket.gethostname())[2]
How to find out your IP address in Python

iki

November 2, 2009
local_ip = set([ i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None) if i[0] == 2 ])
How to find out your IP address in Python

Fred

November 2, 2009
testing rst ------------- - point 1
An Introduction to ReStructuredText

Ano

October 27, 2009
"You simply found the license of the StumbleUpon Toolbar for Internet Explorer." That's possible. I've got some more interesting information to add. Firstly, go to this page: https://addons.mozilla.org/en-US/firefox/addon/138 - this ...
Are your Firefox extensions proprietary software?

Ken

October 21, 2009
Stumbled in here at lunch. This is the best find of the week. Thanks.
Three classic command line tips

Jim

October 19, 2009
Thanks for the rtsp:// post - that's something that has been bugging me for a while!
Three classic command line tips

Zeth

October 18, 2009
Thanks for the comments guys. Great to see the all the gang are still here!
Three classic command line tips

Bubba

October 18, 2009
Is there any way psftp can return the true transfer rates oberved during the actual transfer?
PuTTY Series: Using PSFTP