# GIT heart FZF | |
# ------------- | |
is_in_git_repo() { | |
git rev-parse HEAD > /dev/null 2>&1 | |
} | |
fzf-down() { | |
fzf --height 50% "$@" --border | |
} | |
_gf() { | |
is_in_git_repo || return | |
git -c color.status=always status --short | | |
fzf-down -m --ansi --nth 2..,.. \ | |
--preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500' | | |
cut -c4- | sed 's/.* -> //' | |
} | |
_gb() { | |
is_in_git_repo || return | |
git branch -a --color=always | grep -v '/HEAD\s' | sort | | |
fzf-down --ansi --multi --tac --preview-window right:70% \ | |
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES | | |
sed 's/^..//' | cut -d' ' -f1 | | |
sed 's#^remotes/##' | |
} | |
_gt() { | |
is_in_git_repo || return | |
git tag --sort -version:refname | | |
fzf-down --multi --preview-window right:70% \ | |
--preview 'git show --color=always {} | head -'$LINES | |
} | |
_gh() { | |
is_in_git_repo || return | |
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always | | |
fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \ | |
--header 'Press CTRL-S to toggle sort' \ | |
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES | | |
grep -o "[a-f0-9]\{7,\}" | |
} | |
_gr() { | |
is_in_git_repo || return | |
git remote -v | awk '{print $1 "\t" $2}' | uniq | | |
fzf-down --tac \ | |
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {1} | head -200' | | |
cut -d$'\t' -f1 | |
} |
bind '"\er": redraw-current-line' | |
bind '"\C-g\C-f": "$(_gf)\e\C-e\er"' | |
bind '"\C-g\C-b": "$(_gb)\e\C-e\er"' | |
bind '"\C-g\C-t": "$(_gt)\e\C-e\er"' | |
bind '"\C-g\C-h": "$(_gh)\e\C-e\er"' | |
bind '"\C-g\C-r": "$(_gr)\e\C-e\er"' |
join-lines() { | |
local item | |
while read item; do | |
echo -n "${(q)item} " | |
done | |
} | |
bind-git-helper() { | |
local c | |
for c in $@; do | |
eval "fzf-g$c-widget() { local result=\$(_g$c | join-lines); zle reset-prompt; LBUFFER+=\$result }" | |
eval "zle -N fzf-g$c-widget" | |
eval "bindkey '^g^$c' fzf-g$c-widget" | |
done | |
} | |
bind-git-helper f b t r h | |
unset -f bind-git-helper |
This comment has been minimized.
This comment has been minimized.
@johnor could you please share your .vimrc, I'm having issues setting this up |
This comment has been minimized.
This comment has been minimized.
Any idea how i apply the key-bindings in ZSH? |
This comment has been minimized.
This comment has been minimized.
@faruzzy These functions are not for vim, they are for bash or zsh. If you want to browse the git history from vim you should take a look at fzf.vim (https://github.com/junegunn/fzf.vim) and :Commits. |
This comment has been minimized.
This comment has been minimized.
@johnor, thanks for the hint about @junegunn, the |
This comment has been minimized.
This comment has been minimized.
@parkercoates Maybe. FYI, the command works for me both on macOS and Linux.
I updated https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236/revisions |
This comment has been minimized.
This comment has been minimized.
@junegunn, my bad. I just realised that I had And thanks for incorporating my |
This comment has been minimized.
This comment has been minimized.
Thank you @junegunn, I am a big fan of your work! Is join-lines really required? I'm unsure about why it is there and it seems to work fine without it. |
This comment has been minimized.
This comment has been minimized.
@parkercoates, I can't seem to be able to get the keybindings to work for the fzf-g$c-widget. Can you try out the key-binding.zsh and let me know if it works for you? |
This comment has been minimized.
This comment has been minimized.
Just realized what was the problem: Basically, all my bindings worked, except for Control-gControl-b. Turns out Control-gControl-b was invoking my I've changed my prefix to Control-a and everything is working as expected. |
This comment has been minimized.
This comment has been minimized.
For Fish shell users: https://gist.github.com/aluxian/9c6f97557b7971c32fdff2f2b1da8209 |
This comment has been minimized.
This comment has been minimized.
no one feeling like creating a repository with these gems ? |
This comment has been minimized.
This comment has been minimized.
I've started one here https://github.com/teto/fzf-gems . Will try to beef up the readme |
This comment has been minimized.
This comment has been minimized.
Very nice! Any objection to replacing |
This comment has been minimized.
This comment has been minimized.
I had an issue getting gh to work, changing thanks for scripts |
This comment has been minimized.
This comment has been minimized.
One of the things that I find useful with these helpers is the ability to do a diff of two hash key. |
This comment has been minimized.
This comment has been minimized.
This is bloody amazing! In my case, the functions weren't working, and as it turns out it was because these conflicted with some existing function somewhere. So, I just prefixed all of your functions with |
This comment has been minimized.
This comment has been minimized.
I had some problems with gb() {
is_in_git_repo || return
git branch -a --color=always | grep -v '/HEAD\s' | sort |
fzf-down --ansi --multi --tac --preview-window right:70% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) -- | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##'
}
gr() {
is_in_git_repo || return
git remote -v | awk '{print $1 "\t" $2}' | uniq |
fzf-down --tac \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" --remotes={1} | head -200' |
cut -d$'\t' -f1
} |
This comment has been minimized.
This comment has been minimized.
Just solved another problem due to the fact that gh() {
is_in_git_repo || return
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always |
fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \
--header 'Press CTRL-S to toggle sort' \
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | head -1 | xargs git show --color=always | head -'$LINES |
grep -o "[a-f0-9]\{7,\}" | head -1
} |
This comment has been minimized.
This comment has been minimized.
for anyone who has oh-my-zsh there are already some alias so the solution is to add a prefix to the functions and modify the helper
|
This comment has been minimized.
This comment has been minimized.
Thanks for the work. There's a tiny issue that I met. When started with |
This comment has been minimized.
This comment has been minimized.
I can't get this working with pure prompt https://github.com/sindresorhus/pure Always getting this:
|
This comment has been minimized.
This comment has been minimized.
nvm, its conflict with existing gf function. I just prefixed it |
This comment has been minimized.
This comment has been minimized.
I am using git bash on Windows and I am getting error like << was unexpected at this time. on those function using <<< {}, any thoughts?
but actually below script works:
I've to change the function to use |
This comment has been minimized.
This comment has been minimized.
Great tool! |
This comment has been minimized.
This comment has been minimized.
hmmm. I can't seem to get bindings of the form ZSH in vi-mode |
This comment has been minimized.
This comment has been minimized.
Do you have |
This comment has been minimized.
This comment has been minimized.
That was it! Also in this process, I've discovered that |
This comment has been minimized.
This comment has been minimized.
Ah, cool. It looks like gists won't let me add a " |
This comment has been minimized.
This comment has been minimized.
This is beyond awesome. Thank you for making my console perfect with this tools. |
This comment has been minimized.
This comment has been minimized.
the main problem with me was the function names. Maybe june could update his gist to rename all functions fzf_gr or something similar i also couldnt use Ctl-G Ctrl-H because of my tmux/vim bindings on Ctrl-H which would change pane so I had to rebrand that to Ctrl-M for comMits or maybe Ctrl-G Ctrl-G (since the hash is the building block of git after all it also makes some sense) |
This comment has been minimized.
This comment has been minimized.
When I put any of these bindings into my ~/.inputrc I lose the ability to enter |
This comment has been minimized.
This comment has been minimized.
i never used inputrc so i have no idea what that is. |
This comment has been minimized.
Thanks, these functions are great! I had to add "zle reset-prompt" though in order to not mess up the prompt when using zsh.
eval "fzf-g$c-widget() { zle reset-prompt; LBUFFER+=$(g$c | join-lines) } "