vim-gitgutter icon indicating copy to clipboard operation
vim-gitgutter copied to clipboard

Version of GitGutterNextHunk that goes through all files

Open mkollaro opened this issue 10 years ago • 5 comments

I would like to have a command that would go trough ALL the hunks in the project, not just the current file. Is this already somehow possible?

mkollaro avatar Jul 27 '16 20:07 mkollaro

It's not built in but you could write a VimL function to wrap GitGutterNextHunk and go to the next buffer's next hunk when there are no more hunks in the current buffer.

airblade avatar Aug 08 '16 10:08 airblade

I've added a section to the README explaining how to do this.

You'll need commit 8576f00 for it to work.

airblade avatar Feb 22 '17 10:02 airblade

I think that is a good idea, and I think it's best if you map @airblade's suggestion to a capital C, such as:

nmap <silent> ]C :call NextHunkAllBuffers()<CR>
nmap <silent> [C :call PrevHunkAllBuffers()<CR>

This way you can still get the (sometimes preferred, if not usually) [c and ]c local buffer only behavior. This lower and uppercase convention is already useful in other areas of vim, and tpope really latches onto it with: https://github.com/tpope/vim-unimpaired/blob/d6325994b3c16ce36fd494c47dae4dab8d21a3da/doc/unimpaired.txt

Additionally, I don't really like the output when you're opening new buffers, or reaching the end hunk for a current buffer: "vim-gitgutter: No more hunks" (normal vim-gitgutter output/behavior), so I squelch that in this specific scenario (NOTE, you should probably reference the snippet from the README, and just note where I am putting silent! in the example below):

function! NextHunkAllBuffers()
  let line = line('.')
  silent! GitGutterNextHunk
  if line('.') != line
    return
  endif

  let bufnr = bufnr('')
  while 1
    silent! bnext
    if bufnr('') == bufnr
      return
    endif
    if !empty(GitGutterGetHunks())
      normal! 1G
      silent! GitGutterNextHunk
      return
    endif
  endwhile
endfunction

function! PrevHunkAllBuffers()
  let line = line('.')
  silent! GitGutterPrevHunk
  if line('.') != line
    return
  endif

  let bufnr = bufnr('')
  while 1
    silent! bprevious
    if bufnr('') == bufnr
      return
    endif
    if !empty(GitGutterGetHunks())
      normal! G
      silent! GitGutterPrevHunk
      return
    endif
  endwhile
endfunction

nmap <silent> ]C :call NextHunkAllBuffers()<CR>
nmap <silent> [C :call PrevHunkAllBuffers()<CR>

(It's just some additions of silent!)

shmup avatar Oct 31 '18 14:10 shmup

Ok this would be a lot more useful if you could [C and ]C (cycle) through all of the hunks/changes in the repo, regardless if the buffer is already open.

I need this, and I'll work on it

shmup avatar Nov 01 '18 18:11 shmup

Hey @shmup!

This is very late, but found a temporary solution that works quite well, while trying to find a way to cycle through all git changes/hunks across all edited files, please see below ->

  1. Set vim as default git editor in your terminal env file (i.e .zshenv)
export VISUAL=vim
export EDITOR="$VISUAL"
  1. Source your terminal env file (for me the file will be ~/.zshenv)
source ~/.zshenv // <-- change this to your terminal env file
  1. Add alias to ~/.gitconfig
[alias]
  edit = !$EDITOR $(git status --short | awk '$1 ~ /^M|A|U/ {print $2}')
  1. Add config from comment to your .vimrc -> https://github.com/airblade/vim-gitgutter/issues/362#issuecomment-434713641

  2. Inside your repo directory where you made multiple changes to different files, you can now run command

git edit

This will open all the files that you edited inside your repo in vim buffers, and then you can use [C and ]C to cycle through all the changes/hunks

Hope this helps!

KevinLeigh avatar Nov 17 '20 13:11 KevinLeigh