11

I want to do the opposite of this question:

Automatically wrap long Git commit messages in Vim

Somehow, git decided it wants to wrap my commit messages at 72 characters. I don't want them wrapped at all... and I didn't do anything to enable the wrapping.

Now, when I'm already editing a commit comment, I can of course enter:

:set textwidth&

which will stop the wrapping, but I don't want to have to do this every time.

Additional information:

  • I'm using Devuan GNU/Linux 3 (~= Debian 10).
  • :set ft? says filetype=gitcommit
  • When I just start vim, or edit any existing file with vim, no wrapping occurs.
einpoklum
  • 118,144
  • 57
  • 340
  • 684

3 Answers3

17

You are getting these settings for a git commit message because Vim recognizes the filetype (gitcommit) and loads filetype-specific settings for it.

In this case, it's coming from file $VIMRUNTIME/ftplugin/gitcommit.vim, which includes the following line:

setlocal nomodeline tabstop=8 formatoptions+=tl textwidth=72

You can override that by adding another filetype plugin for gitcommit to your home directory, one that will load after the one from the Vim runtime.

You can do that with a file named ~/.vim/after/ftplugin/gitcommit.vim (assuming you're using Vim, if you use NeoVim the initial part of the path will be different.) The after directory is used for plugin files that are loaded at the end, so by placing your file there you'll be sure your code will run after the one mentioned above.

In that file you can add a command to undo the unwanted effects of line wrapping, for example:

setlocal textwidth&

Or:

setlocal formatoptions-=t formatoptions-=l

Either of these two settings will prevent automatically breaking lines at column 72. The advantage of changing 'formatoptions' rather than resetting 'textwidth' is that by only changing 'formatoptions' you can still use commands such as gq to manually format a block of text to conform to the 72 character line width limit, if you wish to do so. You get the best of both worlds that way.

Whichever of the two options you decide to set, make sure you use :setlocal rather than :set, since that plugin is loaded for that buffer only, you should try to only modify the options on that buffer alone and avoid touching global options.

filbranden
  • 8,522
  • 2
  • 16
  • 32
  • 2
    I don't understand why you suggest two options, then tell me to make sure and only use one of them. Or - am I misunderstanding your last sentence? Also, the `setlocal textwidth&` works, the `formatoptions-=tc` doesn't... – einpoklum Mar 16 '20 at 15:25
  • @einpoklum Either of the two will prevent the automatic line breaks. You can set both together, but either one of them is enough. Last sentence is about using `:setlocal` and not `:set` with either of them! I'll rephrase so it looks better. – filbranden Mar 16 '20 at 15:29
  • 1
    You have a typo in the second option, it says "formatoptions-=tc" but should say "formatoptions-=tl". – Rafał G. May 26 '21 at 11:17
  • Thanks @Rafał, great catch! I have just edited the answer to fix it. No wonder why einpoklum had trouble with it... Cheers! – filbranden May 26 '21 at 13:32
  • 1
    About `set formatoptions-=abc` : I’ve discovered that `-=` does not work as expected for subtracting a set of letter flags. It only works if `'formatoptions'` contains `abc` as a *substring*, i.e. it contains the letters `abc` *in this exact order* and *with no other letter between them*. Otherwise nothing happens. You should remove them one by one. – Maëlan Aug 06 '21 at 22:23
  • Thanks @Maëlan for the finding! I updated the answer to correct that command now. – filbranden Aug 06 '21 at 23:13
  • 5
    If you are using Neovim the right path would be `~/.config/nvim/after/ftplugin/gitcommit.vim` on Linux, or `$env:LOCALAPPDATA/nvim/after/ftplugin/gitcommit.vim` on Windows. – pcesarperez Apr 20 '22 at 07:26
2

An alternative would be to add an autocommand to your vimrc. If you add it any time AFTER your

filetype plugin on

line, it will override whatever is in the ftplugin. I used this to turn off wrapping in git commits.

" Stops gitcommit from auto wrapping
au Filetype gitcommit call SetGitCommit()
func SetGitCommit()
    setlocal formatoptions-=tl
endfunc
dar512
  • 862
  • 8
  • 18
  • 2
    Extracting only the [`t`](https://vimhelp.org/change.txt.html#fo-table) from `formatoptions` is enough and we don't need to wrap it in a function. So this will do: `au Filetype gitcommit setlocal formatoptions-=t` – M Imam Pratama Jan 24 '22 at 16:32
1

If you are using Neovim the right path would be /.config/nvim/after/ftplugin/gitcommit.vim on Linux, or $env:LOCALAPPDATA/nvim/after/ftplugin/gitcommit.vim on Windows.

All credits to @pcesarperez; I just copied his comment from the first answer.

halfer
  • 19,824
  • 17
  • 99
  • 186