Vim Tips Wiki
(More manual cleaning to standardise format)
(rm examples and duplicate utf8 example)
Line 51: Line 51:
 
/* and establish the Constitution of the United */
 
/* and establish the Constitution of the United */
 
/* States of America. */
 
/* States of America. */
</pre>
 
 
Or after "par 59f":
 
 
<pre>
 
/* 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. */
 
</pre>
 
 
Or after "par 59l":
 
 
<pre>
 
/* 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. */
 
</pre>
 
 
Before:
 
 
<pre>
 
1 We the people of the United States,
 
2 in order to form a more perfect union,
 
3 establish justice,
 
4 insure domestic tranquility,
 
5 provide for the common defense,
 
6 promote the general welfare,
 
7 and secure the blessing of liberty
 
8 to ourselves and our posterity,
 
9 do ordain and establish the Constitution
 
10 of the United States of America.
 
</pre>
 
 
After "par 59p12l":
 
 
<pre>
 
1 We the people of the United States, in order to
 
2 form a more perfect union, establish justice,
 
3 insure domestic tranquility, provide for the
 
4 common defense, promote the general welfare,
 
5 and secure the blessing of liberty to ourselves
 
6 and our posterity, do ordain and establish the
 
7 Constitution of the United States of America.
 
</pre>
 
 
Before:
 
 
<pre>
 
# 1. We the people of the United States.
 
# 2. In order to form a more perfect union.
 
# 3. Establish justice, ensure domestic
 
# tranquility.
 
# 4. Provide for the common defense
 
# 5. Promote the general welfare.
 
# 6. And secure the blessing of liberty
 
# to ourselves and our posterity.
 
# 7. Do ordain and establish the Constitution.
 
# 8. Of the United States of America.
 
</pre>
 
 
After "par 37p13dh":
 
 
<pre>
 
# 1. We the people of the
 
# United States.
 
# 2. In order to form a more
 
# perfect union.
 
# 3. Establish justice,
 
# ensure domestic
 
# tranquility.
 
# 4. Provide for the common
 
# defense
 
# 5. Promote the general
 
# welfare.
 
# 6. And secure the blessing
 
# of liberty to ourselves
 
# and our posterity.
 
# 7. Do ordain and establish
 
# the Constitution.
 
# 8. Of the United States of
 
# America.
 
 
</pre>
 
</pre>
   
 
===Unicode===
 
===Unicode===
Note that 'par' doesn't handle Unicode correctly. There is a [http://sysmic.org/dotclear/index.php?2006/06/22/55-add-multibyte-characters-support-in-par patch] available to add multibyte support, (though at least the 'e' option is known not to work correctly as of Dec 2006).
+
Note that 'par' doesn't handle Unicode correctly. There is a [http://sysmic.org/dotclear/index.php?2006/06/22/55-add-multibyte-characters-support-in-par 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:
 
A workaround, using an unpatched 'par' is:
<pre>
 
cat &lt;file&gt; | iconv -f utf-8 -t &lt;encoding&gt; | par &lt;options&gt; | iconv -f &lt;encoding&gt; -t utf-8
 
</pre>
 
replace &lt;file&gt; with filename, &lt;encoding&gt; 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 &lt;options&gt; with the par options you wish to use.
 
 
''or'' an HTMLized version.
 
 
<pre>
 
<pre>
 
cat &lt;file&gt; | iconv -f utf-8 -t &lt;encoding&gt; | par &lt;options&gt; | iconv -f &lt;encoding&gt; -t utf-8
 
cat &lt;file&gt; | iconv -f utf-8 -t &lt;encoding&gt; | par &lt;options&gt; | iconv -f &lt;encoding&gt; -t utf-8

Revision as of 19:26, 15 April 2008

Tip 584 Printable Monobook Previous Next

created October 10, 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.                            */

Unicode

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.

Comments