0

I am trying to find (in Vim script) if the 2 files open using vimdiff are same or different? Is there a way to do that? (a variable like &diff perhaps?)

  • 1
    If the files are identical, there is one closed fold displayed on each side. Perhaps you could check for that. – Ralf Mar 29 '19 at 13:17
1

The easiest option is not to make vim do any work at all: diff -q reports if files are different, and diff -s if they are the same. According to this answer if you don’t need output, cmp -s works.

So in vimscript you might do

let l:files = [ get_file_names() ] " left as an exercise
call system('cmp -s ' . join(l:files, ' '))
" do something with v:shell_error
|improve this answer|||||
  • Thanks for the reply. I am looking for any sort of native solution. Don't want to depend on external programs for better portability. If nothing comes up, will use this :) – Ankit Jain Mar 29 '19 at 13:04
  • 1
    @AnkitJain, I think you should depend on external dependencies in this case, because vimdiff is supposed to be used for resolving merge conflicts and such, not calculation stuff. I skimmed through the help docs for vimdiff, and found nothing related to what you want. As I said, it is supposed to be interactive. But if you have constraints to fulfill, then you should be able to build up a vimscript function that ultimately checks itself if contents of two files are same or not. But I doubt you can do it with vimdiff itself. – klaus Mar 29 '19 at 13:46
  • @klaus you may be able to use the fact that vimdiff folds sections of the file that are the same, but as I learned creating auto-origami this kind of fold detection is hard – D. Ben Knoble Mar 29 '19 at 13:50
  • 1
    @D.BenKnoble, one answer using that approach is provided. – klaus Mar 29 '19 at 14:04
  • 1
    @klaus I guess I will accept this answer since there is nothing that can be done by Vim itself. – Ankit Jain Mar 29 '19 at 15:17
2

Okay, this is a very hacky approach. You really should check the result of diff using e.g. a custom diffexpr. But you asked for it, so here we go:

let a=win_getid()|exe "windo let g:diff=(get(g:, 'diff', 1) && foldclosed(1) && foldclosedend(1)==line('$'))"|if g:diff |echomsg 'no differences found' |else |echomsg 'there are some differences!' |endif|call win_gotoid(a)

(one line).

This assumes, that all windows are in diff mode and basically checks, whether each window contains only a single folded line.

|improve this answer|||||
  • As you said this is a hacky. Let me check it out nonetheless and see if this can work. Thank you. – Ankit Jain Mar 29 '19 at 15:19
  • 1
    Well, you asked for it... – Christian Brabandt Mar 29 '19 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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