Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@ 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.

```bash
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.

Expand All @@ -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:
Expand Down
39 changes: 39 additions & 0 deletions ftplugin/gitcommit.lua
Original file line number Diff line number Diff line change
@@ -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,
})