Ask HN: Suggestions for mastering vim?
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?
67 comments
[ 3.0 ms ] story [ 127 ms ] threadTry reading :he instead of HN. I've found far more in its built-in docs than I do with google or books anyway.
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.
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.
Generally, you want to stay out of insert mode as much as possible (memorize most of the movement commands and use them), use splits liberally (if you have a big monitor) and use generic awesome plugins such as: NERDTree, surround.vim, snipmate, taglist.vim, etc. Use a complete mode that works with your language of choice or a third-party one that gives more. Omnicomplete is a good place to start and it's built in.
Check out the vim sub-reddit, vimcasts.org, and the Daily Vim blog. There is a wealth of information in those places.
Shoot me an email (see profile) if you want more specific guidance. I initially planned to type stuff out here but remembered how awful the formatting is.
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.
Maybe I should spend my 4th of July weekend with vimtutor :)
read more about it, on web and books
use what you read
rinse, repeat
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.
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.
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!
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.
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.
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
After learning Emacs, which derives its awesomeness from the amazingly large set of stuff it knows how to do, I found vim to be good for other reasons. Vim is good because with very, very few commands, you can do things much quicker.
The "f" or "t" commands, combined with the fact that you can use "c", are just amazing. Same with the "i" (inner) command. Those few commands alone can make editing any line faster and funner.
(Technically it's a motion, not a command. "i" as a command just enters insert mode.)
To elaborate, since this is one of my favourite vim features, say you have the following code:
Say your cursor is where the caret indicates. Typing 'ci)' ("change inside parens") in normal mode will:* delete all the text between the two matching parens
* place you in insert mode with the cursor between the two (now adjacent) parens
* put the deleted text in the yank buffer so that 'p' will paste it.
The use case here is obviously so you can assign a name to that long complicated expression. 'ci)' is much easier than selecting it with the mouse, and keeps your hands on the keyboard where they belong ;)
With nested parentheses, it does what you expect (affects the text contained by the innermost matching pair to contain your cursor - try it and see).
Other equally useful variants:
* i" - "inside double quotes" - everything between double quotes
* i' - "inside single quotes"
* iw - "inside word" - the word the cursor is on
* is - "inside sentence" - great for editing prose
* ip - "inside paragraph"
There are also similar motions beginning with "a":
* a) - like i) but includes the parens (e.g. 'da)' deletes everything inside parens and the parens themselves)
* a" - similarly
* aw - like iw but includes trailing whitespace.
(Update - I thought this deserved its own air, so I turned it into a blog post: http://blog.samstokes.co.uk/post/767636740/vim-wizardry-1 Bonus tip if you read to the end!)
I've been using Vim for over a year and had no idea about this.
Holy crap, that's awesome.
My only regret is that I have but one upvote to give!
I always used to do F(lvf)hc rather than ci)
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.
I often find that studying other people's vimrc's can be enlightening http://dotfiles.org/.vimrc
And as mentioned above, vimcasts.org is very useful too.
On the similar note, read the book 'Mastery' by George Leonard, and he'll tell you that in order to achieve mastery, you'll have to go through periods of long plateaus before you get to the next level.
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:
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.
Once I mastered those, I learned 5 more, etc.
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)
I should have mentioned that another thing that can get you more intimate with vim keyboard shortcuts is trying out one of the vim browser plugins. I use vimium on chrome and vimperator on FF. These give you a number of the keys you're used to using in vim like gg, G, and even / search.
Gmail binds those keys, but I'm so used to scrolling up and down with j/k that the action Google chose is infuriating. What's worse is that turning the keyboard shortcut option off in gmail still captures the key press event, but silences it, effectively breaking my favorite plugin.
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?
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.
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.
Never use an "IDE" unless there is absolutely no other way.
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.
http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial...