10

I want vim to wrap long lines, but not split words in the middle. I found this post: Word wrap in Gvim

but that does not work for me. I already have l in formatoptions and linebreak enabled.

:set formatoptions?
formatoptions=lnq

As you can see, it is still splitting words: https://i.stack.imgur.com/diLlh.png

After consulting the relevant help pages, I also tried setting breakat to \s, but that didn't work either.

0

2 Answers 2

17

As you found out this is done using the 'linebreak' option (with 'wrap' on).

If on Vim will wrap long lines at a character in 'breakat' rather than at the last character that fits on the screen.

And since 'breakat' by default contains Space and Tab and some punctuation characters, this should break lines as expected (not in the middle of a word). I suggest resetting 'breakat' to the Vim default in case it has been changed by a plugin or a mapping.

Oh, and don't set 'list', these features don't mix.

All together now:

:set nolist wrap linebreak breakat&vim
3
  • 2
    So I have to choose between visible tabs and not breaking words for soft line wraps? That's very unusual and frustrating.
    – Daniel
    Oct 28, 2013 at 1:52
  • 1
    @Daniel Don't complain to me! This has actually been on the to-do list for a long time (:h todo). Until somebody takes the time to implement it, we won't have this feature, I'm afraid.
    – glts
    Oct 28, 2013 at 7:22
  • 2
    For future visitors to this page: this is changed now, you can use list and linebreak together.
    – ospider
    Sep 9, 2020 at 1:35
1

It depends whether you are talking about hard linebreaks, where a newline character is actually inserted into the text, or a soft linebreak, which only affects how the text is displayed on the screen. I suspect that you are referring to the latter.

If that is correct, then you want to :set list and probably add set list to your vimrc.

Besides, wrapping the display,just the display, not the actual text at word boundaries, list also causes Vim to display various "invisible" characters, such as spaces, tabs, and (hard) newlines, according to the listchars variable (see :help listchars for more); if you want the soft line-wrapping at word boundaries but not invisible characters, you could presumably set the listchars to nothing.

I personally find that I sometimes want list enabled and sometimes not. :set nolist turns it off and :set list! toggles it. And just for completeness, :set list? tells you whether is is currently enabled or not. Those are all standard Vim conventions.

And if that's too much typing, you could set up a custom key mapping in your .vimrc, but that's another question.

2
  • I actually have list set already: set listchars=tab:>-,trail:-. Whenever I :set nolist, the line wrap is no longer in the middle of the word, which puzzles me. The previous answer mentioned that the two are not compatible but I don't understand why.
    – Daniel
    Oct 28, 2013 at 1:50
  • Oh, right, my bad, sorry: word boundaries are ignored for the purposes of wrapping the display if list is enabled. I don't understand it either.
    – Edward
    Oct 28, 2013 at 2:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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