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

Essex Web Design

September 3, 2010
A lot of contract providers give you free internet usage now, but if you have Pay As You Go, then you are going to be paying heavy prices.
Calling time on mobile internet nonsense?

Krasochka

September 2, 2010
Hack again?!
Adding more terminals to your function keys

GenryFlorist

September 2, 2010
<b>Cheap flowers delivery around the world!</b> Celebrate summer with our gorgeous flowers. They?re the perfect gift for any summer occasion. From birthdays to anniversaries, we offer beautiful flowers, lush plants, ...
Burning an iso to CD on Windows

auto-financing.co.cc

September 2, 2010
auto-financing
ReStructuredText tables and doctests

rubaxa

September 1, 2010
FTP = NOT RANDOM software Dominated hands postflop suckout often on all-ins. EX. AK vs. A9 or KQ vs. K6. Both players hit top pair. Bad player goes all in ...
Burning an iso to CD on Windows

empodayaddelm

September 1, 2010
Sorry admin - my post is test
This Week: Heroes and Monsters

increase synthroid dosage

September 1, 2010
Latest world news: 1 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/viagra-cialis-buy-no>buy cheap cialis generic levitra viagra</a> Viagra 2 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/buy-viagra-online-at-lowest>rainbowpush discussion board buy viagra</a> Viagra 3 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/how-to-get-generic-brand>search viagra ...
SFTP in Python: Paramiko

Lacilslaw

September 1, 2010
HYUN JAIMIE enniless and homele JAMILA
This Week: Heroes and Monsters

domaserisk

August 31, 2010
who was shaking his head back and forth knowingly Grissom shifted his eyes over at Brass,
How I Removed Windows from my Laptop

get ready loan

August 30, 2010
Though, by the you kill the legitimate PC user from visiting the site. Also, think about the dynamic IP's issue.
Only the penitent man will pass - on captchas and cotton wool

Packers and movers in pune

August 30, 2010
The topic you disscussed here is very amazing, informative and useful in future...
On Comment Spam

serhanters1

August 30, 2010
?? ???????? ??... ??????...... ??. ????????? ??? ??? ???????D ???????? ??. ????? ????? ???? ??? ???=) ?? ?????- http://letitbit.net/download/8746.894a84bc20f38f1661895aeee0/stereokartinki.html ???http://f-zona.ru ? ? ?? ????????????
Burning an iso to CD on Windows

strona startowa

August 29, 2010
Thanks For This Post, was added to my bookmarks.
Python CGI contact forms

lerexottori

August 29, 2010
?????????????? ??????????
Adding more terminals to your function keys

KelpAugmeme

August 29, 2010
aofaapsymp, http://forums.quark.com/members/jennaq.aspx online stock trading broker, rdgofzary
PuTTY Series: Adding PuTTY to your system path

Cheeday

August 28, 2010
What flowers do you like?
This Week: Heroes and Monsters

magfcvb

August 28, 2010
??????? ?????????????? ?????? - ????? ?????? ?????????????? ??????, ?????????????? ?????? ???????, ?????????????? ?????? crosman, ???? ??????????????? ??????, ?????????????? ?????? ?????? ????????. ???? magazin-oruzhie.ru
Include ODF support in the Linux Standard Base?

noni

August 28, 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

Latenadsfes

August 28, 2010
http://mynewblog.for-breastcancer.com/ http://mynewblog.photoblogcentral.com/ http://ilovezebras.thechicks.org/ http://mynewblog.cyberbardsymposium.com/ http://wewphost.com/ilovezebras/
Burning an iso to CD on Windows

LeupoldEst

August 28, 2010
pretty cool stuff here thank you!!!!!!!
OOXML Vote Coverage