← Back to index

Open conflicted files in Vim

When working with other developers who overlap in the same areas of a code base, Git conflicts can be a common occurrence. A git status like this is all too familiar:

You are currently rebasing branch 'feature' on 'b0b1f65'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   bar.rb
        both modified:   foo.rb

I like resolving conflicts in Vim.1 For a long time I would refer to git status, manually open a file in Vim (with something like fzf), resolve the conflict, then repeat until done. I wanted to improve that flow to no longer require manually navigating to the conflicted files.

Vim has a feature called the argument list that let's you specify multiple files at launch. It will launch with the first file open in a buffer. You can move to the next file with :n and move back with :N.

vim one.txt two.txt

We can use git diff with --diff-filter to show only the conflicted changes, and --name-only to list only the file names .

> git diff --name-only --diff-filter=U
bar.rb
foo.rb

Combing these two, we can open the conflicted files in Vim with:

vim $(git diff --name-only --diff-filter=U)

Since this happens often, creating an alias can help reduce our typing:

git config alias.conflicts '!vim $(git diff --name-only --diff-filter=U)'

With an alias, and fugitive.vim, my flow now looks like:

  1. git conflicts to launch Vim with conflicted files

  2. Resolve the first file and stage the changes with :Gw

  3. Move to the next file with :n

  4. Repeat until no more conflicts

  1. I've tried other options, like vimdiff and Tower, but nothing beats the simplicity of Vim for me.