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
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.
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<"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With