259

I know there are ways to automatically set the width of text in vim using set textwidth (like 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

the Tin Man
  • 150,910
  • 39
  • 198
  • 279
wickedchicken
  • 4,073
  • 5
  • 20
  • 28
  • 9
    You might also be interested to learn that Vim can delegate text formatting to an external program, such as [par](http://www.nicemice.net/par/). I made a screencast which demonstrates how to [format text with par](http://vimcasts.org/e/18). – nelstrom Jun 13 '10 at 22:49
  • @nelstrom thanks, TIL about `par`. It's fantastic! – Steven Lu Dec 14 '13 at 15:52
  • 3
    When you want text to look slim & trim (well cut?), always think of `gq` -- [Gentleman's Quarterly](http://www.gq.com/). (Apologies for anthropomorphizing male, but the mnemonic works.) – ruffin Jul 13 '16 at 18:45

6 Answers6

406

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.

jpaugh
  • 5,719
  • 4
  • 33
  • 83
Michael Madsen
  • 51,727
  • 6
  • 69
  • 80
  • 18
    And it also works with visual selections. Ie something like: `V}gq`. In this way you see what you are going to format before actually formatting it. – Plouff Dec 02 '14 at 11:09
  • 9
    By the way, what exactly does `q` by itself? – Richard-Degenne Dec 06 '14 at 18:25
  • 1
    Is there a way to do this with lines that are just one long string of characters, with no spaces? EDIT: I've discovered the "fold" utility. Still wondering about doing this in vi. – Donald Smith Jun 02 '15 at 18:38
  • @DonaldSmith - I've added an answer below. – shearn89 Aug 24 '15 at 14:54
  • 8
    use `set tw=80` instead of `set textwidth=80` to save time – Yibo Yang Jan 25 '16 at 05:51
  • Is there a way to keep these settings applied so that when you enter more text into the document, it auto-wraps at 80? Or do you have to reapply these steps every so often to reformat the doc? – dmranck Feb 23 '16 at 22:04
  • 5
    @dmranck After `set textwidth=76` your lines will autowrap while typing. My complaint is that it doesn't do anything to existing lines being editing. But `V}gq` is extremely useful in that regard. `V` enters visual selection mode, `}` selects down to the next paragraph break, and `gq` executes the current formatter on it (or use `gw` to explicitly call the vi formatter). Also, `{` will select upwards. You can select the whole document and format with `ggVGgq` although maybe there's an easier way. Save that as a macro using `@` to make it easier. Use `vipgq` to fmt current paragraph. – ktbiz Apr 22 '17 at 17:37
  • `gw` does the same thing as `gq` except it keeps the cursor in place. – Shammel Lee Jun 15 '17 at 03:28
  • This doesn't have the same behavior as Emacs's `fill-region`. – Geremia Sep 14 '18 at 03:37
101

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}.

Alex Hirzel
  • 1,479
  • 2
  • 10
  • 18
  • 18
    expanding on this - you can use gq to format whatever you'd like. – Dan Schnau Aug 02 '13 at 14:45
  • 1
    Also, the 'nojoinspaces' option which prevents two spaces from appearing after periods when lines are joined at a sentence boundry. Good when your typographical style is single spaces between sentences in English. – Alex Hirzel Aug 27 '14 at 16:51
37

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)

JohnGH
  • 703
  • 7
  • 10
25

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.

shearn89
  • 651
  • 9
  • 17
  • 1
    This is very nice. All answers focus on limiting the width of a line when a line is split spaces into words. This solution works for very long words (DNA sequences). – pedrosaurio Feb 14 '18 at 07:49
  • 1
    For long urls this is about the only thing I've found that works. I already new about textwidth but it will not break unbroken strings (which is usually a good thing) – SkyLeach May 30 '18 at 16:34
15

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.

Ehtesh Choudhury
  • 6,882
  • 5
  • 37
  • 46
13

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

ncmathsadist
  • 4,227
  • 3
  • 26
  • 40