

Ask HN: Suggestions for mastering vim? - physcab

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?
======
bobbyi
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
_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

~~~
bobbyi
What's not obvious at first about vim is that its power (somewhat like unix)
comes from orthogonality. Since you can combine any operator with any movement
command, the number of commands that you know is the product of the number of
operators and the number of movements.

~~~
edanm
This really hits home for me.

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.

~~~
samstokes
_Same with the "i" (inner) command._

(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:

    
    
        do_something_with(some + long * complicated * expression)
                                   ^
    

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!)_

~~~
rbxbx
And if you like operating inside of matching pairs, just think how much you'll
like manipulating the pairs themselves! <http://github.com/tpope/vim-surround>

~~~
edanm
This is really interesting. I don't vim much anymore but thanks for this!

------
raju
(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
These are great links, thanks! I hadn't heard of either of these.

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

~~~
wyclif
My first piece of advice to vim n00bs is simple. Use vimtutor until you can
cruise through the entire thing without reference. When I was learning vim I
would go as far as I could doing the exercises. If I forgot something I would
start all over again from the beginning. Do this over the course of a week and
you will begin learning quickly. My second piece of advice is to use vim for
_everything_.

------
bigmac
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
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
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...](http://www.ibm.com/developerworks/linux/library/l-vim-
script-1/index.html)

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
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
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)

~~~
rbonvall
There are also web applications where hjkl work as well, like Gmail and Google
Reader.

~~~
duckpunch
This is actually a huge pain for me.

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.

~~~
rbxbx
with vimperator you can press ctrl+z to enable pass-through mode, therefore
allowing you to use the gmail shortcut keys, press escape to go back to normal
mode.

~~~
duckpunch
Thanks for the heads up. That is quite convenient, unfortunately I'm now using
vimium. I'm reminded that in my vimperator days gmail wasn't a problem because
the vi key bindings just worked everywhere. All this is moot considering I can
just patch the plugins. I assume vimium either has a similar capability or
that it can easily be added, but it does allow a web app to trap key strokes
before by default (opposite of vimperator)

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

~~~
tdavis
Ah, fair enough.

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.

------
lawn
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
That sounds really useful... Any chance of sharing this part of your vimrc?

~~~
lawn
This is how I did it

    
    
      " Trim trailing spaces and convert tabs to spaces
      map <F3> :silent! %s/\s\+$//<CR>:retab<CR>

------
philwelch
Keep this cheat sheet somewhere visible near your workspace:

[http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial...](http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html)

------
jcw
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
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
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
Yes, step by step. I find post its with the new commands work great.

------
unshift
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...](http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html))
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

------
daflip
I found this video useful - "Seven Habits of Effective Text Editing" by Bram
Moolenaar (the author of VIM):
<http://video.google.com/videoplay?docid=2538831956647446078#>

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.

------
BrandonM
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
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
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?

~~~
phr
I've always thought Dasher (project at
<http://www.inference.phy.cam.ac.uk/dasher/> ) was interesting. It's intended
for people with disabilities, but maybe it could be adapted for program text
entry?

~~~
thebigshane
Nice find! For those looking for a good demo:
[http://www.inference.phy.cam.ac.uk/dasher/images/newdasher.g...](http://www.inference.phy.cam.ac.uk/dasher/images/newdasher.gif)

Still... Great for firehose text entry, but doesn't address the sort of text
manipulation for which vim/emacs have become so popular.

------
genieyclo
[http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-
novice...](http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-novice-
tutorial-videos/) and <http://vimeo.com/user1690209>

------
yanilkr
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?

~~~
etal
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.

~~~
jrockway
I know hard-core Java people that use Vim. It integrates with Eclipse (by
talking to a headless instance over a socket) for the refactoring stuff. Vim
for the editing, Eclipse for the Java munging. Seems to work very well.

Never use an "IDE" unless there is absolutely no other way.

------
l0nwlf
Here is a word from horse mouth itself - Bram's tip on "Seven habits of
effective text editing" - <http://www.moolenaar.net/habits.html>

------
vdoma
I've been using it for many years and still learn something new frequently.
Don't worry about mastering it quickly. Subscribe to the vim mailing lists,
read other peoples vimrcs, and over time it'll come to you.

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.

------
spatulon
If, like I did for several years, you rely on using the arrow keys instead of
hjkl, disable the arrow keys in your vimrc. It found it incredibly painful for
maybe a couple of days, but now using hjkl is like second nature. The
efficiency improvement from not moving my hand back and forth between the
arrows and the home row is not insignificant.

------
prog
I have been using vim for 10 years or so but still there is much to learn :)

I find vim documentation very helpful. Its details and easy to access. So, I
make it a point to look around read a bit of :h for a few minutes.

------
ashitvora
I have recently switched from Textmate to VIM and I am loving it so far.

check this out <http://jmcpherson.org/vimrc.html>

------
pwim
I'm still a vim newbie, but something I find helpful is using ctrl-{ to go
back to normal mode (instead of escape), as I find it a faster and more
natural motion.

------
IgorPartola
Gentoo's vim cheat sheet is how I learned it in about 1 hour.

~~~
zaphar
As a long time Vim user I can pretty much guarantee you didn't learn it in an
hour. You may have learned enough to do the very basics but after nearly 10
years of using it _I_ still don't know everything.

~~~
IgorPartola
I'm sorry for being a bit rash with my statement. I learned to get around vim
as fast as in my old editor in 1 hour. Since then I learned to be much more
efficient than in my old editor. I don't claim to be a vim expert today
either. My point is that as soon as you are as comfortable in vim as in your
Notepad++/Coda/Eclipse, etc. you can pat yourself on the back and drop
whatever you were using before. From there you'll only get faster/better/more
efficient.

------
mkramlich
how does one master any software tool?

read more about it, on web and books

use what you read

rinse, repeat

------
ashishb4u
get emacs

------
pvg
One suggestion is: stop. Get an editor or IDE rooted in the practices of today
or at least, the last decade of the last century. It's not going to do half
the things vim can do and you'll never notice. If you really really really
care about the full end-to-end customization and programmability of your
editor, learn emacs. No great or useful product has ever been created solely
because someone knew vim. Or emacs. Move on and tackle actual problems.

