182

I can't stop vim from wrapping my Python code. If I enter :set nowrap like a champ, but it still wraps.

I can hit J to unite the split lines of code, so it seems like a real carriage return is being inserted. I just don't understand why or how to stop it.

2
  • 22
    :set nowrap only prevents it from wrapping the display of lines, not from inserting linebreaks. Commented Aug 17, 2009 at 20:46
  • For multiple windows, run: :windo set nowrap. Commented Oct 6, 2020 at 11:44

10 Answers 10

144
'textwidth' 'tw' number (default 0) local to buffer {not in Vi} Maximum width of text that is being inserted. A longer line will be broken after white space to get this width. A zero value disables this. 'textwidth' is set to 0 when the 'paste' option is set. When 'textwidth' is zero, 'wrapmargin' may be used. See also 'formatoptions' and |ins-textwidth|. When 'formatexpr' is set it will be used to break the line. NOTE: This option is set to 0 when 'compatible' is set. 'wrapmargin' 'wm' number (default 0) local to buffer Number of characters from the right window border where wrapping starts. When typing text beyond this limit, an <EOL> will be inserted and inserting continues on the next line. Options that add a margin, such as 'number' and 'foldcolumn', cause the text width to be further reduced. This is Vi compatible. When 'textwidth' is non-zero, this option is not used. See also 'formatoptions' and |ins-textwidth|. {Vi: works differently and less usefully} 

If you refer to auto wrapping of long lines sending them to the next one, try

:set textwidth=0 :set wrapmargin=0 
Sign up to request clarification or add additional context in comments.

5 Comments

Some plugins seem to override this setting.
The tw and wp options didn't really work out for me so I had to go for formatoptions github.com/ain/.vim/blob/…
If tw and wp still doesn't solve the problem have a look at this post on SU which helped me solve my problem: superuser.com/questions/250112/…
Removing filetype plugin on from my .vimrc did it for me.
With this answer, you cannot use gq to realign the paragraph manually to textwidth. @Engineero's should be the correct one.
141

None of the other answers worked for me (IDK why).

:set wrap! Did the trick for me (using GVim for Windows).

8 Comments

This solves a slightly different problem. wrap provides the appearance of line wrapping, but doesn't actually break text onto new lines. You likely had wrap enabled, so :set wrap! toggled it off. You can check with :set wrap? which will echo the current value (i.e. wrap or nowrap).
This solution works for me at Mac OsX. Vim version 7.4
This worked for me, I changed textwidth and wrapmargin first but this actually forced vim to update and rerender without any wrapped lines.
Ding din ding! Works in neovim
What a magical moment when you find out that the command you really need is so expressive and simple. Thanks devs
|
88

set formatoptions-=t should do the trick. set formatoptions+=t will turn auto-wrapping back on.

For more information on what you can do with formatoptions, see the docs.

3 Comments

Thank you! Your solution is the only one that worked for me. No longer does vim insert newlines sometimes when I enter insert mode.
Yup. This is the real solution.
To turn off auto wrapping in a markdown file, I had to use set formatoptions=cq as explained by @Ain_Tohvri in a comment under another answer.
36

For preventing vim from wrapping long lines I use these two lines in my .vimrc:

set nowrap " do not automatically wrap on load set formatoptions-=t " do not automatically wrap text when typing 

1 Comment

set nowrap is the only one that worked for me. in neovim
15

To disable line wrap, you can enter :set wrap! or append this command to your ~/.vimrc.

1 Comment

This does not affect the automatic insertion of real line breaks, which this question is about.
11

Maybe it's the textwidth that's set, which automatically breaks lines when it reaches a certain length Try

:set tw=0 

If that fails play with e.g.

:set wrap linebreak textwidth=0 

and

:set virtualedit=insert 

1 Comment

wrap and linebreak don't insert actual end-of-lines into the buffer, so that doesn't seem to be his problem.
0

Vim may have to be in vi-compatible mode.

Comments

0

Open vimrc_example.vim (Yes, this is the file in Vim74) and set textwidth=0.

Comments

0

On macbook pro I outcommented in .vimrc the line

autocmd FileType text setlocal textwidth=78 

so it became

" autocmd FileType text setlocal textwidth=78 

.

(I installed a version of vim via homebrew.) This helped for all .txt files.

Comments

0

For clarity we should distinguish between soft wrap and hard wrap as explained in Difference between hard wrap and soft wrap? Soft wraps only appear on the screen, but the underlying text in the file is unchanged. Hard wrap actually introduce new line breaks this is what the OP wants to turn off.

Define a new command in .vimrc to turn hard wrap off.

command! Nowrap set formatoptions=cq 

Use it

:Nowrap 

I use this command when editing markdown tables with long lines for example.

Notes on related commands and tools:

  • you can also use the :set wrap and :set nowrap commands to turn on and off the soft wrap of long lines as they appear on the screen.
  • An auto formatter can wrap your lines at a predefined length, to be agreed with the people who work on the same project. For python you can use the black auto formatter for example. The documentation section on line length explains why black chose an 88 character line length.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.