How to stop line breaking in vim

Vim

Vim Problem Overview


I like that the long lines are displayed over more than one terminal line; I don’t like that vim inserts newlines into my actual text. Which part of .vimrc I should change?

Vim Solutions


Solution 1 - Vim

Use

:set wrap

To wrap lines visually, i.e. the line is still one line of text, but Vim displays it on multiple lines.

Use

:set nowrap

To display long lines as just one line (i.e. you have to scroll horizontally to see the entire line).

Solution 2 - Vim

> I like that the long lines are displayed over more than one terminal line

This sort of visual/virtual line wrapping is enabled with the wrap window option:

:set wrap

By default this will wrap at the first character that won't fit in the window. This means it will wrap in the middle of a word if that's where the window boundary lies. To change it to wrap on word boundaries, you can also:

:set linebreak

This will cause wrap to only wrap at the characters in the breakat setting, which defaults to space, tab, and small set of punctuation characters.

:set breatat
  breakat= ^I!@*-+;:,./?

> I don’t like that vim inserts newlines into my actual text.

To turn off physical line wrapping, clear both the textwidth and wrapmargin buffer options:

:set textwidth=0 wrapmargin=0

Solution 3 - Vim

I'm not sure I understand completely, but you might be looking for the 'formatoptions' configuration setting. Try something like :set formatoptions-=t. The t option will insert line breaks to make text wrap at the width set by textwidth. You can also put this command in your .vimrc, just remove the colon (:).

Solution 4 - Vim

Use :set nowrap .. works like a charm!

Solution 5 - Vim

:set tw=0

VIM won't auto-insert line breaks, but will keep line wrapping.

Solution 6 - Vim

You may find set lbr useful; with set wrap on this will wrap but only cutting the line on whitespace and not in the middle of a word.

e.g.

without lbr the li
ne can be split on
a word

and

with lbr on the
line will be
split on 
whitespace only

Solution 7 - Vim

set formatoptions-=t Keeps the visual textwidth but doesn't add new line in insert mode.

Solution 8 - Vim

Its strange that such a simple setting would require this amount of 'hocus-pocus' to work.

To answer your question now, for me it seemed to work with the combination of the following:

:set wrap linebreak nolist

(this seems to prevent existing lines from breaking, just wrap.)

AND

set formatoptions=l

(this prevents new/edited lines from breaking, while += does not do it for me as other settings/plugins seem to find space and add their own options which override mine.)

Solution 9 - Vim

If, like me, you're running gVim on Windows then your .vimrc file may be sourcing another 'example' Vimscript file that automatically sets textwidth (in my case to 78) for text files.

My answer to a similar question as this one – How to stop gVim wrapping text at column 80 – on the Vi and Vim Stack Exchange site:

> In my case, Vitor's comment suggested I run the following: > > :verbose set tw? > > Doing so gave me the following output: > > textwidth=78 > Last set from C:\Program Files (x86)\Vim\vim74\vimrc_example.vim > > In vimrc_example.vim, I found the relevant lines: > > " Only do this part when compiled with support for autocommands. > if has("autocmd") > > ... > > " For all text files set 'textwidth' to 78 characters. > autocmd FileType text setlocal textwidth=78 > > ... > > And I found that my .vimrc is sourcing that file: > > source $VIMRUNTIME/vimrc_example.vim > > In my case, I don't want textwidth to be set for any files, so I just commented out the relevant line in vimrc_example.vim.

Solution 10 - Vim

It is correct that set nowrap will allow you to paste in a long line without vi/vim adding newlines, but then the line is not visually wrapped for easy reading. It is instead just one long line that you have to scroll through.

To have the line visually wrap but not have newline characters inserted into it, have set wrap (which is probably default so not needed to set) and set textwidth=0.

On some systems the setting of textwidth=0 is default. If you don't find that to be the case, add set textwidth=0 to your .exrc file so that it becomes your user's default for all vi/vim sessions.

Solution 11 - Vim

I personnally went for:

  • set wrap,
  • set linebreak
  • set breakindent
  • set showbreak=ͱ.

Some explanation:

  • wrap option visually wraps line instead of having to scroll horizontally
  • linebreak is for wrapping long lines at a specific character instead of just anywhere when the line happens to be too long, like in the middle of a word. By default, it breaks on whitespace (word separator), but you can configure it with breakat. It also does NOT insert EOL in the file as the OP wanted.
  • breakat is the character where it will visually break the line. No need to modify it if you want to break at whitespace between two words.
  • breakindent enables to visually indent the line when it breaks.
  • showbreak enables to set the character which indicates this break.

See :h <keyword> within vim for more info.

Note that you don't need to modify textwidth nor wrapmargin if you go this route.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaiyuan ZhangView Question on Stackoverflow
Solution 1 - VimBrian RasmussenView Answer on Stackoverflow
Solution 2 - VimLaurence GonsalvesView Answer on Stackoverflow
Solution 3 - VimErik HesselinkView Answer on Stackoverflow
Solution 4 - VimVatsalaView Answer on Stackoverflow
Solution 5 - VimoripView Answer on Stackoverflow
Solution 6 - VimdavetapleyView Answer on Stackoverflow
Solution 7 - VimJimmy TranView Answer on Stackoverflow
Solution 8 - VimpetroslambView Answer on Stackoverflow
Solution 9 - VimKenny EvittView Answer on Stackoverflow
Solution 10 - VimCruciverbalistView Answer on Stackoverflow
Solution 11 - VimN. GimenezView Answer on Stackoverflow