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.
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
…. which should of course be combined with a proper setting of
set wrapmargin=60
or something
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 🙂
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:
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:
If I open latex files (and I dont want to reformat all day) I have an autocommand:
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
and add another SOMETEXT-X in the middle row with your setting it looks like:
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:
If you are interested in a more readable version try http://saulus[DOT]dyndns[DOT]org/~david/config/vimrc
(frequently updated)
Why use your own keybinding? Just use
formatprg
to tell vim to usepar
forgq
.You can also use
equalprg
to tell vim what program to use for the=
operator.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