169

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
  • 21
    :set nowrap only prevents it from wrapping the display of lines, not from inserting linebreaks.
    – rampion
    Aug 17, 2009 at 20:46
  • For multiple windows, run: :windo set nowrap.
    – kenorb
    Oct 6, 2020 at 11:44

9 Answers 9

138
'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
5
  • 15
    Some plugins seem to override this setting. Apr 29, 2011 at 0:40
  • 1
    The tw and wp options didn't really work out for me so I had to go for formatoptions github.com/ain/.vim/blob/…
    – Ain Tohvri
    Jul 15, 2014 at 13:27
  • 2
    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/…
    – Alien_SM
    Mar 30, 2016 at 8:33
  • 5
    Removing filetype plugin on from my .vimrc did it for me.
    – user458541
    May 6, 2016 at 11:07
  • 2
    With this answer, you cannot use gq to realign the paragraph manually to textwidth. @Engineero's should be the correct one. Aug 17, 2018 at 23:31
126

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

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

8
  • 13
    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). Mar 22, 2016 at 18:34
  • 2
    This solution works for me at Mac OsX. Vim version 7.4
    – Shnkc
    Aug 18, 2017 at 11:09
  • This worked for me, I changed textwidth and wrapmargin first but this actually forced vim to update and rerender without any wrapped lines.
    – Kredns
    Apr 24, 2019 at 14:34
  • 2
    Ding din ding! Works in neovim
    – Gjaa
    Mar 13, 2020 at 16:40
  • What a magical moment when you find out that the command you really need is so expressive and simple. Thanks devs Jun 6, 2020 at 12:29
76

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
  • 3
    Thank you! Your solution is the only one that worked for me. No longer does vim insert newlines sometimes when I enter insert mode.
    – Geremia
    Feb 4, 2018 at 19:31
  • 3
    Yup. This is the real solution. Sep 27, 2018 at 16:49
  • 1
    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. Jul 28, 2021 at 7:55
34

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
  • set nowrap is the only one that worked for me. in neovim
    – ninja star
    May 27 at 0:04
15

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

1
  • 4
    This does not affect the automatic insertion of real line breaks, which this question is about. Aug 17, 2018 at 13:05
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
  • 2
    wrap and linebreak don't insert actual end-of-lines into the buffer, so that doesn't seem to be his problem.
    – A. Levy
    Aug 17, 2009 at 20:50
0

Vim may have to be in vi-compatible mode.

0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.