Introduction
While writing my masters thesis I used a surprisingly large amount of time on reformatting my paragraphs in vim. I wanted paragraphs with a line width of 79 characters and even though my .vimrc had the setting set textwidth=79 to wrap the text automatically I spent a lot time manually reformatting certain paragraphs. New paragraphs will be well formated but when editing a already well formated paragraph the problem arises.

Reformating text is a waste of time and after a little research I found a great tool called par, see http://www.nicemice.net/par/. The Wikipedia article states:

The command effectively replaces white characters with space characters while revising word connections and line transitions for a more easily read text block. It also understands the conventions commonly used for quoting in email replies, and is capable of intelligently reformatting these several levels deep while rewrapping the text they quote.
http://en.wikipedia.org/wiki/Par_(command)

Installing par on any modern Linux version is quite simple:

root@bohr:~# aptitude install par

Integrate with vim
Using par from within vim is quite simple. In this example I have found a ugly formatted paragraph from my thesis:

I then selected the paragraph by using visual mode, see http://www.vim.org/htmldoc/visual.html and executed the command !par on the paragraph

The resulting paragraph is much nicer

This is of course a to tedious task for any lazy vim user, so I have binded F6 to execute the par command on the paragraph the cursor is currently in by adding the following to my .vimrc file.

map <F6> {!}par

Bonus tip – autoindent code
Another related tip is to use the autoindentation. As an example I have this unindented piece of code

By selecting the block in visual mode and typing “=” the block is autoindented based on the filetype.

## Only registered users can comment.

1. Rasmus says:

Maybe I dont fully understand the par-thingy, but I think in visual mode you can get the same effect by using the builtin: gq

2. Rasmus says:

…. which should of course be combined with a proper setting of

set wrapmargin=60

or something

3. I think the builtin reformatter is fmt and as far as I have read par is superior to fmt. But I do know what vim uses with gq. Thx for heads up 🙂

4. David Schaefer says:

Hi,

I totally understand the indentation problem that arises when writing text files with vim. So I spent some time on the vim help.
How the code is being reformatted is a setting on “cinoptions”. This specifies how autoindention is done on a specific filetype automatically and afterwards in visual mode using the “=” sign. So I put in my vimrc:

set cino=l1g1t0:0 " How should cindentation be accomplished
"        | | | |
"        | | | +-- ": place case labels N chars from prev. indent
"        | | +---- "t place function return type N chars from margin
"        | +------ "g place scope declarations N chars from indent of block
"        +-------- "l align with a case label instead of statement after

I hope you dont strip whitespaces above so my comments make sence.

There is some setting called “formatoptions” that does a lot what you want to gain! So I put in my vimrc:

set formatoptions=1crql " See Help (complex)

If I open latex files (and I dont want to reformat all day) I have an autocommand:

augroup VimrcTex
" iskeyword+=: for completing \ref{fig:something} with
autocmd FileType tex,plaintex call RmTrlSpc() | setl pm= sw=2 ts=2 sts=2 fo+=taw isk+=: spl=en_us spell noacd| silent! unmap m
augroup End

There you can see I add taw to formatoptions in case a tex file is being opened. This does EXACTLY what you want, i.e. if you have the setting

SOMETEXT-A SOMETEXT-B SOMETEXT-C linebreak
SOMETEXT-D SOMETEXT-E SOMETEXT-F linebreak
SOMETEXT-G SOMETEXT-H SOMETEXT-I linebreak

and add another SOMETEXT-X in the middle row with your setting it looks like:

SOMETEXT-A SOMETEXT-B SOMETEXT-C linebreak
SOMETEXT-X SOMETEXT-D SOMETEXT-E linebreak
SOMETEXT-F linebreak
SOMETEXT-G SOMETEXT-H SOMETEXT-I linebreak

My setting reformats the whole paragraph (all lines in it that doesnt end on a white space!). So the example from above in my setting looks like:

SOMETEXT-A SOMETEXT-B SOMETEXT-C linebreak
SOMETEXT-X SOMETEXT-D SOMETEXT-E linebreak
SOMETEXT-F SOMETEXT-G SOMETEXT-H linebreak
SOMETEXT-I

If you are interested in a more readable version try http://saulus[DOT]dyndns[DOT]org/~david/config/vimrc
(frequently updated)

5. Hi Chas – Thanks for the input – I didn’t really think about this when I wrote the article, but I have ended up using the built-in reformatter instead of par anyway, so gq is all I use now.

Kind regards
Thomas Jansson