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

How do I view the git history for the current file as a navigable quickfix list using vim-fugitive?

Answer

:Gclog

Explanation

Running :Gclog in vim-fugitive loads the git log for the current file into the quickfix list. Each entry is a commit that touched the file, and you can navigate through them with :cnext/:cprev or the quickfix window, opening any historical version for inspection. This is much faster than running git log -- filename in a terminal and manually checking out commits.

How it works

  • :Gclog runs git log scoped to the current file and populates the quickfix list
  • Each quickfix entry corresponds to a commit that modified the file
  • Navigate entries with :cnext / :cprev (or ]q / [q with vim-unimpaired)
  • Opening a quickfix entry loads the file at that commit in a temporary buffer
  • :Gclog -- % is equivalent, where % refers to the current filename

Example

With the cursor in src/utils.py:

:Gclog

Opens the quickfix list with entries like:

fugitive://...HEAD~1:src/utils.py  | Fix edge case in parser
fugitive://...HEAD~5:src/utils.py  | Refactor util functions
fugitive://...HEAD~9:src/utils.py  | Initial implementation

Press <CR> on any entry to view that version of the file.

Tips

  • Use :Gclog -10 to limit the history to the last 10 commits
  • :Gclog! opens the log in a location list instead of the quickfix list, keeping the quickfix available for other uses
  • While viewing an old version, :Gread loads it into the current buffer
  • For the full repo log (not just the current file), use :Git log instead

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?