Vim is the perfect replacement for vi.

Vim includes hypertext help, powerful ctags support, syntax coloring, visual selection, paragraph reformatting, triggered autocommands, and mouse support.

Vim runs on BeOS, every sort of UNIX (including Linux and FreeBSD), microsoft windows95, NT, MSDOS, OS/2, the Macintosh, and the Atari ST. It was originally created for, and still runs on, the Commodore Amiga. There is a port for Acorn RiscOS in the works.

Handy VIM cheatsheet!

IN COMMAND MODE (hit the escape key, friend!)

Entering text:

i -- insert text at the cursor
a -- append text after your cursor
R -- overwrite the characters that your cursor falls on

Deleting text

x -- delete the character your cursor is on
dd -- delete the line your cursor is on

Saving the file

ZZ -- saves the file!

Exiting without changing the file

:q! -- it's your Get Out of Jail Free Card!

Stupid VIM tricks


Multiple yank buffers.

The way this is used is by preceeding a yank/paste command with "(buffer key). That is: the double quote key, and then any other key. The latter key is the buffer you are yanking into. You refer to this same buffer when you want to paste.

For example: ayy (yanks the current line into both the 'a' buffer and the default buffer) jjyy (moves down two lines and yanks that line into the default buffer) aP (pastes the contents of the "a" buffer after the current line)


Block indenting.

In UltraEdit (for Win32) you could select a block of text and then use tab/shift-tab to indent/unindent it. In vim, this is accomplished with the >/< keys.

Use it with a movement to do blocks of text.

For example: >] (indents the paragraph)


Reformatting paragraphs.

I forget how to get VIM to format paragraphs nicely by default. But even then, it can sometimes get whack. You can force it to reformat by using "gq-(motion command)"

For example: gq] (reformats the paragraph)


Searches are movements.

You can use a search (using "/" or "?") as a movement.

For example: d/> (delete to the end of a tag), and d/} (delete to the first curly)

A brand of scouring powder containing bleach.

You can still get it but it isn't used much any more as most modern surfaces don't take too kindly to being scoured.

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 User-Hostile

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.

Vim Takes a Long Time to Become Proficient With

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:

  1. Downloaded and installed Vim from its website at www.vim.org
  2. 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)
  3. 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.

Vim is Modal

"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.

Regular Expressions

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!

A pleasant form of over engineering, vim is a rather odd but ultimately useful text editor. If you use text editors to do a lot of work (eg you are a writer or programmer) then vim is a tool worth mastering. The advantages of vim over an ordinary text editor is that it is as concerned for editing as it is for typing, and all without moving your hand from the touch typing position.

So editing is performed by keys or chords of keys. A key press in normal mode does something like move the cursor or search for the current word, jump to a previously marked position etc. This doesn't sound as impressive as it is. Because people do the same dam thing in a text editor a lot, for example jump to a previous spelling mistake and look at options, these series of key presses become lodged in muscle memory like walking or changing gears while driving. You just do it, so movement keys become more like an alternative language, but only after a bit of use. Until then, it is more of an intellectual curiosity.

While there is a lot to learn, you will make up the time from efficiency, but more importantly, when you are doing something otherwise dull, you will get distracted to learn how to do something obscure, but useful rather than manually changing a lot of indentation. This leads to a sense that you're actually present infront of your computer rather than simply zoning out. So a major argument is really that it is just a nicer user experience in the long run. Nothing is repetitive and everything is a puzzle. What constantly surprised me was that there was some obscure setting to speed up a little detail, and that attention to detail was on some level nice for its own sake.

Another advantage is to avoid the mouse hunch where you lean to the forwards to one side. Granted sitting in front of a keyboard does not do wonders for your posture, but if you're editing pages of text/code a day then you do spend a lot of time hunching over and jumping between mouse and keyboard. With vim, you are in the touch typing position and your hands make the minimum of movements. That is not to say you are lazy. When I type it is generally a little frenetic as I try to get the idea out of my head before it disappears, but with vim this equally applies to editing as well so you can perform more subtle edits over a fixed period of time.

There is a nice fluidity in keeping your hands close to where they are about to be useful which avoids irritating little pauses while your hands catch up moving to the appropriate position. This is taken to absurd levels for example when you realize the most irritating thing is that you have to push the escape key (and use instead), then move to the really strange level of using -m for return and -h for backspace. But don't rush these things, their is a constant learning process where you learn the next best time saving step. And your muscle memory for keys will become sufficiently automatic for it to feel natural rather than contrived or forced. Incidentally, and I don't know if this is fatal for future back problems, it offers a realistic way of editing with a laptop on a couch as their is no annoying scrolling with the touch pad, so you get more variety that way too.

So vim is in the long run far faster and intuitive to use. It is more interesting in the ways you waste your time and is ultimately a nicer experience which puts less strain on your hands. A sign that I was 'ready' to move to vim was when I preferred shortcut keys for things like save , find or move a word at a time in notepad. So if you're getting board writing a lot of text, give vim a try, but remember to be very patient for the first few times and play through the tutorial.

Vim (?), n. [L., accusative of vis strength.]

Power; force; energy; spirit; activity; vigor.

[Colloq.]

 

© Webster 1913.

Log in or registerto write something here or to contact authors.