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).


Tom said...

Did you know about "gw"? It limits lines to "textwidth" characters and with the syntax files I've been using, honors coding conventions.

Are you going to start posting again?

Alex said...

I have my Vim set up to not enforce textwidth in source code files. I pretty much just use this to wrap comments in source, as I use textwidth for everything else.

I work full time and I'm still in school, so posts are usually sparse, but I've got a few (some Vim stuff, some GDB stuff, and maybe some Python stuff) lined up for when I get some breathing room.

Stone Giant said...

Kind of curious. It appears that there is an extra forward slash in what you posted. Seems like it should look like this:

:map \br 80l? r

I'm using gVim, and so I know that escaping characters is sometimes a little different. Maybe you need to escape the question mark?

Sorry to bump an old post. Just found this really helpful. Thanks.

Stone Giant said...

Make that...

:map \br 80l? <CR>r<CR>

Looks like the comment utility thought I put tags in there.