Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set textwidth in vim without overriding filetype specific

Tags:

vim

word-wrap

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
like image 269
Alex Avatar asked Apr 08 '13 01:04

Alex


1 Answers

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<"
like image 84
Ingo Karkat Avatar answered Nov 29 '22 07:11

Ingo Karkat