Sunday, December 6, 2009

Vim 201: An Intermediate Guide to Vim

Ready to boost your Vim skills? Many use Vim, but don't make use of nearly all the features.

In this guide, we'll take a look at some of the intermediate features offered by Vim, including abbreviations, word completion, and editing multiple documents in the same Vim session.

In the previous installment, I took a look at the basics of editing, movement, searching and replacing, and copy and pasting text.

That is enough to get most people by if they're just using Vim for quick and dirty text edits, etc. But that doesn't even begin to scrape the surface of what Vim can do!

Remember that learning Vim is not a sprint, it's a marathon! But you can go at your own pace and stop along the route at any time to admire the scenery.

Even though I'll try to cover as much as possible in each guide, it is impossible to cover all that Vim is capable of in a handful of articles.

This isn't meant to be a wholly comprehensive guide, but a set of starting points for users to get oriented with Vim and become more and more productive.

Word Completion
If learning Vim seems like hard work at first, rest assured that in the long run it will save you work.

One of the ways that Vim saves you work is with word completion.

Obviously, this is very useful for developers and system administrators when editing code or configuration files.

As you're typing a longer word, hit Ctrl-p or Ctrl-n.

Let's say you're typing the word searching, which you've already used once in a document.

Type "sea" and then hit Ctrl-p, and Vim will either autocomplete the word if "sea" is a unique string, or show a list of words that begin with "sea".

When you hit Ctrl-p, you can keep typing to narrow the selection, or use the arrow keys to choose the right word, and then hit tab.

What Vim is doing when you use this command is to search through the document and find similar strings, and then displays the options.

If the word/string hasn't appeared in the document before, you'll get an error that says "pattern not found."

What's the difference between Ctrl-p and Ctrl-n?

The Ctrl-p command searches for "previously" used terms and displays them in that order,

and Ctrl-n searches for the "next" terms, and displays in that order.

For most uses, they're pretty equivalent.

Abbreviations
As you're probably noticing, Vim is a great editor for people who like to conserve keystrokes.

Another extremely handy feature of Vim is abbreviations.

These can be set on the fly. Let me show you what I'm talking about.

Let's say that you're editing a document and have to use a particularly long word repeatedly, or a company or person's name.

If you don't want to type out ReallyLongWord over and over again, you can do this instead:
:ab rlw ReallyLongWord

So the first argument is the abbreviation, the second argument to the :ab command is the expansion.

Now, when you type the characters rlw you'll get ReallyLongWord. Simple, no?

What happens if you want to unset an abbreviation?

You could exit Vim, and the abbreviation will go away when the session ends, but that's a hassle.

To get rid of an abbreviation, use :una rlw and it will clear it. Just that easy!

This is a feature that I used extensively when writing full-time.

It came in especially handy with longer names and projects.

It also ensured I only needed to spell someone's name correctly once, which is handy.

Mappings
Being able to quickly enter arbitrary strings with abbreviations is useful, but what if you'd like to run a sequence of commands with a keystroke or two? You can do that with a mapping.

Let me give a quick example.

Vim offers the ability to highlight search terms. This should be off by default.

If you run ?searchterm or /searchterm you'll be moved to the first instance of those terms, but won't see the rest of the instances in the buffer.

If you want to see all instances, though, you need to have search highlighting on.

I find it distracting most of the time, but sometimes it's useful.

So I have this mapping set in my Vim configuration file:
nmap   :call ToggleHLSearch()

This sets up a mapping for Ctrl-n to turn The highlight search function off and on. It basically runs Escape to enter command mode, and then :call ToggleHLSearch() to turn on the highlight search function.

Another, simpler example.

When I edit other people's writing, I like to jump through the file a few words at a time and review everything.

So I have set the following mapping to jump through the document five words at a time:

imap ,w 5W

That may not seem like a lot of improvement, switching two characters for two other characters, but by using ,w I don't have to move my hands to the number keys.

You can add abbreviations and mappings to your Vim configuration file, the .vimrc in your home directory assuming you're on a Linux or other *nix system, and make them permanent.

We'll get into more mappings and configuring the .vimrc in the next installment!

Mastering the Vim Interface
It's not obvious at first, but you can actually edit two, three, even dozens of files at a time in a single Vim session simultaneously.

You can do this by making use of multiple "viewports" and by using a relatively new feature introduced in Vim 7, tabs.

Let's start with the viewports first. When you fire up Vim with a file, you'll see a single viewport with the file.

Let's say you're editing a really long file and want to be able to make edits in two sections of the file at the same time.

Run :split (remember, you need to hit Escape first) and then you'll see two views of the same file.

That's useful, but how do we get to the old viewport? Simple, enter command mode (hit Escape) and use Ctrl-w and then either the arrow keys or movement keys to move.

The Ctrl-w command tells Vim you want to do a window/viewport operation, and then the movement keys that you already are familiar with (h,k,j,l) will move you around.

For instance, if you have a Vim session running split horizontally with two viewports, you can run Ctrl-w k to move to the upper viewport, and Ctrl-w j to move down.

Or using Ctrl-w Ctrl-w will cycle you through the windows.

If, instead of moving between viewports, you want to move the viewports, you can use Ctrl-w r and Ctrl-w R to rotate windows clockwise and counter-clockwise, respectively.

Seeing multiple views of the same file is moderately useful. But what I find particularly useful is having two (or more) viewports open with different files in each.

Let's say you want to open a new viewport with your httpd.conf in it. Just run :split httpd.conf and then you're in business.

What if you want to return to a single view? You can close the view by quitting the file normally (:q) or by running Ctrl-w c in the viewport you want to close.

Note that Vim is looking out for you, and won't quit an unsaved file when using Ctrl-w c. You'll be prompted to save the file first. Isn't Vim a sweetheart?

Here's a look at all the major commands you'll want to know. Note that there are several valid combinations that do the same thing.

In the interest of brevity, I'm not going to cover very conceivable key combination that works to do the same things.
  • :split or Ctrl-w s will split the Vim view into two viewports, horizontally.
  • :vsplit or Ctrl-w v will split the Vim view into two viewports, vertically.
  • :split filename will split the Vim view into two viewports, horizontally, and open filename in the new viewport.
  • :vsplit filename will split the Vim view into two viewports, vertically, and open filename in the new viewport.
  • Ctrl-w r moves viewports clockwise.
  • Ctrl-w R moves viewports counter-clockwise.
Why do you want to do this all in a single session? A couple of reasons. First, if you're SSH'ed into a remote machine, it can be a hassle to open two, three, or more sessions.

Better to have a single session open. Also, you might want to do things like copy and paste between files -- which you can do easily by opening multiple files in viewports.

Vim and Tabs
The tab interface has made Web browsing so much more user-friendly, why not bring 'em to Vim too? If you tend to work on several projects at once, having tabs set up can be a good way to multi-task without having to see all of your files at once.

If you want to start Vim with more than one file, run vim -p filename1 filename2. This will open each file in its own tab.

Already in a Vim session? You can open a new tab with :tabnew filename to open (or create) a file.
Moving between tabs is dead easy.

To switch to the next tab (next being the one to the right, or wrapping back to the first tab) use gt in command mode. To switch to the previous tab, use gT.

To close a tab you can do a few things. One is to simply use Vim's usual commands to quit a session. If you're editing one file in a tab and use :wq or similar, it will also close the tab.

Or you can use :tabc to close the tab. Again, Vim is watching out for you, and won't quit that session without saving the file.
  • vim -p filename1 filename2 to open multiple files in tabs from the command line.
  • :tabnew to open a new tab.
  • gt to switch to the next tab.
  • gT to switch to the previous tab.
  • :tabc to close a tab.
You can also combine tabs and viewports -- so you can have a session with multiple viewports in each tab, if you like.

Mix and match, Vim is very flexible and if you can think of something that Vim would logically need to do, odds are that it does.

There is, of course, always more. Use Vim's :help function to read more about the various tab commands and functions.

Master the techniques I've covered here, and you're well on your way to being productive with Vim. In the next installments, I'll get into more advanced techniques.

In the meantime, you might also want to check out the vimtutor command. It will walk you through some of the more common functions in Vim, most of which were covered in the 101 guide, and is good hands-on experience.

No comments:

Post a Comment