Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 36 ยท Workflows

Vim and Git

Use Vim with Git: write commit messages, compare files in diff mode (do, dp, ]c), resolve merge conflicts with vimdiff, and add vim-fugitive and gitsigns for a full workflow.

Git and Vim go together. Git opens your editor for commit messages and interactive rebases, and Vim has a built-in diff mode that shows differences side by side and lets you move changes between files. This lesson shows the pieces you meet daily and the plugins that make Git feel native.

Git uses your editor

Set Vim (or Neovim) as Git's editor once:

Terminal
git config --global core.editor "vim"

(Or rely on $EDITOR/$VISUAL.) Then git commit opens COMMIT_EDITMSG. Write a subject line of about 50 characters, a blank line, and a body wrapped at 72. Vim helps: the gitcommit file type highlights lines that are too long, and gq wraps selected text. Save and quit with :wq to commit; quit without saving (:cq returns an error code, which aborts) to cancel. An empty message also aborts.

git rebase -i opens a todo list. Change the leading word (pick, reword, squash, fixup, drop) with cw, or use :%s/^pick/fixup/ on many lines, and move commits with ddp. Then :wq. The related text objects and operators you learned earlier make this fast; :g/^/m0 reverses the order of every line.

Diff mode

Open two files side by side with changes highlighted:

Terminal
vim -d old.txt new.txt        # or: vimdiff old.txt new.txt

Inside Vim, :diffsplit other.txt compares the current buffer with another, and :windo diffthis turns existing windows into a diff. :diffoff! leaves the mode.

Diff mode highlights added, changed and deleted lines, aligns the windows with filler lines and folds unchanged text. The commands:

Command Meaning
]c / [c Jump to next / previous change
do (:diffget) Obtain: pull the other window's version of this change into this one
dp (:diffput) Put: push this version to the other window
:diffupdate Recompute the diff after edits
zo / zc Open / close a fold
Ctrl-w w Move between the two windows

A typical cycle: ]c to the next hunk, decide, do or dp, repeat, then :wa. u undoes in the current window.

flowchart LR
    L["Left: old.txt"] -- "dp (put)" --> R["Right: new.txt"]
    R -- "do (obtain)" --> L

Remember: do pulls the other side's change into the window you are in; dp pushes yours out.

JS Stand-in: find the changed lines (a diff)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Merge conflicts

When a merge or rebase cannot combine changes, Git writes conflict markers into the file:

TEXT
<<<<<<< HEAD
port = 8080
=======
port = 9090
>>>>>>> feature

You can fix them by hand: search for /^<<<<<<<, keep the correct lines, delete the markers, save and git add. Vim helps with a few moves: /\v^(\<{7}|\={7}|\>{7}) finds all markers, d with a motion deletes blocks, and :g/^<<<<<<<\|^=======\|^>>>>>>>/d removes all marker lines at once (after you have deleted the side you do not want).

For a visual approach, configure Git to use Vim's diff:

Terminal
git config --global merge.tool vimdiff
git config --global merge.conflictstyle diff3   # also shows the base
git mergetool

git mergetool opens a window layout with the LOCAL version (your branch), BASE (the common ancestor), REMOTE (the incoming branch) on top and the MERGED file at the bottom. Go to MERGED (Ctrl-w j), then pull a change from a window with :diffget LO, :diffget BA or :diffget RE (also :diffg RE). When done, :wqa saves everything and tells Git the conflict is resolved. If you quit with :cq, Git treats it as unresolved.

JS Stand-in: resolve conflict markers by choosing a side

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Plugins for Git

  • vim-fugitive (Vim and Neovim): :Git opens a status window where s stages, u unstages, = shows the diff inline and cc commits. :Gdiffsplit shows the staged version beside your file; :Git blame annotates lines; :GBrowse opens the file on the hosting site (with a companion plugin). Any :Git ... command passes through to the git program.
  • gitsigns.nvim (Neovim) or vim-gitgutter: mark added, changed and removed lines in the sign column and let you stage hunks, preview them or blame a line.
  • Diffview.nvim and neogit give review and magit-style interfaces.
  • Without plugins: :!git status, :r !git show HEAD:%, and :terminal still work from the previous lesson.

Diff-related options worth setting: set diffopt+=vertical,algorithm:patience,indent-heuristic for clearer diffs, set diffopt+=iwhite to ignore whitespace differences when you want that.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In diff mode, which command pulls the other window's version of a change into the current one?

    do (diff obtain) takes the other side; dp (diff put) sends yours.
  2. 2. How do you jump to the next change in diff mode?

    ]c goes to the next change and [c to the previous one.
  3. 3. In git mergetool with vimdiff, which buffer do you edit to produce the result?

    MERGED is the file that will be saved; pull changes in with :diffget LO, BA or RE.
  4. 4. How do you cancel a git commit from inside Vim?

    :cq quits with a non-zero exit code, which makes Git abort. An empty message also aborts.

Summary

  • Set Vim as Git's editor for commits and interactive rebases; :cq aborts.
  • vim -d or :diffsplit starts diff mode; ]c / [c jump, do obtains, dp puts.
  • git mergetool with vimdiff gives LOCAL, BASE, REMOTE and MERGED; use :diffget.
  • vim-fugitive and gitsigns add staging, blame and hunk actions inside the editor.

Next: Vi Keys Everywhere