vim command to restructure/force text to 80 columns

VimWord Wrap

Vim Problem Overview


I know there are ways to automatically set the width of text in vim using set textwidth (like https://stackoverflow.com/questions/235439/vim-80-column-layout-concerns). What I am looking for is something similar to = (the indent line command) but to wrap to 80. The use case is sometimes you edit text with textwidth and after joining lines or deleting/adding text it comes out poorly wrapped.

Ideally, this command would completely reorganize the lines I select and chop off long lines while adding to short ones. An example:

long line is long!  
short

After running the command (assuming the wrap was 13 cols):

long line is  
long! short

If this isn't possible with a true vim command, perhaps there is a command-line program which does this that I can pipe the input to?

After searching I found this reference which has some more options: http://www.cs.swarthmore.edu/help/vim/reformatting.html

Vim Solutions


Solution 1 - Vim

Set textwidth to 80 (:set textwidth=80), move to the start of the file (can be done with Ctrl-Home or gg), and type gqG.

gqG formats the text starting from the current position and to the end of the file. It will automatically join consecutive lines when possible. You can place a blank line between two lines if you don't want those two to be joined together.

Solution 2 - Vim

Michael's solution is the key, but I most often find I want to reformat the rest of the current paragraph; for this behavior, use gq}.

Solution 3 - Vim

You can use gq with any movement operators. For example, if you only want to reformat to the end of the current line (i.e. to wrap the line that your cursor is on) you can use gq$

You can also reformat by selecting text in visual mode (using `v and moving) and then typing gq.

There are other options for forcing lines to wrap too.

If you want vim to wrap your lines while you're inserting text in them instead of having to wait till the end to restructure the text, you will find these options useful:

:set textwidth=80
:set wrapmargin=2

(Don't get side-tracked by wrap and linebreak, which only reformat the text displayed on screen, and don't change the text in the buffer)

Solution 4 - Vim

Thanks to a comment from DonaldSmith I found this, as the textwidth option didn't reformat my long line of text (I was converting playing with hex-to-byte conversions):

:%!fold -w 60

That reformated the whole file (which was one line for me) into lines of length 60.

Solution 5 - Vim

If you're looking for a non-Vim way, there's always the UNIX commands fmt and par.

Notes:

  • I can't comment on Unicode, it may or may not behave differently.
  • @nelstrom has already mentioned using par in his webcast.

Here's how we would use both for your example.

$ echo -e 'long line is long!\nshort' > 3033423.txt
$ cat 3033423.txt
long line is long!
short
$ fmt -w 13 3033423.txt
long line is
long!  short
$ par 13gr 3033423.txt
long line is
long! short

To use from inside Vim:

:%! fmt -w 13
:%! par 13gr

You can also set :formatprg to par or fmt and override gq. For more info, call :help formatprg inside Vim.

Solution 6 - Vim

Almost always I use gq in visual mode. I tell my students it stands for "Gentlemens' Quarterly," a magazine for fastidious people.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwickedchickenView Question on Stackoverflow
Solution 1 - VimMichael MadsenView Answer on Stackoverflow
Solution 2 - VimAlex HirzelView Answer on Stackoverflow
Solution 3 - VimJohnGHView Answer on Stackoverflow
Solution 4 - Vimshearn89View Answer on Stackoverflow
Solution 5 - VimEhtesh ChoudhuryView Answer on Stackoverflow
Solution 6 - VimncmathsadistView Answer on Stackoverflow