2023-04-14

Conventional Commits Vim Fugitive's Filetype Plugin

This note describes another application of Vim's filetype plugins (read my previous note for extending Netrw). It shows how to extend Vim-Fugitive, a convenient wrapper for GIT command-line popular SMC (Source Code Management) tool, with a step-by-step interactive guide for editing conventional commit messages.

Vim's configuration used in this note is summarized in the figure below.

  • Vim-Floaterm allows to easily create terminal windows.
  • Vim Fugitive provides a summary window with a specific filetype.
  • Deno JavaScript runtime is necessary for running the remote commitizen command-line script that will be displayed inside a terminal window.
  • The below filetype plugin for Fugitive's summary window which should be located inside one of Vim's runtime folders and named fugitive.vim.
  • FZF and GIT working in the command shell.
vim9script
# filename: ~/.vim/ftplugin/fugitive.vim 

if v:version < 900
    finish
endif

if isdirectory(".git") != 1
    echom "Not a git repo" 
    finish
endif

command -nargs=0 CCommit CCommit()

# remap default cc create commit
nnoremap  <buffer> cc :CCommit<CR>
   

# floaterm config

g:floaterm_wintype = 'vsplit'
g:floaterm_autoclose = 1
def CCommit(): void
    exec ":FloatermNew! set NO_COLOR=0 && 
    \ C:\\Path\\to\\deno.exe run --no-check --allow-run --allow-read 
    \ --allow-env https://tessarinseve.pythonanywhere.com/staticdeno/commitizen-deno/commitizen-deno"
enddef

As shown in the video below, you can create a new repository or modify an existing one. Open VIM Fugitive's summary buffer (:G command), and under the Untracked section, stage the files that you are interested to track. After staging you can edit a conventional commit message with the default Fugitive's keyboard map cc.