Wednesday, 14 May 2008

Vim: The Word Processor

I love Vim. It took me a while to get there, but I've been using it exclusively for coding for a couple of years now and it has become second nature. I first learnt to use it during a practical exercise for an operating systems course at uni. We had to write a Minix driver so all the work had to be done on the command line. A powerful text editor was a must. I think I used Vi (rather than Emacs) because it was available and I had been told it was great. It was a steep learning curve, but I got a hang of the basics after a few days.

It was a couple of years before I started to use it again. In the meantime I'd mostly been working with .NET and Java so I'd been using some pretty decent development environments and it didn't seem necessary to use anything else (especially considering the power of their debuggers). But I moved into the world of the web and started writing PHP and then Python, these did not really have especially good IDEs and so it was back to a text editor and my choice was Vim. It also coincided with worsening RSI, for which Vim is great.I tend to learn a couple of features in a burst every few months when something really bugs me. This is probably not ideal, but Vim is so powerful I will never learn it all. I've been keeping a to do list in a text file recently (rather than on paper - go planet!) but Vim's defaults are not great for editing prose:
  • Vim's word wrap is by character, not word.
  • k and j (up and down) work on a line basis. If you have a wrapped line, you cannot move inside it with j and k (like you would with a normal text editor).
Of course, Vim is hugely powerful and can be tweaked to be much more useable when writing prose. These .vimrc commands:
  • Wrap lines by cutting lines off at word boundaries.
  • The word wrap is virtual, no extra line break is inserted (so that it's easy to edit afterwards).
  • j and k are replaced with gj and gk which allow you to move up and down inside a wrapped line.
  • I've also added the spell checker (I've not played around with it properly yet, but it looks a little weak).
  • smartindent for bullet points.

 autocmd BufRead *\.txt setlocal formatoptions=l
 autocmd BufRead *\.txt setlocal lbr
 autocmd BufRead *\.txt map  j gj
 autocmd BufRead *\.txt  map  k gk
 autocmd BufRead *\.txt setlocal smartindent
 autocmd BufRead *\.txt setlocal spell spelllang=en_us

On a slightly different note: if you're using OS X, get this port of Vim. It's aim (and it does) is to integrate better into Macs. It's worth getting just because it has pretty Carbon tabs ;-)


rohan said...

Thanks for this! Works a charm : )

All it needs now is to add in a couple of syntax highlighting rules for *bold* /italics/ _underline_ and it'll be perfect!

benwr said...

I don't know whether this is a thing in newer versions of vim, but for anyone trying to apply these to your config, you need a '.' before each asterisk in the regexps to get them to match properly.

Phoenix said...

Wow, I have been searching for the past couple of days for a way to do virtual line wrapping with Vim! Thanks for the post, now I can start using Vim as a word processor. Speaking of which, do you know any addons for Vim to make it edit LaTeX in suedo WYSIWYG way? Example: You would select text in Visual mode, then make it turn visually bold, underline, or surround it with special characters marking it as italic. You would also be able to apply left, center, right, and justify alignment visually. Then, when you save the document, it compiles the markup automatically.

Geoff said...

Hi Phoenix, I've not heard of anything like that, sorry.

What I often find is that I want to use vim commands in other editors. I'm a huge fan of the visual studio and eclipse vim plugins. So for example, perhaps there's a WYSIWYG editor that someone has written a vim plugin for, long shot, but you never know (and apologies to vim purists who probably consider this a heresy).