top | item 2936670

Learn Vim Progressively

605 points| yogsototh | 14 years ago |yannesposito.com | reply

113 comments

order
[+] dschobel|14 years ago|reply
Damn, I've been using vim for almost a decade and didn't realize that /<term> worked as a movement so you can do things like y2/foo and yank to the second occurrence of “foo” (to use the example from the tutorial).

That's why I scan every beginner vim tutorial that comes across hn. I always learn something

[+] billybob|14 years ago|reply
Took me years to learn that one, too. It may be favorite Vim trick now.

Basically everything that can make your cursor jump can be used as a motion with a command - moving to a mark, undoing/redoing jumps with Ctrl+o and Ctrl+i, and hopping to a matching brace with % are more examples.

Wrote a short intro myself about such revelations: http://nathanmlong.com/vanquish/

[+] saraid216|14 years ago|reply
I gave up pretending that I'm a Vim beginner a year or two ago, but I also continue to skim through and learn from "beginner" tutorials that show up in my stream. People always pick a different set of "basic" commands, and from this one, I learned about "t<letter>", which I hadn't known before.
[+] Estragon|14 years ago|reply
Same thing works in emacs. Incremental search sets the mark at point of initiation, so you get the same effect with C-s foo C-s C-w.
[+] Ideka|14 years ago|reply
Funny. I found out about that early, merely experimenting.

I rarely use it nowadays tough. I used to use it for stuff like "delete up to the next dot" and the like. But that was until I learned about f and t. :)

[+] thristian|14 years ago|reply
Here's a Vim trick I only figured out fairly recently. Everybody knows that % jumps between a bracket, brace or parenthesis and its matched pair, but what happens if you hit % while the cursor isn't on such a character? Turns out, it searches forward until it finds such a character, then jumps to its matched pair.

So for example, let's say you had a nested function invocation that was getting long and unwieldy and you wanted to break it out onto its own line:

    foo = makeFoo(
            globalConfig.getParam(
                "FooSize",
                int,
                default="37",
            )
        )
If you put the cursor on the 'g' at the beginning of 'globalConfig' and press "d%", it will cut the function name and all the parameters in one action.
[+] mathias_10gen|14 years ago|reply
ci( is another nifty trick that will do the same but will work anywhere between the two ('s not just at the beginning of globalConfig. See ":help text-objects" for more details on one of the more powerful feature of vim.
[+] mnazim|14 years ago|reply
It can also be used with y and c(y%, c% etc.)
[+] agscala|14 years ago|reply
That is a neat trick. Even though it's one more character, the expected way to do this would be: "dt("
[+] beaumartinez|14 years ago|reply
Nice tutorial—better than many.

However, these kind of tutorials always fail to mention the number one way to learn Vim:

    vimtutor
and

    :h usr_02.txt
Those two (and successive pages in the user manual) will teach you practically everything about Vim—and they're included right in Vim.
[+] rfrey|14 years ago|reply
From the article:

"Fortunately vim comes with some very good tools and an excellent documentation. Run vimtutor until you are familiar with most basic commands. Also, you should read carefully this page: :help usr_02.txt."

[+] hasenj|14 years ago|reply
vimtutor is actually slow and boring
[+] d0m|14 years ago|reply
Actually "cw" doesn't change the current word.. it changes where the cursor is to the end of the word. Something I tend to use a lot is: (| is the cursor)

  ciw (Really change the current word. "Fo|o Bar" -> "| Bar"
  ci" (Change in between ":  "Test 12|34" -> "|"
  da" (Delete in between " AND the "":  a"Test 12|34"b -> ab
Also, plugins are extremely important. For instance, one of my favorite makes the "w" smarter for day to day programming word. (I mapped it to ,w) For example:

  "pac|kageManager" ci,w "|Manager" 
  "pac|kage_manager" ci,w "|_manager"
[+] beaumartinez|14 years ago|reply
Exactly—use text objects.

    :h text-objects
To be consistent with dw you can use caw which will delete until the next word, not just the current word.
[+] d0m|14 years ago|reply
Oh, and the surround plugin is so useful.. something I tend to do a lot is:

  viws' : viw "highlight the current word" 
          and s' surrounds it with ''. *Soooooo* useful. 
          You can also do yiws' but I like to see what 
          will be surrounded as something there're spaces
          or other characters which doesn't *count* in
          "w".
[+] tudorizer|14 years ago|reply
I use that da" very often, but for some strange reason it stuck in my brain as abbreviation from 'delete around "', which is inaccurate. Thanks for making me realize this.
[+] yogsototh|14 years ago|reply
Of course, you're right, I fixed that. And many people asked for a text_object section. I'll certainly add it.
[+] majika|14 years ago|reply
Which plugin is that?
[+] ma2rten|14 years ago|reply
Don't do what this guy says! Or at least don't stay in phase 1 for longer than a day. I've had used vim for a few years as a pico replacement, whenever I was on a remote computer on ssh. I picked up some really bad habit, like staying in insert mode all the time and using the arrow, home and end keys. I actually had to deactivate the arrow keys in my vimrc.

I would recommend going thought the tutorial, that comes with vim (vimtutor command) and after that reading those articles:

http://www.moolenaar.net/habits.html

http://www.viemu.com/a-why-vi-vim.html

[+] initself|14 years ago|reply
I'm in the minority camp that doesn't think arrow keys are a big deal, but I suppose I need to qualify that and say "not a big deal if you are on a Thinkpad" and you have an solid reach of curled fingers in your right hand. There's are efficiency gaps in between

  1) keeping fingers on jkl;
  2) manipulating the arrow keys with fingertips, 
     primarily using the pinky for quick movements
  3) lifting the hand to use the arrow keys
and they do add up over time, but I'm not sure how detrimental they are.

Sometimes, the arrow keys provide a nice mental break, where you can step back from the doc and examine what's happened so far with the arrow keys or page up/down.

[+] insraq|14 years ago|reply
Great post. I like the way author splits the big problem into smaller ones and reduce the learning curve by different stages. When I recommend vim to my friends, they are usually frightened by the OReilly's thick vi/vim book and most people cannot "survive" in vim before they give up.
[+] dbingham|14 years ago|reply
My problem with using Vim, and the source of my hesitation to learn it more, is that I'm a fairly haphazard typist. I type fast, but messily. I've never really been able to clean it up. I'm just naturally a little clumsy.

In my IDE, this is never, ever a problem. Most of the dangerous commands require I go to the mouse, so I can't simply blaze through them on the keyboard. Anything I mess up at the keyboard, I can always undo with backspace or a cntl+z. Furthermore, there is visual confirmation of exactly what each command I entered is doing and a chance to cancel it if it's going to do stuff I don't want.

In Vim, this isn't the case. I can be blazing along on the keyboard, attempt to enter a command, screw it up and enter not just one, but a whole sequence of incorrect commands. These commands could be very problematic -- doing things that are hard to undo. And it can be very difficult for me, as a Vim novice especially, to figure out exactly what I screwed up and how to undo it.

I know some of that may be remedied with more knowledge. I'm sure there's some way to view a detailed command history, and I know it has an undo. But I still feel as if, for myself at least, the danger of keyboard mashing screw ups completely counteracts any gains in productivity I'd make. And given that I wouldn't really gain anything by using Vim, I prefer the comfort of my visually based IDE.

Aside from which, I'm really fond of the way Eclipse's windows are dockable and movable and I make frequent use of that feature. Interrupting processes or juggling multiple terminal windows just isn't the same.

[+] humbledrone|14 years ago|reply
What do you mean by "hard to undo?" Every Vim user has, more than once, accidentally begun typing in command mode and done crazy things. The solution is to just hit u (undo) a couple of times.

Granted, some commands have side-effects, (e.g. "w! existing.txt" which would overwrite an existing file), but they are sufficiently complex that the odds of typing one are extremely low. I have been using Vim on a daily basis for over a decade, and I cannot recall a single time that I've accidentally typed an irreversible command.

[+] crux|14 years ago|reply
One thing I would love to see—and which I can only assume exists—is a guide like this, tailored to writing prose rather than code. This article maintains certain assumptions that are held by nearly every other guide, like 'You are always in normal mode, and only enter insert mode for short bursts of typing text, after which you press <Esc> to go to normal mode.' Which is true and useful for coding, but slightly less so for prose—not entirely less so, and it's still a good idea to get in the habit of busting out of insert mode when you've finished whatever thought you're writing down, but the lines between thinking, composing and editing are blurrier than in prose.

Nevertheless vi controls can be pretty semantic, and lend themselves pretty effectively to working with words, clauses, sentences, and paragraphs. On the other hand, using search for mid-paragraph navigation is an order of magnitude less efficient in prose, when you're so much more likely to have symbols and words doing totally different things in many places in your text.

[+] sixtofour|14 years ago|reply
Thank you keithpeter and keithpeter for those two links.

A minor thing that's useful if you stay in insert mode for long periods: You can click the mouse anywhere while in Insert mode, and your cursor/insertion point will move to where you click. This works in Gvim, and also in vim, but I suppose vim depends on how your terminal is set up. I've done nothing special to my terminal or my vim.

[+] pooya72|14 years ago|reply
I use VIM mostly for writing prose, and I learned VIM through these same tutorials. I don't think it's anything different.

I don't edit while I'm writing the first draft. But when I do edit, I'm always pressing <Esc>. That's why changed the keymap.

/<text> is an editor's best friend.

[+] mcantor|14 years ago|reply
Use Ctrl-o in insert mode to dip into normal mode for just 1 command. Also, use Ctrl-r in insert mode to paste the contents of an arbitrary clipboard (Ctrl-r, a to paste register "a, Ctrl-r " to paste register "" and so on).
[+] ranza|14 years ago|reply
Love the small videos that illustrates whats going on! Nice one!
[+] tudorizer|14 years ago|reply
They are actually animated .gifs. Awesome!
[+] St-Clock|14 years ago|reply
Yes, I don't see these often and it seems that they are powerful learning aids. Full-fledged videos/screencasts are hard to search and index while screenshots are bad to show actions.
[+] JshWright|14 years ago|reply
"A last word about notation: instead of writing Ctrl-λ, I’ll write <C-λ>."

Does anybody else's keyboard have a λ key?

[+] morbofry|14 years ago|reply
Well, when you use Emacs, every key is a λ key...
[+] avinashv|14 years ago|reply
I assume the λ is just a placeholder for any character.
[+] yogsototh|14 years ago|reply
In vim I used digraph.

<C-k>l*

You can have the complete list with :dig

[+] shocks|14 years ago|reply
Everyone uses vim in their own way, you should learn how to use vim for the style that suits how you work. Only learn what you need to learn, you can't learn everything. No one knows all of vim; if you say that you do, you are a liar.
[+] pestaa|14 years ago|reply
Nicely illustrated tutorial, well done.

One thing to note though: Y and yy are not shortcuts to 0y$ -- the latter does not yank the line ending. I use it to insert one line into another, but find splitting and joining lines to be cumbersome.

[+] mathias_10gen|14 years ago|reply
The defualt mapping of Y is arguably broken since it doesn't follow the normal vi conventions. I have these lines in my .vimrc to "fix" it:

    "make shift Y behave like shift-[cd] (copy to end of line)
    nnoremap Y y$
[+] Bo102010|14 years ago|reply
What I would love (and suppose I should make) is a CodeAcademy sequence for vi.
[+] WilhelmJ|14 years ago|reply
When I learned to use vim years ago, one interesting observation was that the biggest thing to learn in vim is efficient navigation.

i.e. if you know how to get to a particular logically defined place in the file, you will always be able to carry out copy-paste-delete to and from that place to where to cursor is.

One would be surprised how easy thing become once you know the basic navigation rules due to marvelous mix-n-match nature of vim operations.

[+] BadassFractal|14 years ago|reply
As someone who works on large .NET / Java / RoR projects, would I gain anything from switching to vim from say, VS 2010, Eclipse and RubyMine? I'm quite used to relying on visual IDEs, but if vim is supposed to make me incredibly productive then I'd certainly like to explore that option.

Anybody here made the switch and felt it was worth it?

[+] tudorizer|14 years ago|reply
Does anyone fancy a game of vim golf? :) (after finishing the last phase, of course).
[+] kmm|14 years ago|reply
I've been using Vim for about three years but I've never been very handy at it. I'm now wondering that that might be because I touch type with only four fingers and whether it's possible to relearn my typing methods.