5

I have a README.txt file as follows:

This document is a list of
a) Known issues in the services or test environment, i.e. things expected to fail
b) Things to be improved in the test suite
c) Possible bugs to be followed up

If I add anything to line 2, gVim automagically wraps it as follows

a) Known issues in the services or test environment, i.e. things expected to
fail blahblah

This is super annoying. I am running gVim on Windows, with the plugins "solarized" and "airline". I have tried to tame this behaviour with the following in my vimrc: (to no avail)

set nowrap              " don't wrap by default
set tw=0                " don't jump to newline after col 80

It only seems to happen with a .txt file, others are OK.

  • 2
    Inside the README file check the output of :verbose set tw? and :verbose set fo?. – Vitor Mar 30 '15 at 10:45
  • Output from tw: textwidth=78 Last set from C:\Program Files\vim\vim74\vimrc_example.vim Output from fo: formatoptions=tcq Last set from C:\Program Files\vim\vim74\vimrc_example.vim – roblogic Mar 31 '15 at 20:40
  • Commented out the offending line of vimrc_example.vim. Problem fixed, thanks – roblogic Mar 31 '15 at 20:51
  • 1
    Interestingly, all three of the answers below have useful and insightful information into why this setting might be behaving as such. I almost wish there was a 'extended accepted answer' reward that could be given in such situations. – reor Oct 7 '16 at 21:12
  • Agreed, I have upvoted all of the answers but can only choose one 'accepted' answer – roblogic Oct 10 '16 at 3:45
6

Try turning off automatic formatting:

set formatoptions=

If this works, you're going to want to try to track down how it's having an effect. There are two possibilities. Either you have 'wrapmargin' set (which causes Vim to insert newlines when you get within a certain distance of the edge of the window), or something is setting 'textwidth' after your .vimrc has been read. Use :verbose set wrapmargin? and :verbose set textwidth? to find out their values and what set them.

It's also possible that something is overriding your 'wrap' setting, causing the lines to display on several screen-lines, but you'd probably know if this were the case, because the wrapping would occur at the end of the window. Using the 'ruler' or 'showbreak' settings make this sort of wrapping easier to see.

UPDATE:

It turns out that the textwidth was set by an autocommand created in a file vimrc_example.vim in the Vim installation, which was being loaded at startup by the line source $VIMRUNTIME/vimrc_example.vim in the OP's .vimrc.

Other answerers have suggested editing the example file, or overriding the autocommand with further autocommands, but a cleaner solution would be to edit your .vimrc not to source the example file. If the example contains some settings you do want, these can be copied into your own .vimrc.

| improve this answer | |
  • Thanks, set formatoptions= fixed the behaviour. From my comment above it looks like gvim is taking settings from vimrc_example.vim so I'll rename the file to vimrc_example.vimx and see what happens – roblogic Mar 31 '15 at 20:44
  • PS: Vim 7.4 has default .vimrc that includes vimrc_example.vim which sets textwidth=78 – roblogic Oct 10 '16 at 3:49
3

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.

| improve this answer | |
3

As per Kenny Evitt's answer, you can find where textwidth is being set using verbose:

:verbose set tw?
  textwidth=78
    Last set from c:\Program Files (x86)\Vim\vim74\vimrc_example.vim

If you are happy editing that file, do so.

If you would prefer not to edit whichever file (or cannot), then you can override it in your own .vimrc (or _vimrc) file.

If it not being set via an autocmd, you can simply undo it.

set textwidth=0

If it is an autocmd, then it won't be set until you open the .txt file (or whatever) and simply setting textwidth=0 in your own .vimrc file won't do anything because it will be overridden.

Therefore, you need to prevent or override the autocmd.

To my way of thinking, you ought to be able to undo the autocmd using something like

autocmd! FileType text set textwidth=78

For me, that doesn't work in .vimrc. It does work if you give it manually once vim has started, but that's not so much use. This feels like a bug in vim, but maybe there's a good reason for it.

What does work for me is to add this to my .vimrc:

autocmd FileType text set textwidth=0
| improve this answer | |
  • 1
    Your :autocmd! will replace all existing autocommands for the FileType text event with your set textwidth 78 command. Is the 78 a typo, or is that really the command you were trying to use? – Rich Oct 10 '16 at 9:32
  • 1
    A better way of removing only the specific autocommand you don't like would be with with the command: autocmd! vimrcEx FileType text, which will only remove the FileType autocommands in the vimrcEx group. (i.e. just the autocommand you don't want). Be sure that this line occurs after the line where you source the example vimrc file. – Rich Oct 10 '16 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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