Prevent automatic line wrapping when editing git commit message in vim
Asked Answered
P

3

13

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.
Ploce answered 16/3, 2020 at 13:45 Comment(4)
Save your setting in a vim config file?Cornucopia
When you're editing a Vim commit, what filetype does it report? What's the output of :set ft? Is it gitcommit or just commit or something to that effect? Knowing the filetype, you can add an override to it.Greave
@evolutionxbox: "My settings" are getting overwritten. When I just start vim this doesn't happen.Ploce
@filbranden: See editPloce
G
18

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.

Greave answered 16/3, 2020 at 15:12 Comment(7)
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...Ploce
@Ploce 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.Greave
You have a typo in the second option, it says "formatoptions-=tc" but should say "formatoptions-=tl".Uitlander
Thanks @Rafał, great catch! I have just edited the answer to fix it. No wonder why einpoklum had trouble with it... Cheers!Greave
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.Banda
Thanks @Maëlan for the finding! I updated the answer to correct that command now.Greave
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.Labrum
M
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
Meekins answered 6/8, 2021 at 22:17 Comment(1)
Extracting only the t from formatoptions is enough and we don't need to wrap it in a function. So this will do: au Filetype gitcommit setlocal formatoptions-=tDashboard
M
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.

Monro answered 15/8, 2022 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.