Take the 2-minute tour ×

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?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

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.

share|improve this answer
Ah, very nice garyjohn. I was missing # as "alternate file name" (see :help :_#). 0d_ is a nice touch too. –  MikeSep Mar 6 '12 at 20:04
add comment

:DiffOrig might be if use. It gives you a diff of the original, at least:

need another buffer.  This command is useful:
        \ | wincmd p | diffthis
between the current buffer and the file it was loaded from.

nodiff" before hiding it.
share|improve this answer
In my case, viewing diff wouldn't be that useful. I know what changes I made, but I want to see how exactly did the original look like. I could figure that out from the diff, but it adds a lot of noise. –  svick Mar 6 '12 at 17:30
Perhaps it is possible to build on :DiffOrig, though. Somehow :DiffOrig knows about the initial state - investigate the given command syntax. –  Daniel Andersson Mar 6 '12 at 17:36
Just remove "| diffthis | wincmd p | diffthis" from the end of the DiffOrig command. That will read the original file into a new buffer and stop. –  garyjohn Mar 6 '12 at 18:16
@garyjohn, thanks that works. If you post it as an answer, I will accept it. –  svick Mar 6 '12 at 19:10
add comment

Disclaimer: I have never needed to do this, but...

I think this one-liner might do the trick:

:new +read\ ++edit\ FILENAME

I haven't figured out yet how to auto-fill the actual filename... I was trying something with the filename-modifiers like %:p. I think the name needs to be stored into a variable before switching windows.

To explain, :new opens a new window, then runs :read inside that window to read the contents of FILENAME directly into that window's buffer. The buffer remains "unnamed", so there is no conflict as when you try to :split FILENAME (which just opens another copy of the same buffer).

It might also make sense to make this a scratch buffer (see the end of :help buffers), but I haven't experimented much with that.

share|improve this answer
add comment

Your Answer


