oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

What Is Vim (It's Easier than You Think)
Pages: 1, 2, 3

The Vim Tutor

By now, you should have a good feel for why Vim's modal behavior might be useful to you, and after a little fiddling around, you should be able to use it if you ever find yourself in need of a text editor during a terminal session. We could talk keystrokes all day, but you may not have known that you already have one of the greatest Vim tutors (ever) installed on your machine that you can already access by simply typing vimtutor in Terminal. What comes up is basically just a plain text document with a lot of tutelage in it that you can follow along with. There's a ton of incredibly useful learning in there, and this is definitely the next place to turn if you want to continue on your way to proficient Vimming.

If you're even mildly interested in leveraging Vim for more of your work but just can't justify the gross inefficiency you feel like you'd suffer initially, try taking it in steps. Not only would doing a cold-turkey switch be extremely frustrating, it might also be a bad career move if you spend most of your day editing text. Try starting out by using Vim to write some small text files or scripts and gradually move up to more complex tasks. As your exposure increases, so will your proficiency. You'll eventually reach the point where you'll decide whether or not it's worth your trouble (and for some folks, it won't be).

Customizing Keystrokes in Vim

While efficiency may be the initial reason you turn to Vim, customization will no doubt be the reason that you won't look back. Let's dive into a quick example of how easy it is to customize keystrokes in Vim. Your .gvimrc and .vimrc files are where you'll do your Vim customization, so if you tend to work on multiple machines, just keep these files up to date and in sync in each of your home directories and you'll never miss a beat. As you might have guessed, settings that generally apply to Vim's GUI generally reside in the .gvimrc file, while all other Vim settings such as remapping keystrokes reside the .vimrc file.

Vim 7.0 introduced the ability to use tabs for working with multiple documents in a single application window. Let's remap the keystrokes for tab-related actions to show off some of Vim's flexibility. The built in commands for invoking and navigating tabs from within normal mode follow:

  • :tabnew: Create a new tab
  • :tabnext: Show the next tab
  • :tabprev: Show the previous tab
  • :tabclose: Close the current tab

Take a moment to try out these commands before pressing on.

While these commands are easy enough to remember, let's shorten them to something less verbose. With some help from Vim Tip 1221, we can do this quite easily. Open up your .vimrc file by typing gvim ~/.vimrc in Terminal, navigate to the bottom of the file, and type in the following:

" tab navigation adapted from vim tip 1221
nmap th :tabprev<cr>
nmap tl :tabnext<cr>
nmap tn :tabnew<cr>
nmap tc :tabclose<cr>

Although you can read about the map command here or by typing :help nmap in Vim, you may find this link to be a much less intimidating overview. In a nutshell, nmap takes two terms, and simply executes the second term whenever you type in the first term when you're in normal mode. For example, when you type th in normal mode, Vim will now act as though you'd typed :tabprev followed by the Return key.

Thumbnail, click for full-size image.
Figure 2 (Click for full-size image). Try experimenting with your .vimrc file, try out Vim 7's new tabs, and split up your view as you journey toward Vim glory.

Pressing g Ctrl g (try it in normal mode) just made me realize that we're running out of space here. Hopefully, you now know enough about Vim to make an educated decision about whether or not it can help you get some work done and whether or not it's worth the initial effort involved. Spend some time browsing the tips on and spend some time in Vim's comprehensive help system by typing :help in normal mode to bring up a complete listing. If you still feel like you're at a loss for moving on, consider these ideas:

  • Try using the :split and :vsplit commands to open up multiple documents or view different parts of the same document. Ctrl w Ctrl w moves the cursor throughout the different views
  • Read about Vim 7's built in spell checking by typing :help new-spell.
  • Read about Vim 7's intelligent completion (intellisense) by typing :help new-omni-completion.
  • Read about everything else new in Vim 7 by typing :help version7.
  • Watch this graphical introduction to Vim 7.
  • Try using exuberant ctags with Vim to get more bang for your buck when editing source code.

As always, you can talk back below to share tips and relate your experiences with Vim.

Matthew Russell is a computer scientist from middle Tennessee; and serves Digital Reasoning Systems as the Director of Advanced Technology. Hacking and writing are two activities essential to his renaissance man regimen.

Return to the Mac DevCenter.