Last active
April 22, 2022 20:45
-
-
Save DanilaMihailov/c4c1a5e44305504ecaaf31cbd4da58e6 to your computer and use it in GitHub Desktop.
Select and checkout git branch using fzf in Vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like this