1

I don't understand what shall I set (I guess fo, but how?) for cutting lines that are longer than textwidth but without joining lines that are shorter.

For example, assume that I have textwidth option set to 78 and I have the following text:

This text is just an example to show what I want to achieve. 

However, I am failing in that and therefore I am asking for help.
Let's add a random enumerated list to make the example more complete.

    1. frog
    2. cow
    3. donkey

And let me add another line, just to make something more complete.
Now, let me add a very long line that it is not formatted at all and I want to cut it happened to be way too long

Hello,
My name
Is John Nash

If I type gggqG, I have the following:

This text is just an example to show what I want to achieve. 

However, I am failing in that and therefore I am asking for help.  Let's add a
random enumerated list to make the example more complete.

    1. frog 2. cow 3. donkey

And let me add another line, just to make something more complete.  Now, let
me add a very long line that it is not formatted at all and I want to cut it
happened to be way too long

hello My name Is John Nash

but what I want instead is the following:

This text is just an example to show what I want to achieve. 

However, I am failing in that and therefore I am asking for help.
Let's add a random enumerated list to make the example more complete.

    1. frog
    2. cow
    3. donkey

And let me add another line, just to make something more complete.  Now, let
me add a very long line that it is not formatted at all and I want to cut it
happened to be way too long

Hello,
My name
Is John Nash

Namely, I want that longer lines than textwidth are cut, but shorter lines are left untouched.

4
  • What did you try?
    – romainl
    Commented Apr 21, 2023 at 10:08
  • I tried set fo=tc
    – Barzi2001
    Commented Apr 21, 2023 at 11:40
  • The list could probably be solved with formatoptions or comments, but the last paragraph is trickier for automatic solutions.
    – D. Ben Knoble
    Commented Apr 21, 2023 at 15:36
  • That's not trying.
    – romainl
    Commented Apr 21, 2023 at 15:49

2 Answers 2

5

I would do:

:g/^.\{80\}/normal gqq

Or more generic as suggested by @Barzi2002

:execute 'g/^.\{' .. &textwidth ..'\}/normal gqq'
2
  • 2
    Very smart and elegant! I would just substitute 80 with &textwidht somehow ;-)
    – Barzi2001
    Commented Apr 21, 2023 at 11:41
  • 1
    Thanks for the feedback, I have adapted the code to take into account the textwidth. Commented Apr 21, 2023 at 11:45
2

Adding w to the default value of 'formatoptions' gives you the desired behavior:

:set formatoptions+=w

You shouldn't have stopped trying at set fo=tc. :help fo-t and :help fo-c are not related to the problem at hand… and :help fo-w is a few lines below.

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.