Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

3 Answers 3

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

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

: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.
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.