sponsor Vim development Vim logo Vim Book Ad

Text (Especially LaTeX) Formatter : Format text or LaTeX text so that lines have approximately equal lengths

 script karma  Rating 17/10, Downloaded by 1111  Comments, bugs, improvements  Vim wiki

created by
script type
This script formats a paragraph of text so that lines have approximately equal length (set by textwidth). It has the following features:

1. It restores logical cursor location (the number of non-space chars from the start of the formatted text).

2. It merges multiple blank lines into one.

3. It is LaTeX aware (many commands and the comments) are recognized (and ignored) so that the source file does not look bad after formatting. It yields better (according to the author :-) results than fmt, or the VIM internal formatter.

4. It can be modified to adapted to your own likes (eg. more ignored code blocks) quite easily (see the functions s:CanJoin, s:BlockStart, s:BlockEnd).

5. It merges multiple spaces into one. (neither fmt nor VIM internal formatter does this).

6. For lines that start with a space/tab, they are not disturbed.

7. It is written in VIM script, so no external programs (awk/fmt/perl) are necessary. This is probably against the UNIX principle. But now that VIM offers scripting capability, one can argue that a VIM script is "simpler" (less reading of disk :-).

8. It can be quite easily extended to format a larger chunk of data (more than just a paragraph). Using the s:FormatLines function provided.

Comments/Suggestions/Improvements are welcomed. You can leave a comment at

Note1:  Also take a look at a similar script, Script 2187 http://www.vim.org/scripts/script.php?script_id=2187
install details
Save the file in the plugin directory (for Unix, this is likely ~/.vim/plugin).
Use K to format a paragraph in the normal mode. Redefine the mapping in the script (last line) if K is used for something else.

If you want to use it to format only .tex files only, then save the file in ./vim/ftplugin/tex.vim (or append to the end of that if it exists already).

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
format.vim 0.32 2008-07-27 6.0 lpb612 a small bug is corrected that affect how many paragraphs are formatted (should be one, but was two).
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
OSDN.net Logo