Wikia

Vim Tips Wiki

Changes: Par text reformatter

Edit

Back to page

(Remove html character entities)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 3: Line 3:
 
|previous=583
 
|previous=583
 
|next=586
 
|next=586
|created=October 10, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=mosh
 
|author=mosh
Line 15: Line 15:
 
To use:
 
To use:
   
Either ''':set equalprg=par''' and format using the magic '=', or assign to a key combination by putting the following in your vimrc (<tt>-j</tt> is to justify lines):
+
Either ''':set equalprg=par''' and format using the magic '=', or assign to a key combination by putting the following in your vimrc (<code>-j</code> is to justify lines):
   
 
<pre>
 
<pre>
Line 65: Line 65:
   
 
==See also==
 
==See also==
* [[Text formatting]]
+
*[[Text formatting]]
* [[Format a code block]]
+
*[[Format a code block]]
 
   
 
==Comments==
 
==Comments==

Latest revision as of 05:37, July 13, 2012

Tip 584 Printable Monobook Previous Next

created 2003 · complexity basic · author mosh · version 5.7


Vim has a very functional built-in formatter (:help formatting), but for raw formatting power nothing beats the Berkeley program 'par'. It can format, justify, align, slice and dice using a bewildering array of options. See http://www.nicemice.net/par/

To use:

Either :set equalprg=par and format using the magic '=', or assign to a key combination by putting the following in your vimrc (-j is to justify lines):

:set formatprg=par\ -w60
:map <A-q> {v}!par -jw60<CR>
:vmap <A-q> !par -jw60<CR>

Select a region and press <A-q> to format.

Here are a few examples from the 'par' docs. See the current par page for more.

Before:

/*   We the people of the United States, */
/* in order to form a more perfect union, */
/* establish justice, */
/* insure domestic tranquility, */
/* provide for the common defense, */
/* promote the general welfare, */
/* and secure the blessing of liberty */
/* to ourselves and our posterity, */
/* do ordain and establish the Constitution */
/* of the United States of America. */

After "par 59":

/*   We the people of the United States, in      */
/* order to form a more perfect union, establish */
/* justice, insure domestic tranquility, provide */
/* for the common defense, promote the general   */
/* welfare, and secure the blessing of liberty   */
/* to ourselves and our posterity, do ordain     */
/* and establish the Constitution of the United  */
/* States of America.                            */

UnicodeEdit

Note that 'par' doesn't handle Unicode correctly. There is a patch which adds multibyte support, (though at least the 'e' option is known not to work correctly as of Dec 2006).

A workaround, using an unpatched 'par' is:

cat <file> | iconv -f utf-8 -t <encoding> | par <options> | iconv -f <encoding> -t utf-8

replace <file> with filename, <encoding> with the single byte encoding that can represent the document. If the document is written in greek it is iso-8859-7 if it is east europe iso-8859-2 etc. Replace <options> with the par options you wish to use.

See alsoEdit

CommentsEdit

Around Wikia's network

Random Wiki