zsh: cd to git repository root with ctrl-g in the line editor

This commit is contained in:
Fernando Schauenburg 2024-07-14 02:34:04 +02:00
parent d28bb6099f
commit c3b1b29768

View file

@ -52,6 +52,17 @@ bindkey -M viins '^[f' vi-forward-word
# Shortcut to start neovim.
bindkey -s '^v' '^unvim\r'
# cd to the root of the git repository (if within one).
cd-git-root() {
local top_level="$(git rev-parse --show-toplevel 2>/dev/null)"
[[ -z "$top_level" ]] && return 1
zle push-line
BUFFER="builtin cd -- ${(q)top_level}"
zle accept-line
}
zle -N cd-git-root
bindkey -M viins '^g' cd-git-root
if command -v fzf >/dev/null 2>&1; then
# Add `fzf` key bindings if it's installed:
# - CTRL-T: paste the selected file path(s) into the command line