macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

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

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
Sam-I-am
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.

Pages: 1, 2, 3

Next Pagearrow