Skip to content

Instantly share code, notes, and snippets.

@DanilaMihailov
Last active April 22, 2022 20:45
Show Gist options
  • Save DanilaMihailov/c4c1a5e44305504ecaaf31cbd4da58e6 to your computer and use it in GitHub Desktop.
Save DanilaMihailov/c4c1a5e44305504ecaaf31cbd4da58e6 to your computer and use it in GitHub Desktop.
Select and checkout git branch using fzf in Vim
function! GitCheckoutBranch(branch)
" branch can look like this: "/remotes/origin/master [hash] info" or this: "master [hash] info"
let l:name = split(split(trim(a:branch), "", 1)[0], "/", 1)[-1]
" just show what is happening
echo "checking out ".l:name."\n"
" you can use !git, instead of Git, if you don't have Fugitive
execute "Git checkout ".l:name
endfunction
" -a option lists all branches (remotes aswell)
" -vv option shows more information about branch
" --color and --ansi enables colors
" --nth=1 makes sure you only search by names and not branch info
command! -bang Gbranch call fzf#run(fzf#wrap({'source': 'git branch -avv --color', 'sink': function('GitCheckoutBranch'), 'options': '--ansi --nth=1'}, <bang>0))
@DanilaMihailov
Copy link
Author

Looks like this
Screenshot 2020-06-16 at 14 03 37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment