The Rules
  • Feel free to leave constructive criticism, or point out a better way to do something.
  • Personal attacks or flames, on me or anyone else, will be deleted.
  • Past history has shown that 99% of comments I can't read (i.e. those in other languages) to be spam. Therefore, any comment I can't read will be removed.
  • I'm pretty mellow concerning profanity, but excessive (as determined subjectively by me), bad language will be removed.

Tuesday, January 27, 2009

Breaking long lines in Vim

Vim allows you to set a line length at which to break long lines, which I use in text files to break at 80 characters. However, in source files, I don't want it to break lines right at 80 characters lest it break in the middle of something that doesn't want to be broken in.

So I put the following in my .vimrc as a shortcut to breaking at 80 characters only when I want it to:

map \br 80\|? r

This tells Vim to go to column 80, search backward for the previous " " (space), and replace it with a carriage return.

I use this primarily for breaking comment lines at 80 characters. If Vim is set up properly (I can't remember the setting off the top of my head, but maybe I'll post it here later) the new line you inserted will be marked as a comment also (in the case of # or " comment markers, as this will obviously work with C/Java style multi-line comments).