What Is Vim (It's Easier than You Think)

by Matthew Russell

Editor's note: If you've had a less-than-agreeable experience with Vim in the past and are willing to give it another shot, please ponder these points as you work through this article: 1) there's generally a learning curve involved with any powerful tool, 2) Vim is much more than just your run-of-the-mill text editor, and 3) the effort required to become a proficient Vimmer isn't as much as you'd think: it's comparable to that of learning to touch type.

Vim (Vi IMproved)
Vim is a modal text editor designed expressly for efficient text editing. Vim behaves much like the Unix text editor Vi but with many improvements, including graphical menus and mouse support, syntax coloring, undo/redo support, tabbed views, spell checking, and more. Vim is commonly known as a programmer's text editor because of its powerful features that are convenient for editing source code, but is suitable for any task that involves editing text. Vim is open source and highly portable, making it especially appealing to those who frequently work amongst multiple platforms.

In This Article:

  1. Download and Install
  2. Normal Mode Isn't so Normal (at First)
  3. More Keystrokes: Cut/Copy/Paste and Search
  4. The Vim Tutor
  5. Customizing Keystrokes in Vim

This article gets you up and running with Vim. Read on get the latest version installed, run though a quick overview of how its modal behavior can increase your efficiency, and then delve into some of the more advanced features that you can use to customize your Vim experience.

Download and Install

Vim.org is the undisputed source for all matters Vim. It houses a massive collection of incredibly useful Vim tips, scripts, and documentation, among other things. An interesting anomaly, however, is that you won't find the precompiled Vim 7 binaries for Mac there. To get those, you'll want to go to macvim.org. Go there, and look toward the bottom of the page to find precompiled binaries of Vim 7. Download the archive corresponding to the version of your OS, and unzip it. Inside, you'll find four files: gvim (a shell script), Vim.app (the GUI-enabled Vim application), gvim.app (the helper application that you'll want to use to launch Vim), and the standard README document.

Follow along with this much-expanded version of the README document to get all set up.

Brief aside: It's worth noting that Vim.app and gvim.app do not have the intuitive meanings that "vim" and "gvim" normally have. Traditionally, "vim" refers to the terminal-based version of Vim, while "gvim" normally refers to the version of Vim that runs as a GUI application with menus, mouse support, etc. The current situation for Mac users is that Vim.app is the single application that drives both the GUI-enabled version as well as the Terminal-based version of Vim. The disconnect, however, is that Vim.app is only capable of providing a single document window per application instance (at least for now). Until this issue is resolved, the gvim.app helper application works around the problem by launching separate instances of Vim.app behind the scenes for you. All that really means in the end is that if you want to invoke Vim from outside a Terminal setting (like from your dock), do it with gvim.app, not with Vim.app. Otherwise, you're stuck with a single document window. If you didn't understand a word of that, then just forget about it all except the previous sentence.

Normal Mode Isn't so Normal (at First)

Open up Vim either through the gvim.app icon in your Application folder or by typing gvim in Terminal and notice that you get what appears to be a standard Mac application. You may be dismayed to notice that you can't just arbitrarily start typing into the editor. Actually, it's worth quickly digressing to explain why such a trivial task requires any explanation at all. What makes Vim different from an editor you may have used before is that Vim is modal. The mode that you're undoubtedly most used to from prior experience with modeless text editors would be analogous to Vim's "insert" mode. Insert mode allows you to freely type in text, but when you just opened Vim, it didn't automatically switch to "insert" mode. Instead, it opened in "normal" mode. What's normal mode? Well, I'm glad you asked.

Figure 1
Figure 1. The GUI-enabled version of Vim behaves much like a normal Mac application

In normal mode, Vim expects you to provide it with a command, which potentially has an operator. A common command you'll no doubt want to use is the "insert" command, and one way that you can enter that mode by simply pressing i (just the single keystroke) when you're in normal mode. In insert mode, you can type freely as you would in any other text editor, and with the particular graphical version of Vim that you're using, you can even move the cursor around the screen by clicking the mouse. To get back to normal mode and leave the world of insertion bliss, simply press the Escape key. At this point, it's noteworthy that pressing the Escape key will always get you back to normal mode from insert mode.

Obviously you realize that you'd be primarily typing text in insert mode, but what sorts of things can you do from normal mode? Let's type in some text and then run through a quick overview of some common operations. Go back into insertion mode by pressing i and type the following:

I do not like them
I do not like
green eggs and ham

Once the text is in the editor, save the document to your desktop by pressing Escape to go back to normal mode and then typing :w ~/Desktop/greenEggs.txt. You guessed it: w stands for "write," which we more commonly think of as "save." After saving your document, get your cursor back into the upper left hand corner, but don't do it by using the arrow keys. Type h to move it left, l to move it right, j to move it down, and k to move it up. That may seem confusing at first, but it really does make sense because it mimics the way the arrow keys are laid out. As you'll soon find out, avoiding the arrow keys can save you more time than you'd think--especially once you've learned a few other time-saving keystrokes. This point illustrates up a maxim from the mothership itself over at Vim.org: "Vim is a highly configurable text editor built to enable efficient text editing." Efficient isn't always convenient--at first--but touch typing wasn't efficient at first either, was it?

So if you were still wondering why you'd want to bother with a modal text editor at all, that's a huge part of it--efficiency. Sure, Vim is portable, flexible, powerful, and lots of other things that we hopefully will not digress into editor wars about--but truly, the one thing Vim is known for foremost by its users is efficiency. If you barely do any typing at all and/or love taking your hands off of the keyboard to double-click the mouse, then you may not ever want to use Vim. Seriously. If, however, you do a lot of typing (maybe even in terminals, across multiple platforms/systems that generally don't have a standard text editor installed for whatever reason), then I suggest you keep reading. It's usually in these cases that Vim is a godsend. But you're not reading because you want an alternative way of moving the cursor around the screen, so let's move on.

More Keystrokes in Vim: Cut/Copy/Paste and Search

Let's investigate a few other commands in our brief preview of normal mode. Once you've gotten your cursor into the upper left-hand corner of the screen, press dd to delete the entire line. Now, suppose you didn't actually mean to do that--just press u to undo it. You can press d1 to delete the current line and the one below it, d2 to delete the current line and the two below it, and so forth. Whenever you delete something, it automatically goes into the default buffer for pasting, so you can press p to paste it after the cursor or P to paste it before the cursor. Additionally, pressing x deletes a single character at a time, dw deletes the current word, and d$ deletes everything from the cursor to the end of the current line. Again, anything you delete goes into the default paste buffer, so you can just move the cursor and press p or P to paste it back in.

These keystrokes may seem cryptic at first, but these sorts of things actually become pretty easy to remember after a little bit of practice, because each character in a command often generalizes to other commands. For example, d refers to the delete command; what's deleted depends on the next key you press. $ refers to the end of the line, so you could press d$ to delete the rest of the line. Alternatively, you can press $ by itself when you're in normal mode to advance the cursor to the end of the line. Sure, you could just use the mouse--but staying on the keyboard is generally faster, and if you're using a terminal version of Vim, you don't have the luxury of a mouse anyway. As another example of the command syntax in normal mode, dw deletes a word, but w by itself advances the cursor one word at a time. Starting to see how it all pans out?

Now try using y (yank) in place of d in all of those commands. You'll see that while d behaves like Cut, y behaves like Copy. No mouse-clicking required! Read this for more on cut/copy/paste operations, including how to cut/copy into named buffers and how to invoke Vim's visual mode for these operations.

One other quickie that's a nice display of Vim's efficiency is Vim's search command. When in normal mode, type / followed by a word or phrase you want to find (like I) and then press the Return key to have the first occurrence of it after the cursor highlighted. Press n to find the next occurrence, or press N to find the previous occurrence. Pressing # over a word automatically highlights every instance of it in the document and allows you to use n and N to jump back and forth between them. Some things can only get so simple.

Whenever you want to quit out of a document, use the :q command. Likewise, you can save any changes and quit the document all at the same time, by typing :wq, or quit without saving changes with :q!. The ! overrides the prompt to save that you'd normally receive if you have unsaved changes.

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:

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 Vim.org 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:

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.

Copyright © 2017 O'Reilly Media, Inc.