Skip to content

Instantly share code, notes, and snippets.

@hsawaji
Last active August 6, 2018 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsawaji/a886e60101056264acc5b0bc6c91de94 to your computer and use it in GitHub Desktop.
Save hsawaji/a886e60101056264acc5b0bc6c91de94 to your computer and use it in GitHub Desktop.
fzf_git.vim
" fzf git grep
command! -bang -nargs=+ Gitgrep call s:fzf_git_grep('<args>')
function! s:fzf_git_grep(args) abort
let dir = s:get_git_base_path(expand("%:p:h"))
if dir == -1
echo 'file not found .git'
return
endif
call fzf#run({
\ 'source': 'git grep -n -I -i ' . a:args,
\ 'sink': function('s:line_handler'),
\ 'dir': dir,
\ 'up': '~40%',
\ 'options': '+m'
\ })
endfunction
" fzf git ls-files
command! -bang -nargs=0 Gitfile call s:fzf_git_file()
function! s:fzf_git_file() abort
let dir = s:get_git_base_path(expand("%:p:h"))
if dir == -1
echo 'file not found .git'
return
endif
call fzf#run({
\ 'source': 'git ls-files',
\ 'sink': 'e',
\ 'dir': dir,
\ 'up': '~40%',
\ 'options': '+m'
\})
endfunction
" get directory what have .git
function! s:get_git_base_path(current_dir) abort
if a:current_dir == '/'
return -1
endif
if isdirectory(expand(a:current_dir . '/.git'))
return a:current_dir
else
let sp_dir = split(a:current_dir, '/')
call remove(sp_dir, -1)
return s:get_git_base_path('/' . expand(join(sp_dir, '/')))
endif
endfunction
" function for s:git_grep
function! s:line_handler(line) abort
let keys = split(a:line, ':')
" path
exec 'e '. keys[0]
" line
exec keys[1]
normal! ^zz
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment