the wonders of vi(m)
Ever since I started using Linux, which at this point was about 10 years ago, I’ve used the Vi editor. It was included as the default editor for most distributions back then, and it was really all that I was accustomed to. The usually cryptic commands make it pretty difficult to pick up, and I think that difficulty is why many people dismiss it so much. If you give it a try though, you’d be surprised at what you can do with it.
An article recently at The Register gave some interesting background. Essentially, Vi was made to be usable over a 300 baud modem. At that speed, even small commands are burdensome to type, and screen painting is horrendous. So the small, one-letter commands in Vi had a specific purpose – speed and efficiency.
So that begs the question: In the age of high resolution and broadband, why would anyone in their right mind still use an editor from 1976? The answer, to be honest, isn’t an easy one. Like I said, I personally started using Vi when I didn’t know of any other choice. I learned three commands… ‘a’, ‘:q’, and ‘:wq’, and that was good enough for me. Those commands allowed me to edit any file I needed to edit on my Linux boxes, and I could do it quickly. As any Vi user will attest, once you learn the very basic commands, you find yourself using them everywhere, even when you’re not using Vi. I think it’s because they are SO easy to use, they just make sense.
At this point, I suppose I should start saying ‘Vim’, for fairness. Vim was created to give a user the power of Vi, but extend it to add many cool features. The abilities of Vim are a surprisingly long list. Most anything you can think of, Vim can manage it, and it still remains a text-based editor.
A GUI exists for it, sure, but I honestly know no one who actually uses it. :)
These days, I’ve had the wonderful chance to use Vim in my every day work, and I must say, I love it. I keep 3-4 xterms open all day long, and Vim runs on all of them. Something I’ve noticed is that like Linux itself, using Vim every day allows you to learn some incredibly useful shortcuts that you may never have seen. That’s my reason for writing this article… to educate those Vi users who only know the most basic commands, and to also help cement some of the useful shortcuts in my brain.
First, my (most recent) .vimrc file:
:hi Normal guifg=white guibg=black
:hi Search guibg=#224488
:hi Visual guibg=#111144
:hi DiffAdd guibg=#003311
:hi DiffChange guibg=#222222
:hi DiffDelete guibg=#110000
:hi DiffText guibg=#AA1100
:set guifont=Bitstream\ Vera\ Sans\ Mono\ 7.5
So some very useful things any newbie should know…
The ‘/’ character, in command mode, starts a search. This search accepts regex, and can be global or one line. So the simplest use is to simply type ‘/something’. That command highlights every instance of the word ‘something’ in the file. While still in command mode, typing ‘n’ will go to the next search result. Quick and painless, right?
The search and replace syntax is pretty standard. The replace the abbreviation ‘NY’ with ‘New York’, simply type ‘:s/NY/New York/g’. That will replace it on the current line only. The expand it to the entire file, just add a ‘%’ in front of the s: ‘%s/NY/New York/g’. Pretty simple, eh?
Here’s something that’s cool… split screen editing of the same file. Hit ‘Ctrl-w s’ and you’ll now have two copies of the current file on the screen. Do it again, and you’ll have three. ‘Ctrl-w c’ closes the current split. If you use the ‘mouse=a’ setting, clicking will switch between the different sections. Otherwise ‘Ctrl-w uparrow/downarrow’ will do it. There’s a surprising lack of split-file editing capabilities in editors these days. Enjoy it!
Did you know about Visual mode? Hit ‘v’ in command mode and use the arrows to highlight lines. You can then use ‘d’ for delete (like the ‘cut’ command), ‘y’ to yank (like the ‘copy’ command), and ‘p’ to paste. If you have the ‘mouse=a’ setting enabled, click-drag will automatically enter Visual mode. So it’s a quick clickdrag-y-click-p to copy and paste text between two places in the file. While you’re at it, highlighting a chunk of text in Visual mode and hitting ‘<' and '>‘ will tab/untab the block. 2007-02-02 – You can also hit a number before you hit ‘<' or '>‘ to tab that number of times.
One thing I like to do is store commonly used methods in regular text files. If I need those methods, I can insert them directly into the current document with ‘:r filename’. That reads the entire file in and places it wherever your cursor was located in the file.
If you don’t like the default colors that Vim uses, you can modify them. Take a look in /usr/share/vim/vim70/colors. Those are the default color schemes that come with Vim, and you can use them with the command ‘:colors [colorfile]’. You can also just use them as a starting point and include custom color commands in your .vimrc file.
I know there are more things that I do in Vim that I can’t think of right now, so I’ll come back and update this article now and then. I hope at least one or two of these functions were unknown when you started reading. If you’re a Vim master and you’ve read this fafr, what are your favorite features? Please leave a comment!
“$” means ‘end’. It can mean the end of the line, or the end of the file. Typing “:$” goes directly to the last line. And on the subject of moving around in the file, use ‘:’ and the line number to warp directly to a specific line.
So recently I’ve started using (gasp!) Gvim, the ‘GUI’ for Vim. The thing that sold me on it wasn’t so much the GUI features (I’ve disabled them all) but the font rendering features. For one, you can use true-type fonts, rather than being stuck with the one in xterm. The other thing though, is that it does AA on the fonts, which means dot values! So I’m now using Bitstream Vera Sans Mono 7.5. It doesn’t seem like much, but try that in an xterm! :)
While I’m here, I’ve started using yet another feature. While in insert mode, with mouse=a enabled, double-click on a word. It enters visual mode and highlights the word. Now hit ‘d’ for delete, and it deletes the word and returns to insert mode.
Also, instead of using the method I detailed above for split-screen editing, I’ve started using :split and :vsplit. This allows you to split the current screen up quite a lot. Then, while in one of the split frames, type ‘:e path/to/file’ and you now have multiple files open in the same window. That was HUGE to me! And since they’re all within the same session of Vim, the copy/paste buffer will go between all of the files. That rids me of the need for the mouse=c functionality. Woo!
It’s funny how your methods change over time. The amazing thing is how often I find new ways of doing something in Vim. I explained somewhere above how to use Visual mode. Basically, if you have mouse=a set, you can just highlight characters and you’ll automatically be in Visual. Here’s something cool though: try moving your cursor to the middle of a line and hit Ctrl-v, then use the down arrow. You’ll notice that instead of highlighting the entire line, it allows you to highlight a certain character region on your screen. This is Visual Block mode. Imagine if you had two columns of text and you wanted to move the second column a couple of tab stops over… Just put your cursor at the beginning of the column you want to move, Ctrl-v, down arrow to the end of the column, and then hit ‘>’. Neat, eh?
Sometimes you need to replace characters in a certain chunk of code, but not the whole document. Using either Visual or Visual Block mode, highlight the area you want the ‘replace’ to affect. Then you modify the normal pattern “:%s/something/something else/g”, adding “\%V”. So to replace ‘Yay’ with ‘w00t’ in a Visual area, you’d use “:%s/\%VYay/w00t/g”. Easy as Pi. ;)