qrcode
The slightly less long-winded not-at-work version of Will

Theme by nostrich.

12th November 2011

Post

vim tips

I’ve mentioned in the past that I’ve been using vim as my primary editor for some time, so I figure now is probably as good of a time as any to give some tips I’ve picked up during my extensive use of vim.

First, people wonder why I use vim when I work primarily in GUI environments:

  • It’s available everywhere. On Windows, Mac, and all the non-GUI platforms I work on. So it’s quite handy to get really used to one editor and know it very well. TextMate isn’t available on Windows, Notepad++ isn’t available on Mac, and neither of them are available to me when I’m SSH’ed into a remote terminal with only terminal access.
  • Once you get through some of the initial learning curve, it’s faster (for me) to move around than other editors - if for no other reason than that I’m not constantly reaching for the mouse.
  • If you don’t like the way it does something, there’s probably a setting to change the way it behaves.

These aren’t necessarily any better than anybody else’s tips, just a random smattering of lessons I’ve learned along the way.

  • Force yourself under whatever circumstances to use it. Set your EDITOR or VISUAL environment variables, and begin deliberately forgetting to use -m when checking in code via git or svn so that you get forced to use it.
  • vimtutor. Make yourself at least 30 minutes to go through the whole tutorial, and on multiple days. Do everything it says to do. You’ll only get better at using vim by using it, so you have to do what the tutorial says to do in order to start developng the muscle emory required.
  • Learn the f, t, F, T, ; and . commands early. These shortcuts alone make me more efficient than most other editors.
  • Figure out something every day that you want customized, then learn how to do that in your .vimrc. Mine is pretty minimal, but I’m working on it.
  • set paste. You’re welcome.
  • Language-specific settings can be set in ~/.vim/ftplugin/<type>.vim instead of using a bunch of autocmd’s in your .vimrc.
  • Learn to use the :help command. First, just do :help to learn how to bounce around within the help. (Ctrl-] will follow a tag, if you don’t bother to read that far initially).
  • Under no circumstances should you ever reach for the mouse or arrow keys - especially early on. Learn to use h, j, k, and l so that one of these days when you’re on a vt100 terminal and the arrow keys don’t work, you don’t suddenly get stuck with an editor that won’t work at all for you. (You can use the arrow keys if you’re in INSERT mode and don’t want to constantly switch back and forth - although I’d recommend figuring out how to accomplish what you want to do with normal mode.)
  • Lean to use some of the features which you don’t use religiously in other editors - split panes, multiple buffers in a single tab, etc. I’m not saying those features aren’t available in other editors, but those are the types of things that tend to be buried a few menu commands done, but with vim, you’re kinda’ expected to be able to use them to your benefit.
  • u is undo. Ctrl-r is redo. You’re welcome. (Incidentally, vim’s undo history is non-linear - if you make change 1, then 2, then undo back to 1, make change 3, you can go back to 2. There are some decent sites which explain how to make sense of this, but maybe :earlier 20s will do what you really need.)
  • I personally don’t use too many plugins, although there are tons of them. The reason for this is that I want to be really good with the core functionality so that when I’m on a system without my plugins, I can still move about eficiently. The one plugin I do tend to install is NERDTree. However, if you want vim to do everything you want it to do, install some plugins.
  • Use it every day. Practice every day. Learn a new command every day, and then go looking for excuses to use those commands.
  • I personally use :set number because a lot of the commands I use are easier to use by knowing line numbers. I don’t use visual mode too much (although I’m getting better). Because I use line numbers so much, but because on a terminal, they’ll be copied to the clipboard if you’re selecting something to put in an email, say - you’ll want a way to quickly turn those off. I use a keyboard shortcut, but :set nu! is not too slow if you’re only going to do it on occasion.

by

()