11

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.sstatic.net/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

19

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 
Sign up to request clarification or add additional context in comments.

3 Comments

So I have to choose between visible tabs and not breaking words for soft line wraps? That's very unusual and frustrating.
@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.
For future visitors to this page: this is changed now, you can use list and linebreak together.
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 Comments

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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.