Vim: Go to the changed block under the cursor from Git diff output

2012-01-18 22:16:47 +0900 / tag:vim tag:release tag:git / Comments

I often read Git diff output in Vim to review patches etc. While reading a diff output, I often open files listed in the diff output to check more code around changed lines.

Vim has a great command for this situation. It is gf. gf opens the file whose name is under the cursor. Since any diff output contains paths of changed files, all I have to do is to move the cursor to a path then type gf.

Though gf is a great command, the above operation is stressful. Because I have to:

Suppose that the current buffer contains the following text (note that the most left numbers are line numbers; please ignore them):

 1 diff --git a/autoload/gf/diff.vim b/autoload/gf/diff.vim
 2 index 469fdb3..b135316 100644
 3 --- a/autoload/gf/diff.vim
 4 +++ b/autoload/gf/diff.vim
 5 @@ -21,7 +22,7 @@
 6  "     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 7  "
 8  " Interface
 9 -function! gf#diff#go_to_hunk(type)
10 +function! gf#diff#go(type)
11    let d = gf#diff#investigate_the_hunk_under_the_cursor()
12    if d is 0
13      echomsg 'There is no diff hunk to jump.'
14 @@ -113,7 +114,7 @@ function! gf#diff#investigate_the_hunk_under_the_c
15        return 0
16      endif
17      let [d.from_path, d.to_path] = xs
18 +  call setpos('.', original_position)
19 -  call setpos(original_position)
20
21    return d
22  endfunction

What I want to do is to:

To realize the desired behavior, I wrote a plugin vim-gf-diff which extends gf, <C-w>f, etc to behave so.

Once I wrote vim-gf-diff, I noticed that there are similar situations to open a file by context. So I wrote also vim-gf-user to easily write gf extension like vim-gf-diff and to coexist multiple gf extensions.

I hope that these plugins help someone who have the same problem.