Swaroop C H

blog books about contact subscribe

Effective Vim

16 Nov 2007

Being a fan of Steve Yegge, I was randomly reading some of his older writings and eventually chanced upon his post on "Effective Emacs". Being a Vim guy, I wondered whether some of the tips he presents are useful for the Vim world as well.

Note: This is not a Vim vs Emacs thing, it is simply a porting of tips for Emacs to see whether the tips are useful for Vim users as well.

So here goes:

10 Specific Ways to Improve Your Productivity With Emacs, ported to Vim:

Item 1: Swap Caps-Lock and Control

This is a desktop-specific customization. However, I don't think it is required for Vim users, but it can be useful.

Update: After using for a couple of days, I'm really starting to like this!

Item 2: Invoke M-x without the Alt key

Not relevant for Vim. Not a good start, first two tips are out...

Item 3: Prefer backward-kill-word over Backspace

This is a good tip. Normally, I would use bdw to achieve the same. To map backspace to this command in normal mode, put this in your vimrc: :map <bs> bdw.

To make it work in insert mode you can put :imap <bs> <esc>bdwa. I'm sure there's a better way to use just one command to do this, please leave a comment if you know of a better way.

Update: You can also use ctrl-w in insert mode (see :help i_CTRL-W), thanks to pimaniac.

Item 4: Use incremental search for Navigation

Use :set incsearch.
Press n to search forward and N to search backward.

Item 5: Use Temp Buffers

Run :new to get a new buffer (or alternately ctrl-w n).
To switch between buffers, use ctrl-w ctrl-w (yes, twice).
Use :q as usual to close the buffer (or alternately, ctrl-w q).

Item 6: Master the buffer and window commands

Item 7: Lose the UI

Item 8: Learn the most important help functions

The help in Vim is vast, see :help usr_toc to see the chapters of the awesome reference manual.

Item 9: Master Emacs's regular expressions

I agree, Friedl's book is the authority on this. However, there are some good introductions to Vim regular expressions available.

Item 10: Master the fine-grained text manipulation commands

Tune in next time...

  1. Filling paragraphs can be done by setting :set textwidth=80 and running gqap command to format 'a' 'p'aragraph, or like me you can map the 'Q' key to run it : :nmap Q gwap. To make this work inside comments, make sure you :set formatoptions+=c.
  2. gnuserv : I use It's All Text! Firefox extension.
  3. Dired : There are plugins available with similar functionality
  4. Whitespace manipulation - plenty of ways such as :set expandtab, :retab!, :help fo-table, etc.
  5. nxml-mode : I haven't used nxml-mode but I'm still looking for something like Emacs' SGML-mode that works for Vim. I miss you, SGML-mode.
  6. picture-mode : Dr. Chip to the rescue with DrawIt.vim
  7. minibuffer management : Not sure what this is.
  8. effortless navigation : I think Vim has enough keys for this by default. See :help navigation.
  9. region management : We can always choose the color scheme of choice for the highlighted region, or change it ourselves, see `:help
    :highlight`.
    
  10. rectangle commands : Use ctrl-v
  11. emacs shells : We have :sh but don't know if Emacs does something more
  12. align-regexp : Not sure what this is.
  13. frame initialization : I set Vim to always opens in full screen, see :help win16-maximized. Not sure how to do it in Linux yet, but in Gnome, I just press Alt-F10.
  14. using the goal column : No idea...
  15. setting the fill column : Nada...
  16. OS settings and font : I like to customize Vim's font and keep trying different fonts, currently I'm using :set guifont=Consolas:h14:cANSI
  17. browsing and editing archives : I think Vim does this by default, see :help netrw.
  18. advanced keybinding : see :help :map and :help keycodes
  19. mastering the kill ring : I guess you can simulate this with :echo @a, etc.
  20. mastering Info : Not sure if this would be useful in Vim.
  21. using M-x customize : Not sure what this does.
  22. utility apps : It's all in the plugins.

Summary: Porting good ideas is a good idea :)



I wonder why a search for Steve Yegge on Wikipedia points to Batman...



Update in November end, 2008: I have released a new book on Vim, read the whole thing right here..

Comments

Chris says:

Hmm, the comments don't understand my angle brackets. With parentheses:

imap (bs) (c-o)bdw

Swaroop says:

@Lee: Okay, I tried changing the caps lock, but I'm having a hard time getting used to it. However, I can see now that it is easier on the fingers :)

Konrad says:

Talking about swapping words, you may be interested in my friend's python script that does that (including some more advanced stuff like recognising function calling or nested parenths). Written using vim-python bindings.

http://blog.kamil.dworakowski.name/2007/09/swap-parameters-script-for-vim.html

Swaroop says:

@CYT: Interesting, thanks.

@Karl: I am a touch typist, so that's why Vim is a comfortable place for me. And yes, Snippets is awesome.

@Choy: I haven't tried Screen yet, should try some time.

@Konrad: Neat, will check it out.

Swaroop says:

@Gopal: Sometimes I can't differentiate your humor and sarcasm! :)

Lee says:

I think swapping the caps lock is still a good idea. I use ctrl-p all the time for completion. And (looking ahead to item 3) ctrl-w to kill the previous word (in insert mode).

jonathantan86 says:

I rebind Caps Lock to Escape via my .xinitrc file. As for item #3, db will delete word in normal mode as well.

t3rmin4t0r says:

There was a Batman episode with Egghead ...

kretik says:

re: fonts, I also like to try out new fonts in [G]Vim, but editing ~/.vimrc to change them or entering the whole correct font descriptor string in the command area can be a pain. So use :set guifont=*. This works with the GTK and Win32 GUIs, and will display a standard font selection dialog. Once you have the font you want, edit /.vimrc and add the command there to change it permanently.

links for 2007-11-18 at Link Right 2 says:

[...] Swaroop C H, The Dreamer » Archives » Effective Vim (tags: howto linux programming vim vi) [...]

Anon says:

It's all text is amazing! Now I can finally feel 1337 while surfing . . .

links for 2007-11-18 « Donghai Ma says:

[...] Swaroop C H, The Dreamer » Archives » Effective Vim (tags: programming tips vi vim) [...]

choy says:

split horizontally - C-w, s

Also if you're using Vim on a vt100 compatible terminal, you must use it together with screen. Screen is like a window manager for terminals.

Together it's very effecient.

Karl Guertin says:

The closest thing to the first two is learning to type instead of since it keeps you on the home row (I don't do this).

To that, I'd add that you should learn and practice:

http://jmcpherson.org/editing.html

Beyond that, I like remapping K to (and the corresponding J) and opening everything with :split, though you could just as easily use the tab equivalents in Vim 7.

As far as plugins, I'll recommend Snippets and AutoClose. Investing in your own set of snippets pays nice dividends.

Karl Guertin says:

Err, that's mapping CTRL-K to CTRL-W K CTRL-_ (and the correspondnig CTRL-J). I get used to writing with the angle bracket convention, which got stripped.

Ben says:

@jonathantan86: how?

Chris says:

You can use for only one command and go back into insert mode. So your mapping would be something like:

imap bdw

I wouldn't like to have backspace mapped like that though, ^W seems more like an appropriate key for that.

cyt says:

Some explanations:


nxml-mode can autoclose tags and validate on-the-fly. Vim probably has a plugin.
the minibuffer works in the same way as vim's :, with history, completion etc.
of course it does more. you can have more than one, for a start.
align-regexp adds whitespace to fields to align a group of characters in a column. yeah, it's hard to explain.
setting the goal column keeps you in the same horizontal position when you move between lines.
text width


Oh, and you should all remap ; to : for the equivalent of tip number two. It's the same as M-x and is easier on the shift key.

VIM tips and tricks 1 « technical jibba jabba says:

[...] w odpowiedzi na post Steve’a effective emacs, dawno temu pojawił się post effective vim [...]

Vasudev Ram says:

Interesting tips on vim.

Here's another similar interesting post about vim on Jamis Buck's blog.

http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim

I also put a comment or two on it.


Vasudev

Swaroop S says:

Hey Swaroop ! These tips are fabulous !

I am hooked ! I have been using VI and frequently get " Why dont u use Emacs" looks - Wait till them i show the new tricks up my sleeve - I am sure the split windows shall "ooh- haah" them

-Swaroop

Sergio Luiz Araujo Silva says:

Hi, to lowcase entire file --> ggguG

Feedback

There's no comment box, but please do email me or tweet me your thoughts and criticisms, and I will publish the relevant ones here.