Drew's Blog - Home - Archive

Vim: Better Line Wrapping With breakindent

When I'm writing with Vim, (which is almost all the time now,) I try to keep my lines to under 80 characters. This lets me have two vsplits on my small monitor and three on my large one, which is pretty much perfect. However, it's problematic when I enter a codebase that doesn't, because vim doesn't wrap lines very well. I spent a lot of time today trying to figure out how to fix this.

Enter breakindent, a patch that automatically indents wrapped lines up to the previous line. You can also use the showbreak option to automatically indent the line even more.

Installing on OSX

(I'm mostly writing this because I'm probably going to want to do this again, and I want to make sure I remember how to.)

First, get a copy of the vim source code. I recommend:
hg clone https://vim.googlecode.com/hg/ vim
Next, get a copy of the breakindent patch. My fork on Github should work for 7.4.x. (Otherwise, you have to go poking through various mailing lists on Google.)

Next, patch vim:
cd vim
patch -p1 < /path/to/breakindent.patch

And the rest of the installation process is standard:
./configure --prefix=/usr/local/ --with-features=huge
If you need ruby and/or python support, you may also want:
--enable-rubyinterp --enable-pythoninterp
This will install vim to /usr/local/bin/vim. Remember to add an alias or update your path accordingly. (You may need to sudo depending on the write permissions for /usr/local/.)
make && make install

Now, the good stuff! Update your .vimrc file to include set breakindent and now you're done! You may also want to use set showbreak=\ \ " comment so the space works. (See my .vimrc for an example.)

Yay, you're done! Go forth, and live in fear of long lines no more!

If you've read this far, you should probably follow me on Twitter.

See more posts

Drew writes code for fun and (sometimes) profit. He's currently studying Computer Science at Carnegie Mellon University. He has previously worked at Facebook, Amazon, and a startup called Intersect.