DAY TIME, EXTERIOR. A PARK. TWO SOFTWARE DEVELOPERS ARE CHATTING
OVER HAM AND CHEESE SAMMICHES.
SPIFF: The thing is, I just can't find a happy medium.
I hate using programs like Dreamweaver to make websites, but
it feels silly to do it in Notepad!
CAESAR: Maybe you should try Vim.
SPIFF: What's Vim?
CAESAR: It's a really powerful text editor--it makes some common
text editing tasks (like search and replace) really easy, but it's
kind of tough to learn. It has lots of neat features, though:
it automatically color-codes most programming languages for you,
and there are lots of time-saving keyboard shortcuts.
SPIFF: What makes it so difficult to learn? It sounds great.
CAESAR: Well, for one thing, you have to sacrifice a goat every
time you save a file.
SPIFF: That's a hassle! Goats take forever to requisition from
the company.
CAESAR: Yeah. It's aimed primarily at developers and voodoo magicians,
but normal folk who spend a lot of time word-processing might like it, too.
Vim is more than the sum of its parts. Vim is not just an editor. Vim is a unique approach to the practice of text editing.
Vim is as user-friendly as an alligator on steroids. Vim is a tool that you must learn to use. Vim can help everyone, even casual users. If you try to use Vim without reading the instruction manual, your toaster will light on fire and your dog will eat your cat.
Vim partially embodies the Unix-centric philosophy of functionality over verbosity.
Before Vim, mountains are mountains and rivers are rivers.
During Vim, mountains are no longer mountains and rivers are no longer rivers.
After Vim, mountains are mountains and rivers are rivers.
- Excerpt, "Zen and the Art of Dear God How the Fuck Does This Thing Work"
Like Chess, you can learn the basics of Vim very quickly. In fact, this writeup is written assuming that you have already completed the following steps:
- Downloaded and installed Vim from its website at www.vim.org
- Sat through the first few chapters of vimtutor (accessible by typing "vimtutor" into a Linux terminal, or running "Vimtutor" from the Windows Start -> All Programs menu)
- Used Vim for about five minutes and thirty three seconds before exclaiming, "How is this better than fricking Notepad?" and closing the program exasperatedly.
There's a lot to Vim, but once you begin to understand why it was built the way it is, things will start getting easier to remember. The key to learning Vim is using Vim. Open it up in a separate window and try stuff out as you read this writeup.
Just like when you learn a new language, you won't be able to express yourself as clearly when using Vim for a while. This means that if you need to get work done as quickly as possible at this point, you shouldn't start learning Vim. Wait until you have some downtime and a project or two that you can tinker with, and use it as an opportunity to start learning Vim.
"What the shit is going on?"
- A New Vim User
Vim is a text editor. Therefore, when you press a key, the corresponding character should appear in your document. Right? WRONG! Vim has different modes. Why?!
Because text-editing is an indecisive activity. When you write or code, you move stuff around. Add a word two sentences ago, delete the rest of this sentence, replace every instance of the word "party" with "sextravaganza", change a list of "Smith, John"s into "John Smith"s. In mortal, mundane text-editors, these activities are tedious and time-consuming; you are stuck at your keyboard when you could be shaving hedgehogs or perving on your best friend's sister. In Vim, switching between modes makes different tasks easier and faster. Vim gives you the freedom to be who you want to be.
Vim Can Be Used Wrong
Because of its esoteric usage design, it's very possible to use Vim incorrectly. Keep that in mind as you learn to use it. Ask yourself: "Am I using Vim the way it ought to be used?" Fortunately, there are more right ways to use Vim than wrong ways. I will get you started on the path to righteousness. A good rule of thumb is, "If my keystrokes appear to be a string of unintelligible gibberish, I'm on the right track."
The Most Important Vim Feature
:h
That's it. :h is for help. It can also be typed as :he and :help. :h can be used to get help on any feature of Vim. Let's say you want to get help on the "substitute" command, which you run in Vim by typing :s. OK, so we type ":he substitute"... oops! That comes up with the documentation for the built-in substitute function, but we want the : command. The solution?
:h :s OR :help :substitute
This tells Vim that you're interested in the : command. Remember: Vim will help you with anything! Use :close to close the help windows.
Using Vim Right
The Keyboard
If you're a touch typist, Vim is for you. It is built to facilitate keeping your fingers on the home row, and during proper use of Vim, you should never need to touch your mouse. Navigating around your document is fast and easy when you use Vim's commands.
Modes
Vim has four primary modes: normal, insert, visual and command. You should be spending most of your time in normal mode. That's why it's called normal. When you decide you need to type something, hit i to enter insert mode, and when you're done, hit Escape or Ctrl + [ to get back into normal mode.
insert mode is Vim's kryptonite. Once you're in insert mode, you've stripped Vim of its superhuman powers and left the world defenseless against Zod. Spend only as much time in insert mode as you have to! When you're in normal mode, you have access to the full range of Vim's impressive features, and I will show you a few of their highlights now.
Text Objects & Word Motions: Vim Makes Indecision Easy
Vim is built strongly on the idea of manipulating "text objects"--words, sentences, paragraphs, blocks of code, and so on. Henceforth, let it be known that the asterisk (*) in my examples represents the position of your cursor. Suppose we are editing the following text:
Why don't you make like a wang tree and get the fuck out of here?*
"Oh bollocks," we say, "I don't know how that 'wang' snuck in there." In a normal text editor, you might hold the left arrow and watch the
Lord of the Rings trilogy, uncut, while your cursor meanders towards the offending
penile euphemism. Or perhaps, if you're a cheeky monkey, you'd hold "CTRL" so it skipped a word at a time. Or
maybe, if you're a heretic, you'd lift your chubby hand off of your Cheetoh-encrusted keyboard and use the mouse. I'm here to tell you that all of these techniques are for
failures.
In Vim, there are two compelling choices to backtrack. The first is the word motion b, which bounces your cursor back one word. Yes, yes! I already hear you cheeky ones: "How is that different from holding 'CTRL'?" I SHALL ENLIGHTEN YOU. The key is counts. You can supply a count to any word motion, just by pressing a number before you enter it.
In this sentence, wang is nine words behind our cursor, so we would hit 9b, INSTANTANEOUSLY transporting us to the right word. Mother of God, we are truly in the future.
Once your cursor has teleported, ninja-like, to the appropriate word, you can type dw, or "delete word", to get rid of it. Or, suppose we're feeling vindictive, and want to delete every damn word up to and INCLUDING that sonuvabitch. We can hit ^ or 0 to zap us to the start of the line, and type d7w, instructing Vim to annihilate the next seven words. (Another alternative would be to type d/tree and hit ENTER, which would use the search command / to find everything up to "tree".)
There are many useful word motions:
- s for sentence,
- p for paragraph,
- w for word,
- W for WORD, which includes special characters like commas,
- t for HTML tag (!),
- ( for parenthetical block,
- " for double-quote-delimited text,
- and similar ones for 's, [s and {s.
Learn them and be blissful.
Visual Mode
visual mode is the equivalent of clicking & dragging. You can hit v to enter visual mode, or hit V to enter visual line mode, where whole lines are selected at once.
Once in visual mode, you still have access to all of normal mode's navigational sugar, including the ubiquitous / command for search, word motions, and so on. You can also still use all of Vim's commands (d, c, y) but aimed at the current visual selection.
Learn Once, Use Forever
Once you learn part of Vim's functionality, like word motions, you can re-use them in many different places. Try out everything, and you'd be surprised with what works.
One Goal, Many Paths
There are dozens of ways to accomplish any particular text-editing task in Vim. All you need to do is learn a few that make sense to you, and you'll be well on your way to hauling ass.
:substitute
:s is search and replace. You can run :%s/lame/awesome/g to replace every instance of 'lame' in the file with 'awesome', where % tells Vim to look through the whole file. If you have a visual selection active, hitting : will automatically prepend whatever you type with `<,`>, which will restrict your search to within the current selection.
Vim supports regular expressions, which you should learn as much about as possible. Take the following file:
Smith, John
Gates, Bill
Jordan, Michael
King, Stephen
Monster, FlyingSpaghetti
Only pretend it's six thousand lines long, and pretend you have to change the names to "FirstName LastName" format in the next thirty seconds, or your shitty hacker movie will bomb. With regexes, we have a terse, but powerful, way of telling Vim what to do: "Whenever you see a word followed by a comma, a space and another word, replace it with the second word, a space, and the first word."
In regex-land, "a word" can be expressed like this: [a-zA-Z]*. Essentially, this means, "any character between a-z and A-Z, any amount of times". We also have to tell Vim to pay special attention to the words, so we'll enclose them in magical parentheses: \([a-zA-Z]*\).
Here's our final product:
:%s/\([a-zA-Z]*\), \([a-zA-Z]*\)/\2 \1/
The \2 \1 tells Vim to replace the pattern with the second parenthized phrase, followed by the first. Copy and paste that file and that command into your Vim, and you'll see the magic happen.
More to Come
More sections will be added soon, by whim or request. In the meantime, check out the help pages for registers.
If anything in this writeup is unclear or confusing, please /msg me so I can rewrite it!