diff --git a/README.md b/README.md index 2e5f54b..d4b7752 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,17 @@ A minimal Vim configuration that enforces git commit message best practices. ## Setup -### 1. Set Vim as your git editor +### Vim + +#### 1. Set Vim as your git editor ```bash git config --global core.editor "vim" ``` -### 2. Download the configuration +#### 2. Download the configuration -#### New to Vim +##### New to Vim If you don't have a `~/.vimrc` file yet, follow these instructions. This will create a new Vim configuration file for you. @@ -27,7 +29,7 @@ If you don't have a `~/.vimrc` file yet, follow these instructions. This will cr curl -fsSL https://raw.githubusercontent.com/jezsung/gitcommit-vimrc/main/.vimrc -o ~/.vimrc ``` -#### Existing Vim configuration +##### Existing Vim configuration If you already have a `~/.vimrc` file with your own customizations, follow these instructions. This will add the git commit settings to your existing configuration without overwriting it. @@ -43,6 +45,21 @@ Then add the source line to your existing `~/.vimrc`: echo 'source ~/.gitcommit.vim' >> ~/.vimrc ``` +### Neovim + +#### 1. Set Neovim as your git editor + +```bash +git config --global core.editor "nvim" +``` + +#### 2. Download the ftplugin + +```bash +mkdir -p ~/.config/nvim/ftplugin +curl -fsSL https://raw.githubusercontent.com/jezsung/gitcommit-vimrc/main/ftplugin/gitcommit.lua -o ~/.config/nvim/ftplugin/gitcommit.lua +``` + ## Usage Run `git commit` in your terminal. Vim will automatically detect the commit message file and apply this configuration. You'll immediately benefit from: diff --git a/ftplugin/gitcommit.lua b/ftplugin/gitcommit.lua new file mode 100644 index 0000000..547bb8c --- /dev/null +++ b/ftplugin/gitcommit.lua @@ -0,0 +1,39 @@ +-- Git commit message best practices: +-- - Subject line (line 1): max 50 characters +-- - Line 2: always blank +-- - Body (line 3+): wrap at 72 characters + +vim.opt_local.textwidth = 72 +vim.opt_local.colorcolumn = '73' +vim.opt_local.spell = true +vim.opt_local.spelllang = 'en_us' + +-- Truncate subject line if it exceeds 50 characters +local function truncate_subject() + local line = vim.fn.getline(1) + if #line > 50 then + vim.fn.setline(1, line:sub(1, 50)) + end +end + +-- Ensure line 2 is blank +local function ensure_blank_line() + local line_count = vim.fn.line('$') + if line_count >= 2 then + local line2 = vim.fn.getline(2) + if line2 ~= '' then + vim.fn.setline(2, '') + end + end +end + +local group = vim.api.nvim_create_augroup('GitCommitFormat', { clear = true }) + +vim.api.nvim_create_autocmd('InsertLeave', { + group = group, + buffer = 0, + callback = function() + truncate_subject() + ensure_blank_line() + end, +})