0

I have set textwidth in .vimrc:

set tw=80

Format the sample file with gggqG:

test
=======================
::

    words_en = ['exercitationem', 'perferendis', 'perspiciatis', 'laborum', 'eveniet',
    'sunt', 'iure', 'nam', 'nobis', 'eum', 'cum', 'officiis', 'excepturi',
    'odio', 'consectetur', 'quasi', 'aut', 'quisquam', 'vel', 'eligendi']

It is converted into the following format:

test ======================= ::

    words_en = ['exercitationem', 'perferendis', 'perspiciatis', 'laborum',
'eveniet', 'sunt', 'iure', 'nam', 'nobis', 'eum', 'cum', 'officiis',
'excepturi', 'odio', 'consectetur', 'quasi', 'aut', 'quisquam', 'vel',
'eligendi']

Why can't be formatted as the below?

test 
======================= 
::

    words_en = ['exercitationem', 'perferendis', 'perspiciatis', 'laborum',
'eveniet', 'sunt', 'iure', 'nam', 'nobis', 'eum', 'cum', 'officiis',
'excepturi', 'odio', 'consectetur', 'quasi', 'aut', 'quisquam', 'vel',
'eligendi']
1
  • 1
    well, you told Vim to format the whole file. How should Vim know, that for the very first 3 lines, it should keep the lines and not put it into a single line? Because that's what you tell it with your textwidth setting? Commented Dec 9, 2022 at 8:27

1 Answer 1

0

If you are using the internal formatting routine (formatprg and formatexpr are empty), then how the text is formatted depends on the value of :help 'formatoptions':

:set fo?
  formatoptions=tcq

With the default value, Vim will try to join every line until it reaches textwidth. If you don't want that, you can add w:

:set fo+=w

which tells Vim to consider each line that ends with a non-whitespace character as a paragraph and thus to not attempt to join it with the surrounding lines. See :help fo-table.

But do you really want that behavior all the time? If not, do you really want to change the option before and after the formatting? Maybe a different approach is worth considering?

gggqG is not a random sequence of characters to learn in isolation. It actually is a sequence of two commands:

  • gg is a motion that puts the cursor on line 1,
  • gqG is an operator, gq, applied to the text covered by a motion, G, which moves the cursor to the last line.

Therefore, a literal translation of gggqG would be:

"move the cursor to the first line and then format the text from there to the last line"

which really means:

"format the whole buffer"

You shouldn't be surprised if Vim does what you ask it to do.

If you don't want to format the whole buffer, then there is no reason to ask Vim to do that.

If you want to format the text from a specific line, say line 5, to the last line, the proper command would be:

5GgqG

where 5G moves the cursor to line 5 and gqG formats the text from there to the last line.

If you want to format the text from the current line to the last line, the first part, where you move the cursor to the first line you want to format, is unnecessary so the proper command would be:

gqG

where gqG formats the text from the current line to the last line.

If you want to format the text between line 5 and line 25, the proper command would be:

5Ggq25G

where 5G moves the cursor to line 5 and gq25G formats the text from there to line 25.

Of course, you can use any motion for the initial positioning and for the operator:

?foo<CR>gq'k

moves the cursor to the first foo before the cursor and then formats the text from there to mark k.

You can even forgo the initial positioning and use a text object for gq if that suits your needs better:

gqip

See :help gg, :help gq, :help G, :help ?, :help ', and :help ip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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