top | item 1484061

Ask HN: Suggestions for mastering vim?

70 points| physcab | 15 years ago | reply

I've been using VIM for the past 15 months or so. However, I've noticed that I've reached a level of comfort that is far below VIM's capabilities and I'm in awe of those who know how to wield it much better than I. I know I can be much more efficient, but I don't know where to start. Thoughts?

62 comments

order
[+] bobbyi|15 years ago|reply
You need to start by learning all of the various operators and movement commands for Normal mode. Memorize as many as you can and try to think about where you would use them.

As you edit, force yourself to notice the situations where an operator/ movement command exists that is slightly more efficient than your usual way of doing things. For example, recognize opportunities to use the c and s operators instead of a delete operation followed by an insert and to use f and t to jump to a specific character rather than holding l.

At first this will be much less efficient than just doing things in the obvious way. Keep pushing and memorizing more movement commands and operators until you know what every key does without thinking about it. Use visual mode when necessary to verify what text a command will operate on.

Then get good at combining the operators with movements and repeating them by using the . operator and by prefixing them with numbers. Also, start remapping things aggressively to make vim work the way you think it should. If there is a key whose default action you don't find that useful (e.g., Enter, Backspace, Space, Tab and the arrow keys), have it do something that justifies its place on the keyboard.

Only at that point, start really drilling into the ex commands and (ugh) vimscript.

You should also figure out how to use/ add auto-completion, version control integration, project management, diff mode, build support, ctags, etc., but that is mostly orthogonal to learning how to use vim as an editor.

[+] csmeder|15 years ago|reply
Memorize as many as you can and try to think about where you would use them.

As you edit, force yourself to notice the situations where an operator/ movement command exists that is slightly more efficient than your usual way of doing things....

At first this will be much less efficient than just doing things in the obvious way. Keep pushing and memorizing more movement commands and operators until you know what every key does without thinking about it.

Great Advice

[+] raju|15 years ago|reply
(Disclaimer - I am not a vim user but a emacs newbie)

I came across this on HN - http://vimcasts.org/

There was a HN discussion on this a while back - http://news.ycombinator.com/item?id=1297221 (Check the comments for resources that other HN'ers mention)

Hope this helps.

[+] physcab|15 years ago|reply
These are great links, thanks! I hadn't heard of either of these.

Maybe I should spend my 4th of July weekend with vimtutor :)

[+] bigmac|15 years ago|reply
Your problem is real. Vim can do so much that you might not even know that there is a better way. The first step is to realize it will be a slow process. Its doubtful that you'll be able to take time to practice vim for the sole purpose of getting better at vim. More than likely, you will have to pick it up along the way while working on real work.

My approach to this problem is to occasionally go to vim.org or http://vim.wikia.com/wiki/Best_Vim_Tips and hit the random button until I find something useful. Try out the new technique a few times, then commit it to memory. If its a common enough task, it will eventually become automatic. For me, I can only incorporate 1 or 2 techniques in a month. YMMV. Good luck!

[+] duckpunch|15 years ago|reply
Two very general suggestions, pretty obvious, but hopefully of some help.

1. The really obvious: You find yourself wanting to do something that is taking too many keystrokes. Think about what you're doing and how it could be improved. It might mean mapping a key to a series of commands or dreaming up a plugin you could write. When you do the latter, look around for one that is solving a similar problem; even if it doesn't directly solve your original problem it might be useful. I find myself trying out people's plugins, and often times they don't turn out to be useful and get deleted.

2. Something that is already in my workflow amazes me in its efficiency. Think about what makes this really cool and useful, and you might be able to think about how this technique can either apply to other situations.

There are some “breakthrough” improvements that I've had, so I'll share the ones I can think of.

- f and F for hitting some character on a line. I still use h, l, b, B, w, W, e, E and the others, but f/F is what I usually think to do. t/T also comes to mind of course, and these are especially powerful in concert with others, for example I use combos like f(ct) enough that they're in muscle memory.

- %

- esc is mapped to C-[ by default. I've been using esc, and am comfortable with it, but I'm starting to get used to this, and it's likely going to be faster in the future.

- block visual mode C-v is really useful

- some emacs-esque keys are available in insert mode. I use C-b, C-f, C-a and C-e quite often there

- I mapped C-j and C-k to do 3 j / k which helps me quickly go up and down a document

Beginner lessons (not for you, but in case someone else can be helped):

- Stay the out of insert mode. This is often my first tip for someone starting out with vim: you're not going to be comfortable until you find yourself being in command mode all the time and only incidentally in insert

- Never ever reach for the cursor keys. hjkl are there for a reason, and if you feel uncomfortable using them now, get over it, you'll thank yourself very soon.

Thanks for posting this question, because now I can ask this: For the vim masters, what changes really improved the way you worked with vim?

You can also rest comfortably in the knowledge that most vim and emacs users probably feel the same way, or so I assume. Anyone out there feel like their vim workflow doesn't incrementally improve? I don't think I've used it long enough to be a judge of this, but I find I'm getting better with use, though the improvement curve has definitely dropped off.

[+] andrewl|15 years ago|reply
I agree with ggruschow: read the help. I printed the manual (it's about 275 pages) and the FAQ (about 130 pages) and put them in binders. I read them periodically to learn new features and concepts. They're available at:

http://vimdoc.sourceforge.net/

Damian Conway has a series of five excellent articles on Vim, starting with:

http://www.ibm.com/developerworks/linux/library/l-vim-script...

At the end of the last article he says that there's at least one more on the way.

Watch "7 Habits For Effective Text Editing 2.0," by Vim creator Bram Moolenar:

http://video.google.com/videoplay?docid=2538831956647446078

He doesn't just tell you things about Vim, but suggests how to become more effective with Vim.

[+] snprbob86|15 years ago|reply
Put in your vimrc:

    " disable arrow keys
    map <up> <nop>
    map <down> <nop>
    map <left> <nop>
    map <right> <nop>
    imap <up> <nop>
    imap <down> <nop>
    imap <left> <nop>
    imap <right> <nop>
[+] duckpunch|15 years ago|reply
I'm curious, do you keep this around to this day? I never thought to do this when I started, but it might have been a good idea.

In similar vein I would say: If your fingers aren't constantly resting on home row, there's a problem.

Another advantage learning to use hjkl is that those keys are common in other unix programs, like less for example (which also has gg, G, / and some others)

[+] 1tw|15 years ago|reply
Similarly, forcing yourself to stay out of insert mode is a huge help when learning vim:

   " Automatically leave insert mode after 'updatetime' (4s by default)
   au CursHoldI * stopinstert
[+] tdavis|15 years ago|reply
The only problem with Vim is that there's so much you could do, but only so much you can apply to muscle memory. In terms of "wielding it much better", what are you trying to do? Ask yourself, "What situations do I get myself in where I intuitively feel there must be a better way to do this?"

Does something take too many key strokes? Too much repetition? Do you need auto-complete for languages, projects, etc.? Do you have trouble navigating? Changing surrounding syntax? Give a bit more detail and I'd be happy to point out stuff you could/should be doing.

Most people will just throw out a laundry list of cheat sheets and useful plugins; while there are many of these, you should concentrate on efficiencies that will gain you the most productivity for your specific tasks.

[+] physcab|15 years ago|reply
See thats just it. I don't really know what I need to improve. Usually what happens is this:

1) I have a bug in my code and I go ask a senior guru for help. 2) Senior guru comes over to my desk and helps me diagnose the problem. 3) While I fix the bug, senior guru notices some inefficiency in my workflow (unrelated to the bug) and asks, "Why are you doing X that way...you know you could just do Y".

It's great that this kills two birds with one stone, but it would be awesome if I didn't have to leave this to chance and I could find the inefficiencies myself.

I feel like I could use help in all those areas you suggested, but I've looked at those cheat sheets and they never help. What I feel like I need is a story line of some sort where best practices are taught and I can follow the behavior of others. Ideally something like pair-programming would be the cure but its a lot to ask for.

[+] lawn|15 years ago|reply
My advice is to configure it. Change the appearance and add your own functions, for example I've mapped f3 to convert all tabs into spaces and to remove all trailing spaces. If you do this you'll get more involved, interested and even more fun to use vim.

Of course you should use and read as much as you possibly can, but I found that it went down quicker after I've spent some time with configuring it.

[+] targz|15 years ago|reply
That sounds really useful... Any chance of sharing this part of your vimrc?
[+] jcw|15 years ago|reply
Try learning one new vim command or feature every day, and forcing yourself to use it. If you find yourself not using it, instead doing something less efficient, undo the changes you've just made and re-do them in the newly learned way.

For example, after learning that ^R let me paste to the command-line, I sometimes would copy and paste text with the mouse. When I would catch myself doing this, I'd delete the newly pasted text and use ^R" instead.

The vim wiki and blogs like the Daily Vim helped out a lot with learning a new thing every day.

Also, the docs are excellent. Start reading them (:help toc for the table of contents).

Learn how to use regular expressions in vim efficiently! Turning on hls (:se hls) helps out a lot in learning regex, because you can interactively see what atoms are doing.

Learn how to script vim (:help vim-script-intro). Vimscript kind of sucks, but being able to automate tasks using it is great.

[+] hundredwatt|15 years ago|reply
Vim has been the program I have spent the most or second most (the other being a web browser) time using over the last 2-3 years. Still, I don't consider myself a "master" at all.

I have not used a formal method for learning, but the general approach has been to check out one of the many vim help pages about once a week. At that time, I look for new tricks based on what currently annoys me the most.

Every 2-3 weeks I end up picking up something that sticks. The most recent trick I picked up is using the '%' key (SHIFT-5). The trick before that was buffers (:e, :bn, :bp).

I initially started using vim because it was more productive for me than the 'fancy' graphical IDEs, even after I just learned the very basics. Since then, I have steadily gotten more and more productive with it.

[+] okaramian|15 years ago|reply
I started by learning 5 commands I would use a good bit at a time. So basics for me were copying lines/deleting lines/moving around/inserting text and saving files. Then I moved on to tabbing, splitting etc.

Once I mastered those, I learned 5 more, etc.

[+] jamesbowman|15 years ago|reply
Yes, step by step. I find post its with the new commands work great.
[+] unshift|15 years ago|reply
I was/am in a similar situation, and I keep the vim graphical cheat sheets (http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial...) open in a browser tab. Whenever I'm doing something "indirectly" that feels funny (e.g. moving to end of line, then pressing <- to move the cursor back to an open paren) I look it up and find the better way (e.g. "$F("). After a while some things start to sink in
[+] BrandonM|15 years ago|reply
I made some helpful comments in a discussion thread here a couple years ago: http://news.ycombinator.com/item?id=233873 (see also http://news.ycombinator.com/item?id=235177 a little farther down that page)

That might help you get started with some of the more interesting capabilities of vim. Some additions I would make now are some of the control shortcuts in insert mode:

  C-n (word complete forward)
  C-p (word complete backward)
  C-x f (complete filename)
  C-x l (complete line)
The mark command is also useful. Need to check something elsewhere in the file while coding? Hit mm (the first m says to set a mark, and the second m stores that mark in the 'm' register). Now check out whatever you need to, and then use `m (backtick, then m) to return to your previous location.

Also, remap Caps Lock to be Escape, or start using Control-[. Escape is pretty far away on most keyboards; using a closer alternative will help you more than you would expect.

[+] ggruschow|15 years ago|reply
Use it more, and as you're working think of things that could be repeated.. or actions that seem too take too long (finding the definition of a function, moving the cursor to the 2nd parameter of the function call on the next line, etc, reformat some data, etc).

Try reading :he instead of HN. I've found far more in its built-in docs than I do with google or books anyway.

[+] thebigshane|15 years ago|reply
By no means do I want to attack the holiness of vim (or emacs for that matter), and I get the idea of "text manipulation using solely the keyboard is far more efficient than using the mouse".... but... keyboards suck too!

I understand that introducing a new mechanism for text entry is hard and going to throw a lot of people off ("but I'm already fast at the keyboard/mouse!") but I really wish we have come up with something better by now. Multitouch gestures? suck at text entry. Voice recognition? Sucks AND awkward. Handwriting recognition? sucks and sometimes slower. I've seen the frogpads and Shark ( http://www.almaden.ibm.com/u/zhai/topics/virtualkeyboard.htm ) is pretty intriguing.... but there has to be something better that would blow vim/emacs/IDEs/N++ out of the water. Anybody have any ideas what that would look like?

[+] yanilkr|15 years ago|reply
This comment is really a tangent to the above topic.

Like 8 years back, I was in a java one conference, James gosling was on stage and some one asked him a question about emacs. His reply was, there are so many good editors now and emacs was written for his earlier times.

Before this incident, I used to feel so much pressure to learn something command linish and primitive like vim or emacs to fit in with the geek crowd and look more techie.

After this incident, I never touched emacs again.

The question here is mastering vim why? is it worth it?

[+] mahmud|15 years ago|reply
Did you just call Emacs primitive? You judge the only piece of programmer productivity tool worth using because someone whose job is to push development tools dismissed it from a podium, 8 years ago? Gosling might have discovered Emacs, but 8 years ago, he was on a payroll to push the One Platform. At the time, Sun and its staff had plans for world domination and had no trouble desecrating what anyone held dear, in order to create a one universal platform, with programmers interchangeable and replaceable as spark plugs; a heavily customized development environment would pose a threat to this.

The benefits of mastering your tools are economical and felt in your pocket and productivity. Every minute you spend waiting for an IDE to move its fat hind around to get something done, is a minute lost from your working day. If your aim is to be productive, and not just "replaceable", then there is no need to shackle yourself with a lowest common denominator environment.

You don't spend 100% of your time writing code in one language or tech stack, in an IDE; there are hours spent researching and taking notes, writing documentation, coding in a myriad of different languages, etc. The point of using a powerful editor like Emacs and Vim is to streamline your work, all of it. It's a generic, customizable UI to automate everything that buzzes and moves in your machine. Writing code; interacting with various interpreters, compilers and debuggers; taking notes and TODO lists; etc.

[+] etal|15 years ago|reply
Java needs an environment that can generate, parse and rearrange Java code in a way that vimscript just isn't suited for. Even in Emacs it's a Herculean effort to make the editor understand Java at a deep enough level. Generally, if the language you're using co-evolved with a particular editor, use that editor, not vim. (That's not limited to Java.)

On the other hand, C as a language grew up alongside vi, as well as the Unix environment it leverages so well. Languages with a nice REPL, like Python and Ruby, also are concise and flexible enough that any text editor will do; the languages are meant to work well enough with minimal editing capabilities. In those cases, vim is still great, because it's optimized for rearranging text of any kind.

[+] loup-vaillant|15 years ago|reply
How about a text editor that just obey your will? That would obviously be the ideal thing to do. The problem is, it's not yet possible.

But we still can try to approach it. You can train to type so fast that your fingers move at thought speed. You can use a keyboard layout and a text editor that helps you do that.

If you type and edit lots of text, the worth of advanced editors is obvious: the various short-cuts they let you use makes you faster. Because of that, whatever you do with it is easier. Because of that, you do more.

Learning to properly use a good editor is a bit like learning to touch-type. Slow at first, but the rewards (better comfort and greater speed) makes you wonder why you didn't do that earlier.