Set text width in vim without overriding a specific file type

I would like to get the default text width of 80 in vim, but if specific file types have their own text width (specifically gitcommit, where tw = 72), I would like vim to keep that width.

In my .vimrc, I have a line:

set tw=80 

I also tried

 setlocal tw=80 

However, this seems to override the width of gitcommit 72.

If I delete the line, git will do the job fine (wrap in 72), but the text file (for example) will not be automatically wrapped.

Is it possible to trim vim to 80 if nothing is specified, but otherwise follow the instructions for a specific file type?

Aside, I think this worked until recently. I tried to remove everything else from my .virmrc, but set tw = 80, but that doesn't make any difference.

EDIT : if I open the git commit message editor and run

:verbose set tw?

vim displays:

  textwidth=80 Last set from ~/.vimrc 
+4
source share
1 answer

With Vim, this is covered by the global buffer related options. As you described, you should :set global default in your ~/.vimrc , and some file types can override the global default with :setlocal .

For troubleshooting, try

 :verbose set tw? 

This should tell you the last place that changed the parameter value.

Edit

For ft=gitcommit , it has special logic to set only the text width if (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 do not allow this to take effect. The solution is to unconditionally set the text string: In ~/.vim/after/ftplugin/gitcommit.vim put this:

  setlocal textwidth=72 let b:undo_ftplugin .= "|setl tw<" 
+3
source

All Articles