Perfecting Vim

Published 16 January 2011

Before I started using vim, my main text editor was the e-text editor. Now that I've been using vim for a while, I have no desire to go back to e, but I still miss one feature -- smart indenting on line wrap. When typing in e if my text wrapped around to the next line it would automatically be indented at the level of the previous line. In vim, when a line wraps, it is never indented. You can handle this by putting in hard line breaks, but then if you need to cut and paste your test anywhere you end up reformatting it all the time. I prefer smart indenting that I don't have to mess with.

Here is an example of the vim behavior that irritates me (see line 6):

Vim Screenshot showing a wrapped line that is not indented.

And here is the way I like it to be:

Vim Screenshot showing a wrapped line that is indented.

Luckily I'm not the first person that has wanted this. Václav Šmilauer developed a patch that has been updated by Eli Carter. There is no way to use this in the default vim distribution, but if you build vim from source you can add in this break indent patch.

Following is my documentation on how to build Vim from source on Ubuntu 10.04 LTS Lucid Lynx with this patch included.

Install a patch to indent wrapped lines in vim 7.3

  1. Make sure you have mercurial installed:

     sudo apt-get install mercurial 

  2. Get the dependencies necessary to build vim

    sudo apt-get build-dep vim-gnome 

  3. Download the vim source code:

    cd /tmp
    hg clone https://vim.googlecode.com/hg/ vim
    

  4. Download the patch from Eli Carter's retractile.net and place it in /tmp/vim/

  5. Apply the patch

    cd /tmp/vim
    patch -p1 < vim-7.3-breakindent.patch 
    
    Note the -p1 option tells patch to ignore the first slash and anything before it in the file name designated in the vim-7.3-breakindent.patch file. This lets you can name the repository whatever you want.

  6. Build vim

    make
    sudo make install
    

  7. Remove unnecessary files created during the build.

    make distclean
    

  8. If you already had vim on your system, make sure you are running the correct version:

    /usr/local/bin/vim

  9. Turn on the break indentation in vim

    :set bri

  10. Test the indentation in vim: Indent and type a line that wraps around. The wrapped part should have the same indentation as the first.
  11. Set your Gnome menu to use the new vim.

    • System > Preferences > Main Menu
    • Find GVim (maybe under accessories)
    • Click properties
    • Set the command to: /usr/local/bin/gvim -f %F

    Note the -f is a gvim option that causes it to run in the foreground. The %F is a Desktop Entry field code that causes it to take a list of file names.

  12. In your shell's startup file (e.g., ~/.bashrc or ~/.zshrc) add an alias so when you call vim you get your new version.

    alias vim='/usr/local/bin/vim'
    

References

Copyright 2000-2008 Jason Anderson.

Powered by Django, Photologue and Galleria.