4

I know we can use :set tw=80 to limit the text width. However, if we insert text before the end, this function doesn't work at all.

For example, let's say we type "Would you like to have responses to your questions sent to you via email?"

If we continue to input after "?", tw works fine. But if we insert before say "have", it doesn't break off the line even if it exceeds the specified text width.

Is there any way to make this work in the latter case?

doubleDown
  • 8,048
  • 1
  • 32
  • 48
dspjm
  • 5,473
  • 6
  • 41
  • 62

2 Answers2

4

You may try

:set fo+=a

which reformats your paragraph as you type.

See :h fo-table.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Fancy, I knew it had to be something with `formatoptions` :-) – timss May 29 '13 at 11:11
  • This does solve the problem I mentioned, however, it's a bit not troublesome because vim doesn't allow you to start a newline with , as you type, two lines would be spliced if the last line hasn't reached tw. If I have to choose, I would use fo+=w instead. – dspjm May 29 '13 at 15:41
2

This might be configurable using formatoptions or formatexpr, but I'm not sure how.
Another solution is to manually format using gq on a visual line selection i.e. add a breakline if the text width is >= tw.

  1. Type in whatever text you like inside another block of text, thus making it 80 chars or more in width
  2. Select the text you think is misformatted using V (Shiftv)
  3. gq

From :h gq:

Format the lines that {motion} moves over.
Formatting is done with one of three methods:
1. If 'formatexpr' is not empty the expression is
   evaluated.  This can differ for each buffer.
2. If 'formatprg' is not empty an external program
   is used.
3. Otherwise formatting is done internally.

In the third case the 'textwidth' option controls the
length of each formatted line (see below).
If the 'textwidth' option is 0, the formatted line
length is the screen width (with a maximum width of
79).
The 'formatoptions' option controls the type of
formatting fo-table.
[..]

For more information on how Vim's textwidth works, take a look at
How to use Vim’s textwidth like a pro

timss
  • 9,982
  • 4
  • 34
  • 56
  • @dspjm Which solution you like best seems to depend on what workflow you have. Personally I don't like automatic wrapping and such, and prefer manually using `gq` in those rare occasions I need it. – timss May 29 '13 at 15:46
  • I know that, just waiting for some further information, but it seems it's not going to appear now. So I would mark you as accepted – dspjm May 30 '13 at 01:03
  • @dspjm Aye, wasn't meaning to push for an accept or anything, but it seemed like it was solved :-) – timss May 30 '13 at 12:50