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

Cupcake

July 31, 2010
Good post! You helped me a lot with my school project! CountryField(blank = True) < (K)
Countries in Django

LeshaShampoo

July 30, 2010
it was very interesting to read commandline.org.uk I want to quote your post in my blog. It can? And you et an account on Twitter?
Email Syntax Check in Python

vemma2018

July 30, 2010
I find myself coming to your blog more and more often to the point where my visits are almost daily now!
On Comment Spam

layecenda

July 30, 2010
Hello. And Bye.test :) http://idfjhvihdfiphvlajbvhalibv.com
PuTTY Series: Adding PuTTY to your system path

scuba

July 30, 2010
I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.
On Comment Spam

Businesking

July 30, 2010
Great site and articles for hack for win, I said Amazing post
How not to program WSGI

Tehnoking

July 30, 2010
This is Great post to learn about the hack Thumbs-up for you :D
How not to program WSGI

Syabiltech

July 30, 2010
I think this articles for master...because very hard to learning, As blogger beginners like me.
How not to program WSGI

coffeeatea

July 30, 2010
Are you looking for coffee gifts? We can tell you more about the coffee gifts including coffee machines and coffee pods.
Introducing Soturi - yet another Django blog application

noni juice

July 30, 2010
I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.
On Comment Spam

Dion Moult

July 29, 2010
What I do know is that ever since I tried out Opera and put their tab bar on the left as a column, I've loved that layout. Back on Firefox ...
We need a thoughout integration of the desktop and the web - not Tab Candy superfast jellyfish

ZonaEntertainment

July 29, 2010
Wow useful articles, I'm read to learn about this and now I bookmark this to my Facebook, thanks for share!
How not to program WSGI

Giacomo

July 29, 2010
Honestly, I think both Mozilla and you are wrong :) This sort of concept adds overhead. A user would have to manage all this crap, constantly dragging and dropping, creating ...
We need a thoughout integration of the desktop and the web - not Tab Candy superfast jellyfish

Matija "hook" Šuklje

July 29, 2010
As a minimalist, you'll probybly moan if I mention KDE, but I'll do so anyway ;) The future I want (and actually see slowly fold out before me) is to ...
We need a thoughout integration of the desktop and the web - not Tab Candy superfast jellyfish

tahitian noni

July 28, 2010
Thank You For This Blog, was added to my bookmarks.
On Comment Spam

Rick

July 28, 2010
I already have piles. It's called A New Window.
We need a thoughout integration of the desktop and the web - not Tab Candy superfast jellyfish

Tech News

July 25, 2010
Thanks for this short tutorial...was auto-FTPing my files from my appserver to webserver for my tech news website. Everything was OK until someone hacked it. Hosting provider is now recommending ...
SFTP in Python: Really Simple SSH

naypalm

July 24, 2010
During the past 3-4 years, I and many others have enjoyed unlimited 2G/3G internet. But ever since the massive cult-like following of i Phone users in the US, most cellular ...
Calling time on mobile internet nonsense?

Steve

July 15, 2010
Very occasionally, you will run into a Java program that uses a lot of memory just to hold all the classes used. It turns out that the JVM uses a ...
Three classic command line tips

no

July 14, 2010
1. number one 2. number two 4. number four 3. number three 6. number six # first # second ## second-ay ## second-bee ### second-bee-one ### second-bee-two
An Introduction to ReStructuredText