Skip to content

Instantly share code, notes, and snippets.

@dhamidi
Created October 5, 2021 08:24
Show Gist options
  • Save dhamidi/13ae615628661e3a38f2f3f18831529f to your computer and use it in GitHub Desktop.
Save dhamidi/13ae615628661e3a38f2f3f18831529f to your computer and use it in GitHub Desktop.
zle-bind-x
#!/usr/bin/env zsh
# zsh uses its own line editor instead of readline: zle.
#
# keys are bound to zle widgets
#
# Plan: create a widget, bind it to a key
# our custom zle widget for selecting a git branch using fzf
yank_git_branch() {
local branch=$(git branch | awk '{print $NF}' | fzf)
# zle uses a gap buffer, LBUFFER is what's left of the cursor
# and RBUFFER is what's right of the cursor
#
# Modifying either of the two keeps the cursor position in the right place (as opposed to modifying BUFFER).
RBUFFER="${branch}$RBUFFER"
}
# register the widget with zle
zle -N yank-git-branch yank_git_branch
# now we can bind it!
bindkey -v '\C-j' yank-git-branch # use -e if you aren't using Vi keys in zsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment