vimtricks.wiki Concise Vim tricks, one at a time.

How do I open the current file at a previous Git revision with vim-fugitive?

Answer

:Gedit HEAD~1:%

Explanation

When you are deep in a refactor, you often need to compare the current buffer with an older version of the same file. :Gedit HEAD~1:% from vim-fugitive opens that historical file directly in Vim without leaving your editing flow. This is faster than dropping to shell commands and manually locating a path at a commit.

How it works

  • :Gedit is fugitive's file-opening command for Git objects and working tree paths
  • HEAD~1 means the commit one parent behind current HEAD
  • :% means "the current file path" relative to the repo, so you do not need to type it

Together, HEAD~1:% resolves to "this same file, but at the previous commit".

Example

You are editing:

app/services/invoice_builder.rb

Run:

:Gedit HEAD~1:%

Now the buffer shows the previous-commit version of invoice_builder.rb for quick inspection or manual copy/paste into your current branch.

Tips

  • Use :Gedit main:% to view the version from your main branch
  • Use :Gdiffsplit HEAD~1 when you want side-by-side hunk navigation
  • Pair with :Gwrite if you intentionally want to stage the current buffer afterward

Next

How do I continuously record and restore sessions with vim-obsession?