einpoklum
einpoklum

Reputation: 131385

Prevent automatic line wrapping when editing git commit message in vim

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:

Upvotes: 14

Views: 2607

Answers (3)

Maxim Yarmolik
Maxim Yarmolik

Reputation: 107

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.

Upvotes: 1

filbranden
filbranden

Reputation: 8898

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.

Upvotes: 18

dar512
dar512

Reputation: 872

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

Upvotes: 2

Related Questions