I'm looking for a quick and easy way to compare two pieces of
code inside a single file. I find it to be quite a common use
case to compare two functions or code block to see if they are
similar enough to be refactored out to a single function.

I've done this occasionally, and use the following method:

1) yank the 1st block
2) create a new window and paste the block
3) execute ":diffthis"
4) go back to the original window and yank the 2nd block
5) jump back to the diff'ed window and ":vnew" to get a vert window
6) paste the 2nd yanked block
7) execute ":diffthis" on the 2nd window

Given that there's no easy way to select two disjoint blocks in vim, there's not much way to automate this unless you resort to convention...something like using a fixed set of registers (as in the example below) or marks.

1) yank the 1st block into register "a"
2) yank the 2nd block into register "b"
3) :new
4) paste register "a"
5) :diffthis
6) :vnew
7) paste register "b"
8) diffthis

Steps #3-8 could be mapped, using the convention of those two given registers:

  :nnoremap <f4> :new|0put a|diffthis|vnew|0put b|diffthis<cr>

or whatever registers you desire.

-tim




Reply via email to