Flaviu's Blog - Read'n'Code

View Old Git Version of a File in a Vim Split

Wouldn’t it be nice if you could view an old version of a file side by side with the current one in Vim? And it would scroll together? And syntax highlight?

Well, open the current file in a Vim window. Then do the following:

  1. Open a new (blank) split or window:
:vsp new
  1. Dump the old file contents in this new buffer: (note: ‘!’ executes a shell command, and ‘read’ puts it in the current buffer)
:read !git show HEAD^:path/to/file/from/root.py
  1. Set the right syntax highlighting:
:setf python
  1. Scroll bind the two splits: (note: you must run this command in both splits)
:set scrollbind

Hope this helps. Let me know if there’s a better way to do this.


Fork Flaviu on GitHub