Search code examples
vimtextdifftext-editorvimdiff

Undoing diff put when copying lines between vimdiff windows


If, at a command prompt, I run

vimdiff file1 file2

I get a vim instance that has two files open side-by-side. Let's suppose that the text in the files looks like this (file1 is on the left, file2 on the right):

╔═══════╤═══════╗
║foo    │-------║
║bar    │bar    ║
║grue   │-------║
║~      │~      ║
║~      │~      ║
╚═══════╧═══════╝

Now suppose that my cursor is on the "f" of "foo" and that I wish to copy the first line of file1 to the first line of file2.

One way to do this is to select and yank (copy) the line with v$ y, and then use Ctrl+w l to move the cursor across to the first line of file2, and then type p to paste the copied line. If I do this and I then decide I didn't really want to do it after all, I can press u to undo the paste command I carried out in file2.

Another way to do it is to use the diff put command dp. However, if after doing this I decide I didn't mean to do it, I can't undo it simply by pressing u because my cursor is still in file1 and the u command will undo the most recent change to file1, not the most recent change to file2. So instead I have to use Ctrl+w l or Ctrl+w w to move the cursor to the window for file2 and then press u.

So, my question is: after I have used dp as above, is there any easy way for me to undo it without having to move my cursor across to the other file's window?


Solution

  • There is really no command for this but you could map your own. Put the following in your vimrc and then use du to do diff undos.

    nmap du :wincmd w<cr>:normal u<cr>:wincmd w<cr>