Instead of wrapping lines visually, I want vim to wrap with a linebreak regardless of my the window dimensions. How can I set this automatically

2 Answers 2


You could work with wrapmargin:

set textwidth=0
set wrapmargin=1
set formatoptions+=t
set formatoptions-=l

The option wrapmargin defines the number of characters from the right window border where wrapping starts. This option is only used, if textwidth is set to zero.

The formatoptions flag t enables auto-wrap of text. By removing l from formatoptions, also lines that are already longer are wrapped.

See :help 'wrapmargin', :help 'formatoptions' and :help fo-table

  • "... regardless of my window dimensions." -- from the description of wrapmargin it is kind of the opposite. But I didn't know about it, thx.
    – Maxim Kim
    Feb 5, 2019 at 6:34
  • @MaximKim Guess the question has room for interpretation. We'll see what the TO meant, after he accepted an answer. :-)
    – Ralf
    Feb 5, 2019 at 6:38
  • I want it wraped using a linebreak. The problem is that it can feel difficult to tell where lines start and end when writing code, and it makes the find shortcut act strange.
    – Hussar
    Feb 5, 2019 at 7:29
  • @Hussar Set wrapmargin to 1. Guess made some mistake while testing.
    – Ralf
    Feb 5, 2019 at 7:35


set textwidth=50

The 'textwidth' option can be used to automatically break a line before it gets too long. Set the 'textwidth' option to the desired maximum line length. If you then type more characters (not spaces or tabs), the last word will be put on a new line (unless it is the only word on the line). If you set 'textwidth' to 0, this feature is disabled.


The line is only broken automatically when using Insert mode, or when appending to a line. When in replace mode and the line length is not changed, the line will not be broken.


In the comments I see you use "auto" linebreaks for coding.

Have you tried breakindent and friends?

enter image description here

  • then setting textwidth requires that I change it each time the dimensions of the window change. Why can't this be done for me?
    – Hussar
    Feb 5, 2019 at 7:15
  • 1
    It can be, using wrapmargin. But it depends on window dimension which you said in the question you want to avoid (or I read it that way :). textwidth doesn't depend on window dimensions and will insert linebreak at the specified column.
    – Maxim Kim
    Feb 5, 2019 at 8:07
  • I should have made that more clear. I suspected a solution like that would exist. Wouldn't this make each function behave like one long line and make it harder to move within the function?
    – Hussar
    Feb 5, 2019 at 8:36
  • Well, you don't have to put a whole function in one line. Breakindent helps visually break a line you don't want to automatically break physically(or hard) because it might change a semantic. Take a python for example, you are not allowed to break a line without adding special continuation char `\`.
    – Maxim Kim
    Feb 5, 2019 at 8:55

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.