Daniel
Daniel

Reputation: 1190

How can I make vim break lines only on whitespace?

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.

Upvotes: 11

Views: 4578

Answers (2)

Edward
Edward

Reputation: 1000

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.

Upvotes: 1

glts
glts

Reputation: 22734

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

Upvotes: 19

Related Questions