gitvimword-wraptext-width

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:


Solution

  • 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.