6

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?

3 Answers 3

4

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.

1
  • Ah, very nice garyjohn. I was missing # as "alternate file name" (see :help :_#). 0d_ is a nice touch too. Mar 6, 2012 at 20:04
2

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

Since 'diff' is a window-local option, it's possible to view the same buffer
in diff mode in one window and "normal" in another window.  It is also
possible to view the changes you have made to a buffer since the file was
loaded.  Since Vim doesn't allow having two buffers for the same file, you
need another buffer.  This command is useful:
     command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
        \ | wincmd p | diffthis
(this is in |vimrc_example.vim|).  Use ":DiffOrig" to see the differences
between the current buffer and the file it was loaded from.

A buffer that is unloaded cannot be used for the diff.  But it does work for
hidden buffers.  You can use ":hide" to close a window without unloading the
buffer.  If you don't want a buffer to remain used for the diff do ":set
nodiff" before hiding it.
4
  • 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, 2012 at 17:30
  • Perhaps it is possible to build on :DiffOrig, though. Somehow :DiffOrig knows about the initial state - investigate the given command syntax. Mar 6, 2012 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, 2012 at 18:16
  • @garyjohn, thanks that works. If you post it as an answer, I will accept it.
    – svick
    Mar 6, 2012 at 19:10
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .