3

How to create a diff of the current text and the clipboard?

It could result in a vertical split showing the clipboard content, and a normal diff between them. Opening a vertical split, pasting and diffing should work, but there are the commands :diffsplit and :diffthis, so I feel I may be missing something.

3
  • 1. What do you call "the clipboard"? 2. What did you try?
    – romainl
    Commented Mar 20, 2023 at 19:33
  • i mean the system clipboard, like when cutting elsewhere with Ctrl-C and pasting with pin vim. I tried to manually create two documents to diff, but that is more like a workaround, not a solution. Commented Mar 21, 2023 at 21:39
  • Hi @Volker, I try to close the question. Do you still have one aspect of your question that is not answered and if so which one? Otherwise I'll be glad if you could accept one answer to allow the question to rest :-) Commented Mar 29, 2023 at 21:10

1 Answer 1

2

You could apply a variant of the Diff recipe:

function! DiffClipboard()
    let ft=&ft
    vertical new
    setlocal bufhidden=wipe buftype=nofile nobuflisted noswapfile
    :1put
    silent 0d_
    diffthis
    setlocal nomodifiable
    execute "set ft=" . ft
    wincmd p
    diffthis
endfunction
command! DiffClipboard call DiffClipboard()
  • let ft=&ft store the file type of the source buffer in the ft variable
  • vertical new create a new vertical window (aka split)
  • setlocal bufhidden=wipe butype=nofile nobuflisted noswapfile make the underlying buffer of the new window a temporary buffer
  • :1put paste the content of the default register after line 1 (if you want to use the * register replace with :1put *)
  • slient 0d_ delete the first line of the buffer (the clipboard content starts on line 2)
  • diffthis make the buffer part of the diff
  • set local nomodifiable make the buffer non modifiable
  • execute "set ft=" . ft align the buffer filetype with the source buffer filetype.
  • wincmd p switch back to the source buffer
  • diffthis make the source buffer part of the diff

Remark: the vertical buffer is created at the right side (if the splitright option is set) of the source buffer. I you want to switch the order you can add the wincmd r command after the vertical new command or unset the splitright option (:set nosplitright).

13
  • 1
    Consider normal! p or just put
    – D. Ben Knoble
    Commented Mar 21, 2023 at 13:26
  • 1
    How to swap left and right sides? How to specify register with clipboard buffer? Now "" or "0 is used, but I want to set "+ or "*. Is it possible to change color schema for diff result? Commented Apr 7 at 17:39
  • 1
    It will be good to have description of commands inside the function Commented Apr 7 at 17:41
  • 1
    Thanks for your feedback. I have updated the solution following your suggestion. Let me know if it make it more clear :-) Commented Apr 7 at 18:31
  • 1
    I have also enrich the solution to address the adaptation you would like to have to the solution. For the color schema of the diff that is another subject :-) There are special highlighting groups used by the diff (DiffAdd, DiffChange, DiffDelete, DiffText). Commented Apr 7 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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