tags:

views:

77

answers:

2

Is it possible to diff or even vimdiff two almost similar fortran subroutines which occur in the same file? If so, how?

I can think of copying the two subroutines in two separate files and then diff them, but is there a way to do it within the original file?

Thanks!

+2  A: 

You cannot do this within the original file, but you can do this without using separate files, only separate buffers. This should work if you copied one subroutine in register a (for example, with "ay typed in visual mode) and other subroutine in register b:

enew | call setline(1, split(@a, "\n")) | diffthis | vnew | call setline(1, split(@b, "\n")) | diffthis

To automate:

let g:diffed_buffers=[]
function DiffText(a, b, diffed_buffers)
    enew
    setlocal buftype=nowrite
    call add(a:diffed_buffers, bufnr('%'))
    call setline(1, split(a:a, "\n"))
    diffthis
    vnew
    setlocal buftype=nowrite
    call add(a:diffed_buffers, bufnr('%'))
    call setline(1, split(a:b, "\n"))
    diffthis
endfunction
function WipeOutDiffs(diffed_buffers)
    for buffer in a:diffed_buffers
        execute 'bwipeout! '.buffer
    endfor
endfunction
nnoremap <special> <F7> :call DiffText(@a, @b, g:diffed_buffers)<CR>
nnoremap <special> <F8> :call WipeOutDiffs(g:diffed_buffers)<CR>

Note that you may want to set hidden option if Vim refuses to abandon changed file (see :h abandon).

ZyX
+2  A: 

you can try Block diff vim plugin, it will make 2 new buffer in a new tab to show the differences.

Vincent
I saw its code: it does almost exactly what my solution above does, but it is not able to close opened buffers by pressing one key.
ZyX
Very nice, but it only works when you have a GUI, which is not always my case when I work on a remote server. So, I chose to accept the answer by ZyX. Thanks anyway!
Nigu