5

I would like a textwidth of 80 by default in vim, but if specific filetypes have their own text width (in particular gitcommit where tw=72) I would like vim to honour that width.

In my .vimrc I have the line:

set tw=80

I have also tried

setlocal tw=80

However this seems to override the gitcommit width of 72.

If I remove the line then git commits work fine (wrap at 72) but a text file (for example) will not wrap automatically.

Is it possible to have vim wrap to 80 if nothing else is specified, but otherwise follow the specific filetype instructions?

As an aside, I think this used to work up until recently. I have tried removing everything else from my .virmrc but the set tw=80, but this doesn't make any difference.

EDIT: If I open up a git commit message editor, and run

:verbose set tw?

vim displays:

   textwidth=80
        Last set from ~/.vimrc
Alex
  • 1,306
  • 3
  • 15
  • 25

1 Answers1

7

Vim has this covered with the global vs. buffer-local options. As you describe, you're supposed to :set a global default in your ~/.vimrc, and some filetypes may override the global default with :setlocal.

For troubleshooting, try

:verbose set tw?

This should tell you the last place that modified the option value.

Edit

For ft=gitcommit, it has special logic to only set the textwidth if (the global value) is empty:

if &textwidth == 0
    " make sure that log messages play nice with git-log on standard terminals
    setlocal textwidth=72
    let b:undo_ftplugin .= "|setl tw<"
endif

Your global settings prevents this from taking effect. The solution is to unconditionally set the textwidth yourself: In ~/.vim/after/ftplugin/gitcommit.vim, put this:

    setlocal textwidth=72
    let b:undo_ftplugin .= "|setl tw<"
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thanks for the tip, it looks like my tw is still being set from my .vimrc when in a git commit... Updated the question with this info. – Alex Apr 09 '13 at 01:06
  • So, now you have the global value but the filetype one is lost?! Isn't that exactly the reverse of what you initially reported? – Ingo Karkat Apr 09 '13 at 06:29
  • No, the problem is that if I set text width in my .vimrc, it overrides the git commit settings. That is it wraps at 80 instead of 72. This is the behaviour I've been getting, and your suggestion confirms that .vimrc is setting the width even in a git commit message editor. – Alex Apr 10 '13 at 00:22
  • 2
    This answer is pure gold. I finally understand how to override annoying mappings set by my plugins. – newUserNameHere Dec 22 '14 at 23:49