7

By default, using gq with a movement command hard-wraps the text described by the movement command to 79 columns. (I'm relatively new to vi/vim so I don't know the correct terminology.) How can I configure that column count so that instead it wraps at a different number of columns?

For example, suppose I have this text:

This is text for illustration.

I'd like to be able to configure the wrap column to 29 so that if my cursor is in the line and I run gqip it is transformed to:

This is text for
illustration.

I'm using gVim on Ubuntu GNOME 17.04, if that makes a difference.


EDIT: I'm looking for a way to manually wrap certain sections of text to the given line length without affecting any others, because there are some cases where I'm OK with going past the limit. To expand on the previous example, if my document looks like this:

This is text for illustration.

This is some other text for illustration.

And I run gqip with my cursor in the first line, I'd like to get:

This is text for
illustration.

This is some other text for illustration.

Then if I start typing on a new line below that, I'd like that line to similarly be unaffected by the limit.

My use case is in writing LaTeX documents: I'd like to be able to wrap paragraphs of prose to a certain number of columns on demand, but in certain cases where I'm writing long equations, or tables, I want those lines to stay longer than the limit.

3

You can do this by setting textwidth, and removing t from formatoptions.

set textwidth=29
set formatoptions-=t

You also need to have the q option enabled in your formatoptions to use gq. It seems to always be on for me, but you might want to add a set formatoptions+=q to be on the safe side.

Shouldn't be a problem with your current usecase, but the c option auto-wraps comments, so if you don't want that do set formatoptions-=c.

For more info see:

:help formatoptions " Not much info.
:help fo-table      " All the options in detail.
0

There are two ways to do this: setting 'textwidth' to the width of your text in characters or columns; setting 'wrapmargin' to the width of the right margin in characters or columns. For example, to wrap your text to a width of 29 characters or columns:

set textwidth=29

See

:help 'textwidth`
:help 'wrapmargin'
:help 10.7
  • Thank you for your response, but this isn't exactly what I was looking for. I want to be able to manually hard-wrap some sections of text without affecting others. For me, both of these options automatically hard-wrap new text that I enter. I'll update the question to better explain this. – jdw1996 Jul 3 '17 at 16:19
  • Vim does not have a built-in way to do that. The gq command does not provide for setting the wrap width per invocation; it follows textwidth and textwidth applies to the entire buffer, not just certain lines. If you're okay with manually wrapping the sections you want wrapped, you could write a function that would: save the current value of textwidth; set textwidth to 29; perform gq on the selected lines; restore the value of textwidth. Then assign this function to formatexpr. Alternatively, you could use an external program such as fmt to format selected lines. – garyjohn Jul 3 '17 at 18:02
  • OK, I see. I'll look into writing such a function. Thanks! – jdw1996 Jul 3 '17 at 22:03

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.