Vim Bits 1
26 Jun 2002
Note: I'm assuming you're using Vim in a terminal (xterm, gnome-terminal, konsole, the Linux console, etc.). Everything here should apply to Gvim (the GUI version) as well.
Milk Before Meat
The Two Philosophies
The software world seems to be dominated by two main camps: the keyboard people and the mouse people. Keyboard people usually come from DOS or Unix backgrounds and prefer terminals to GUI apps almost always. Mouse people usually come from Macintosh or Windows backgrounds and prefer point-and-click. If you're a mouse person, Vim probably isn't for you. Try it and see if you like it, but expect to be frustrated. If you're a keyboard person, however, why aren't you using Vim? (If you are, pat yourself on the back.)
The Two Editors
Anyone who has spent some time in the Unix world has heard of the infamous vi/Emacs holy war. Vi zealots claim that Emacs is an operating system with a text editor attached. Emacs fans have issues with vi's modes. This article is obviously about Vim and not about Emacs, so we'll leave the battle behind us with one more remark: why Vim?
Why Vim?
I could easily just put a list of features here. For some, that's enough. But when you're choosing between editors that have pretty much the same feature set (e.g. Vim and Emacs), that doesn't cut it. Both editors are filled to the brim with features; both editors are fast; both editors are quite extensible. What it really comes down to is simply personal preference. Try both editors and use whichever you like best.
Personally, I prefer Vim because it fits my style (of writing, of coding, of working, etc.) much better than Emacs does. I would also say that Emacs' default keybindings hurt my wrists, but both editors are so extensible that you can use whatever keybindings you want to, so it's not a valid argument. If you like Vim, use it. If you like Emacs, use it. If you like both, use both.
What about the other editors? Again, it's largely a matter of personal choice. If you prefer using the mouse to get the job done, you'll want a GUI editor. (GVim is a GUI editor but it's really meant for keyboard use.)
Does All This Even Matter?
Yes.
Oh, I guess you want an explanation. Sorry about that. :) If you're a programmer, most of your time will be spent in an editor. Even if you're not, chances are good that you'll spend quite a bit of time using whatever editor you've chosen.
But don't all editors do pretty much the same thing? Yes, more or less. But each does it differently. Some are more efficient than others. Vim is extremely efficient, provided that you know how to use it right. Once you learn the skills, you'll be able to get your work done faster and better than before.
Read Bram Moolenaar's Seven habits of effective text editing (Bram is Vim's author).
Getting Started
Introduction
If you're just starting out with Vim, begin with vimtutor (type "vimtutor" at the command-line prompt and follow the instructions). Then get familiar with the help system. This is crucial. The Vim documentation is very extensive -- if you don't know how to use it, you're crippling yourself. At the very least, skim through the user manual so that you'll have an idea of where things are when you need to find them.
First Things First
Note: whenever you see the instruction "Type 'x'", you need to be in Normal mode unless it explicitly says to switch to Insert mode. Just hit Escape to get there.
1. How do I start Vim?
Type "vim" at the prompt. (Type "gvim" if you want the GUI.)
2. How do I exit Vim?
Type ":q". If you've made changes to the file but don't want to save them, type ":q!". (The ! tells Vim to ignore the changes.)
3. How do I start typing?
Type "i". This puts Vim into Insert mode. You can type now as usual.
4. How do I save my file?
Type ":w".
5. How do I save and quit at the same time?
You have two options here: ":wq" and "ZZ". The only real difference between the two is that ZZ will only save your file if there have been changes made to it. ZZ is also one less keystroke than :wq, saving you some time. (Yes, not much, but it adds up.)
6. How do I open a file?
Type ":e filename".
7. How do I create a new file?
Type ":n filename".
8. How do I save my file under a different name?
Type ":w filename".
9. What if Vim says the file is read-only?
Try typing ":w! filename". If that doesn't work, check the permissions on the file.
10. Oops, I made a mistake. How do I undo it?
Type "u".
11. Oops, I hit undo too many times. Can I get my changes back?
Type Ctrl-R.
12. Somehow the screen got all messy. How do I clean it up?
Type Ctrl-L.
Help!
1. How do I get to help?
Type ":help".
2. There are all these different |usr_05.txt| things but I don't know how to click on them.
To follow a link, move the cursor over it and hit Ctrl-]. To move back, hit Ctrl-T. If "set mouse=a" is in your .vimrc, you can also click on the link with the mouse. But you really should know the keyboard commands for moving through the help system.
3. How do I get help on a specific command/option/whatever?
Type ":help command-name". You can start typing in the name and then hit Tab to have Vim try to complete it.
4. Is there a list of available commands?
Type ":help index".
5. Vim says I can only use Perl (for example) if the +perl option was set when it was compiled. How can I tell if it was set?
Type ":ver" and look for "+perl". If it wasn't set, it will show up as "-perl".
Choices, Choices
1. How can I see what options are set?
Type ":set".
2. How can I see what options are available?
Type ":options".
3. How can I set an option?
Type ":set option-name". If you want to set it permanently, remove the colon (i.e. "set option-name") and put it in your .vimrc.
4. How can I unset an option?
Rather than having an "unset" command, Vim prefixes the option name with "no". For example, to tell Vim not to expand tabs, you would type "set noexpandtab" rather than "unset expandtab".
Basics
Modes
1. What's with all these modes? Why can't I start typing right away?
There are six modes in Vim: Normal, Insert, Visual, Command-line, Select, and Ex. You really only need to know the first four, but it doesn't hurt to learn about the other two. Normal is the default mode, used for commands. Insert mode lets you type text into the buffer. To select text, use Visual mode. Command-line mode is used mostly for searches and ":" commands. Type ":help vim-modes" for more information.
2. Aren't modes stupid?
The original vi was written for a world that doesn't exist anymore, according to Bill Joy, the author of vi. Modes don't seem like such a good idea anymore. They certainly make the learning curve much steeper than for modeless editors. And yet modes don't seem to get in the way of vi gurus.
3. How do I switch to Normal/Command mode?
Hit Escape.
4. How do I switch to Insert mode?
There are a number of commands to switch to Insert mode. "i" will place the cursor before the current character. "a" will place the cursor after the current character. "o" will create a new line beneath the current one and place the cursor at the beginning of that line. "I", "A", and "O" will also place you in Insert mode -- read ":help inserting" for more details.
5. How do I switch to Visual mode?
"v" allows you to select text by character. "V" allows you to select text by line. Ctrl-V allows you to select text in a block (try it out if that doesn't make sense).
6. How do I switch to Command-line mode?
Type one of the characters that starts a command-line expression (":", "/", etc.).
7. How do I tell Vim start out in Insert mode?
Put "set insertmode" in your .vimrc.
Movement
1. Why are h/j/k/l used for moving around?
Because it's faster. The less you have to move your hand, the better. (The home row keys are underneath your fingertips, whereas the arrow keys are off to the side.) The one difference between the home row keys and the arrow keys is that the latter can be used in Insert mode, but the former can't. This seems like it would get annoying, but the idea is that Insert mode is used only for inserting text. If you want to move around the file, you switch to Normal mode. The more you do it the more natural it will feel.
2. Why are there so many other movement commands?
Efficiency. Most Vim gurus say that the arrow movements (either h/j/k/l or the arrow keys) and Page Up/Page Down are not the best way to move around. You won't need to use all of the other commands, of course, but the more you know, the quicker you'll become. Type ":help motion.txt" for more information on all the ways to move the cursor.
3. Are there any shortcuts for Page Up and Page Down?
Ctrl-B and Ctrl-F are closer and do the same thing. By default they only work in Normal mode, but you can remap them to work in Insert mode if you wish.
4. What if I only want to scroll half a page?
Ctrl-U and Ctrl-D scroll half a page up and down, respectively.
5. Can I scroll down without moving the cursor?
Ctrl-Y and Ctrl-E scroll up and down, respectively, without moving the cursor.
6. When I scroll, I like to be able to see a few lines below where I am. Can I do this in Vim?
":set scrolloff=2" will show you two lines beyond the cursor's location (and above it when you're scrolling up). Put "set scrolloff=2" in your .vimrc if you want to make this permanent. (The number is up to you, of course; I prefer two lines, but you may want more or less.)
7. Can I move by word?
"w" will move to the beginning of the next word. "b" will move to the beginning of the last word. "e" will move to the end of the next word. Any of these can be prefixed with a number (e.g. "3w") to repeat the movement ("3w" means "move the cursor three words forward").
8. Can I move by sentence?
")" will move to the next sentence and "(" will move to the previous sentence.
9. Can I move by paragraph?
Assuming the paragraphs are separated by blank lines, "}" will move to the next paragraph and "{" will move to the previous paragraph.
10. How do I move to the beginning of the line? The end?
"0", Home, "^", and "1|" will all move to the beginning of the line, but each is slightly different. Read ":help left-right-motions" for more detail. Type "$" to move to the end of the line. You can also use the "g" commands ("g0", "g$", etc.).
11. How do I move to the beginning of the file? The end?
"gg" or "1G" will move to the beginning. "G" will move to the end.
12. How do I move to a specific line number?
Type ":#" or "#G" (where # represents the number). E.g. "125G" will move to line 125.
13. Can I move to different parts of the screen?
"H" will move to the top line on the screen; "M" will move to the middle line; and "L" will move to the bottom line.
14. How do I move between open and close braces when programming?
"%" will move between opening and closing braces (curly braces, parentheses, and brackets).
15. I've selected some text in Visual mode. Can I easily move to both the top and the bottom of the selection?
Type "o" to move to the top and bottom of the selection.
Coming in future parts:
Buffers and windows, copying and pasting, search and replace, formatting text, maps and abbreviations, using Vim for programming, Vim scripting, digraphs, Unicode, folding, .vimrc, and much, much more.

