merlin2011
merlin2011

Reputation: 75555

How do I get vim to wrap at 80 characters only for `gq` command?

Consider the following example file, which contains a single line with exactly 80 characters.

==foo     ===  ============================================================= bar

If I select this in Vim and type gq, it will transform perform text wrapping because it uses 79 characters for the threshold.

==foo     ===  =============================================================
bar

This is undesirable. I'd prefer to have vim wrap text only if it exceeds 80 characters. I attempted to use the approach suggested here.

:set textwidth=80

This has the unfortunate side effect of Vim automatically adding a line break as I'm typing.

How can I enforce the 80 character limit (instead of 79) only for the gq command, and not as I'm typing?

Upvotes: 0

Views: 1448

Answers (1)

Kevin
Kevin

Reputation: 30151

Remove t from 'formatoptions' and/or add l to disable wrap-on-insert.

See 'formatoptions' and |fo-table|. 'formatoptions' is a string of characters that control how 'textwidth' and gq work. The letter t has this meaning:

Auto-wrap text using textwidth

The letter l (lowercase L) is defined as:

Long lines are not broken in insert mode: When a line was longer than 'textwidth' when the insert command started, Vim does not automatically format it.

I believe removing t should be sufficient, but if not, you can add l as well. l is particularly useful for people who want t enabled for most lines, but also want the editor to "stop messing with my one intentionally long line."

Upvotes: 2

Related Questions