How do I define a filetype plugin for git commits in Neovim?

93 Views Asked by At

I want to set textwidth=72 when I'm editing a git commit. How do I load a git commit ftplugin in Neovim? I've tried defining the ftplugin/gitcommit.lua file, but it has no effect. (Other ftplugins in the same folder work as expected.)

3

There are 3 best solutions below

0
larsks On

I had this in my older Vim configuration:

au BufRead */COMMIT_EDITMSG set filetype=markdown tw=72

Using lua, that would look something like this:

vim.api.nvim_create_autocmd({ "BufEnter" }, {
  pattern = {
    "*/COMMIT_EDITMSG",
  },
  command = "set filetype=markdown tw=72",
})

...although with my current configuration this isn't necessary, and I can populate ~/.config/nvim/after/ftplugin/gitcommita.lua like this:

vim.o.textwidth = 72
vim.o.filetype = "markdown"

And it works as expected.

0
Sasgorilla On

Ensure you're setting the EDITOR or GIT_EDITOR variable correctly! I was defining ftplugin config for Neovim but git was using the system Vim. Pointing to the correct Vim executable resolved my issue.

0
romainl On

The built-in git ftplugin already sets :help 'textwidth' to the desired value so there is no point whatsoever in trying to do it on your own.

Just make sure filetype detection and plugins are enabled by putting this in your vimrc or whatever is the Neovim equivalent:

filetype plugin on