How to have a static view of the same file

vim

I am editing a file in vim and I'd like to create a new window (in a similar way to what :vsplit does), that contains a view of the file before I started editing.

In other words, I want to edit the text in the left window, but I want the text in the right window to remain unchanged.

Is there some way to do this in vim, or do I have copy the file to a temporary file and open that in the new window?

Best Answer

You could create a command similar to DiffOrig (see Daniel Andersson's answer and ":help DiffOrig) that would simply read the original file into a new buffer:

command ShowOrig vert new | set bt=nofile | r ++edit # | 0d_

To put the cursor back in the original window, add wincmd p:

command ShowOrig vert new | set bt=nofile | r ++edit # | 0d_ | wincmd p

The ++edit option was also added per MikeSep's answer and ":help DiffOrig" in recent releases of Vim.

Related Question