Yan Pritzker photographer, entrepreneur, software engineer, musician, skier

Blog :: Git Workflows Book :: Dotfiles :: Photography :: About Me

TwitterCounter for @skwp

Get the news feed
Get updates by email
Follow me on twitter

hello, i'm yan

I am a photographer, entrepreneur, software engineer, guitarist, climber, and telemark skier

This blog is about startups, blogging, Ruby On Rails, virtualization and cloud computing, photography, customer service, marketing, ux and design, git, and lots more.

I am the chief technical something or other at Reverb.com - The Marketplace for Musicians. We're hiring web designers!

Recent Tweets

Selected Reading

Archives

Contact

Reach me at yan at pritzker.ws

Stop using colon commands in vim

Posted 30 May 2012 @ 11am in vim



Submit to HN
Time for another vim geekout! :)

Typing colon :commands is really slow. You have to hit shift, colon, command, plus enter to execute. You’re taking a 3 keypress overhead every time you use one. So what to do? Simplify your life with a three tiered approach

  1. Super common things should be chords (Ctrl-something, Cmd-something, Cmd-Shift-something) or single key presses.
  2. Second most common things should be two character mnemonics such as vv
  3. Third tier should be leader-based commands with two or three keys such as ,gg for GitGrep in my setup

For example, the normal way to go to a tab is :tabn which is at least 7 keypresses. A much better way is to map all your tabs to Cmd-1 through Cmd-9 and jump instantly to them with a single chord (This if for MacVim, you can map another key if you need unix)

map <silent> <D-1> :tabn 1<cr>

What about jumping between windows? The standard vim way will have you doing a chord and then a key (C-w, l) to get to the right. Remap to a two-key chord:

nnoremap <silent> <C-l> <C-w>l

Closing windows? Normally you’re stuck with :bd for 5 key presses to kill a buffer or C-w, c – a chord and a key (3 slow keys). I kill windows all the time, so for me it’s just the letter Q – a simple chord. And I made it smart enough to kill either the window or the buffer, using a script that figures out if there are any other windows into the same buffer.

What about splits? Same deal..way too many keys. Type a two key mnemonic vv for a vertical split:

nnoremap <silent> vv <C-w>v

I usually grep for things all day long so I mapped a single key K to give me the git grep for the current word under the cursor:

nnoremap <silent> K :GitGrep <cword><CR>

My rule is to really consider every time I type more than 3 keys to execute a command in vim, and if it’s something I do every day, remap, remap, remap!

If you liked this, please take a look at my YADR dotfiles project – which has many more interesting keymappings to save you time, and has recently broken 700 watchers on GitHub!

6 Comments

[...] Stop using colon commands in vim [...]


Posted by
Scott
6 June 2012 @ 1am

I have to say that I really hate this idea. Key chords destroy one of Vim’s primary advantages: the ability to touch-type commands. Colon commands are not as bad as using command/control/alt key chords because they can be touch-typed, but I do agree that they are a problem. They require at least one extra key just to get the colon.

My way of dealing with this is to remap the semicolon to the colon and the colon to the semicolon. It’s a small change, but makes a big difference in speed and convenience. Window navigation in Vim is one of those features that relies on key chords, but shouldn’t. It’s jarring to use this feature as delivered. I have leader set to comma and ,w mapped to ctrl-w.

Buffer management can also be time consuming. This mapping helps with that:
nnoremap b :buffers:b

In case your blog strips what looks like markup tags from comments, that was:
nnoremap leader b :buffers cr :b Space

Anyway, the whole point of mappings like these is to reduce the need for chords of any kind so that I can lock my eyes onto the text and let my fingers type one key at a time.


Posted by
Yan
6 June 2012 @ 11am

Thanks for your comment Scott.

Yes the semicolon remap is nice, I used it for a while myself but then decided instead to learn to use the semicolon more often (still working on that), and to avoid the colon commands.

I do map leader b for buffers (except I use CtrlPBuffer instead of the native buffer feature), so I agree with that.

I guess chords vs touch type is a matter of taste, but if you’re typing a colon command you have to hit enter at the end of it. My most common tasks like running an rspec or opening a related spec to the file I’m looking at are all single key chords which are much faster than hitting enter after typing any combination of things.

Of course I also use the kinesis advantage keyboard which means all my modifier keys are immediately under my thumb and my hands never leave the home row position, e ver.


Posted by
Yan
6 June 2012 @ 11am

I fixed the comments, you can now use code tags.


Posted by
Niels Bom
27 July 2012 @ 10am

I’m not that good with Vim but doesn’t Ex Mode offer a lot? Moving, deleting and applying macros to ranges is a lot more efficient when using Ex Mode commands than having to select a visual block and then doing stuff. It’s also more repeatable and changeable than doing things in Visual Mode.

I agree that certain commands that a) have no dynamic components like :bnext or starting your tests or whatever and b) are used quite often could benefit from a chorded mapping.


Posted by
Yan
31 July 2012 @ 3pm

Sure there are many good uses of Ex mode, I just mean to say that all the trivial usages should be avoided for efficiency.


Leave a Comment

To post code, use <code></code> tag around your code