6

I recently discovered that I can use gq} instead of !}fmt to break up long paragraphs into lines. This works great.

Unfortunately, I ran into a bit of a problem. GNU fmt uses a default max line width of 75 columns. This works great. gq} on the other hand uses a line width of something like 79 columns (I think). Since I have set number enabled in my vim config, and the default terminal size in most GUI environments (MacOS, Linux) is 80x24, a line of 79 characters, plus the gutter bar that shows the line numbers, doesn't actually fit on my screen of 80 characters, and most lines wrap around.

It seems like using set textwidth=75 is an easy way to replicate the GNU fmt behaviour, and I was happy with that, until I found out that the textwidth setting affects other things, and not just gq}. It also forces vim to insert newlines while I type when I hit the textwidth number of characters on my line, which is super annoying.

Is there a way to make textwidth effect gq}, but not force vim to insert newlines as I type?

Automatically moving to the next line when the user hits textwidth characters seems like the kind of thing that would be nice for people who mainly write text (paragraphs), but terrible for coding, and most of what I do in vim is code.

1
  • FWIW, I have textwidth=80 for most things (but not all). I don't mind the automatic line-breaks, and I dont get that close to the margin most of the time. – D. Ben Knoble Jan 12 at 16:26
5

You can either set 'textwidth' to 0 and then gq{movement} will line break at screen width or you can set it to desired width and disable autowrapping by changing 'formatoptions' (alias 'fo') like so...

set fo-=tc

Per :h fo-table:

With 't' and 'c' you can specify when Vim performs auto-wrapping:

value  action
""     no automatic formatting (you can use "gq" for manual formatting)
"t"    automatic formatting of text, but not comments
"c"    automatic formatting for comments, but not text (good for C code)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.