Is there an easy way to diff two fragments of code in two buffers?

33 views
Skip to first unread message

DwigtArmyOfChampions

unread,
Apr 17, 2025, 6:35:36 AMApr 17
to vim_use
I have two buffers open in vim. I want to compare a subroutine that's present in both buffers for differences. I don't care about the rest of the two files, they're very different. I only care about this subroutine. Is there an easy way to diff just this part of the code? I could go into Visual mode, yank the subroutine of buffer1, create a new file in my temp directory called sub_buffer1.txt, Put the code, save it, then switch to buffer2, go to visual mode, yank that buffer's subroutine code, create a new file in my temp directory called sub_buffer2.txt, Put that code, save it, exit Vim, then run vimdiff on sub_buffer1.txt and sub_buffer2.txt. Is there an easier way?

A. S. Budden

unread,
Apr 17, 2025, 7:11:04 AMApr 17
to vim...@googlegroups.com
On Thu, 17 Apr 2025 at 11:35, DwigtArmyOfChampions <dwightarmy...@hotmail.com> wrote:
I have two buffers open in vim. I want to compare a subroutine that's present in both buffers for differences. I don't care about the rest of the two files, they're very different. I only care about this subroutine. Is there an easy way to diff just this part of the code? I could go into Visual mode, yank the subroutine of buffer1, create a new file in my temp directory called sub_buffer1.txt, Put the code, save it, then switch to buffer2, go to visual mode, yank that buffer's subroutine code, create a new file in my temp directory called sub_buffer2.txt, Put that code, save it, exit Vim, then run vimdiff on sub_buffer1.txt and sub_buffer2.txt. Is there an easier way? 

The two thoughts that occurred to me off the cuff were:
  1.  You don't need to save the files - vim will do a diff on a buffer.  So you can do :vnew a couple of times, copy the bits you're interested in into the two new buffers, then do :diffthis on each.  No mucking around with temporary directories/files.
  2. Alternatively, you could diff the entire files with something like BeyondCompare and use the "Align With" function (F7) to tell it to align the start of each block for diffing (select the first line in the block on the left file, hit F7, then click on the first line of the block on the second file).  That would mean there would be loads of changes you could ignore and then a block that it compared nicely.  As far as I know, Vim doesn't have the equivalent of BeyondCompare's "Align With", but I could be wrong.
If there's a way to do #2 in Vim, I'd be very interested, but for any serious diffing I tend to reach for BeyondCompare anyway.

Al

Salman Halim

unread,
Apr 17, 2025, 7:49:28 AMApr 17
to vim...@googlegroups.com

There is a plug-in you can download from the Vim site called LineDiff that does exactly this.

Salman

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/CAOaJ26QERp1B1epFGwR-4_RouL4W9EfDTg8z9JwGSY-3vUNXWQ%40mail.gmail.com.

Tony Tam

unread,
Apr 17, 2025, 1:28:26 PMApr 17
to vim...@googlegroups.com
vim_use+u...@googlegroups.com

On Apr 17, 2025, at 4:49 AM, Salman Halim <salma...@gmail.com> wrote:


Salman Halim

unread,
Apr 17, 2025, 1:40:32 PMApr 17
to vim...@googlegroups.com

I wrote back a proper response, saying to use the LineDiff plugin. I didn't send that unsubscribe thing.

Salman

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.

Gary Johnson

unread,
Apr 17, 2025, 3:09:17 PMApr 17
to vim...@googlegroups.com
On 2025-04-17, Salman Halim wrote:
> There is a plug-in you can download from the Vim site called
> LineDiff that does exactly this.

I was about to recommend LineDiff when I saw your reply. I just
thought I'd add that one of the nice features of LineDiff is that it
creates a buffer for each of the blocks to be compared in a new tab,
and if you edit those buffers, the changes will be incorporated into
the original file(s) when you write and close those buffers.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages