From c3b1b2976860da9ce039720a1fc4b14055982938 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sun, 14 Jul 2024 02:34:04 +0200 Subject: [PATCH] zsh: cd to git repository root with ctrl-g in the line editor --- config/zsh/line-editor.zsh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/config/zsh/line-editor.zsh b/config/zsh/line-editor.zsh index 3ba3511..924d7e7 100644 --- a/config/zsh/line-editor.zsh +++ b/config/zsh/line-editor.zsh @@ -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