From 6679619c00c2ca7381f2ed72b29581a2f2750f2c Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Wed, 19 Mar 2025 06:01:14 +0530 Subject: [PATCH] bash: rewrite prompt after space-tab --- CHANGELOG.md | 6 ++++++ templates/bash.txt | 26 +++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 419c5ae..d47ae49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Changed + +- Bash: zoxide will now rewrite the prompt when using Space-Tab completions. + ## [0.9.7] - 2025-02-10 ### Added diff --git a/templates/bash.txt b/templates/bash.txt index 560a050..a4177a8 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -86,8 +86,6 @@ function __zoxide_doctor() { # When using zoxide with --no-cmd, alias these internal functions as desired. # -__zoxide_z_prefix='z#' - # Jump to a directory using only keywords. function __zoxide_z() { __zoxide_doctor @@ -101,10 +99,6 @@ function __zoxide_z() { __zoxide_cd "$1" elif [[ $# -eq 2 && $1 == '--' ]]; then __zoxide_cd "$2" - elif [[ ${@: -1} == "${__zoxide_z_prefix}"?* ]]; then - # shellcheck disable=SC2124 - \builtin local result="${@: -1}" - __zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}" else \builtin local result # shellcheck disable=SC2312 @@ -144,8 +138,11 @@ function {{cmd}}i() { # - Completions don't work on `dumb` terminals. if [[ ${BASH_VERSINFO[0]:-0} -eq 4 && ${BASH_VERSINFO[1]:-0} -ge 4 || ${BASH_VERSINFO[0]:-0} -ge 5 ]] && [[ :"${SHELLOPTS}": =~ :(vi|emacs): && ${TERM} != 'dumb' ]]; then - # Use `printf '\e[5n'` to redraw line after fzf closes. - \builtin bind '"\e[0n": redraw-current-line' &>/dev/null + + function __zoxide_z_complete_helper() { + READLINE_LINE="{{ cmd }} ${__zoxide_result@Q}" + READLINE_POINT={{ "${#READLINE_LINE}" }} + } function __zoxide_z_complete() { # Only show completions when the cursor is at the end of the line. @@ -157,12 +154,15 @@ if [[ ${BASH_VERSINFO[0]:-0} -eq 4 && ${BASH_VERSINFO[1]:-0} -ge 4 || ${BASH_VER \builtin compgen -A directory -- "${COMP_WORDS[-1]}" || \builtin true ) # If there is a space after the last word, use interactive selection. - elif [[ -z ${COMP_WORDS[-1]} ]] && [[ ${COMP_WORDS[-2]} != "${__zoxide_z_prefix}"?* ]]; then - \builtin local result + elif [[ -z ${COMP_WORDS[-1]} ]]; then # shellcheck disable=SC2312 - result="$(\command zoxide query --exclude "$(__zoxide_pwd)" --interactive -- "{{ "${COMP_WORDS[@]:1:${#COMP_WORDS[@]}-2}" }}")" && - COMPREPLY=("${__zoxide_z_prefix}${result}/") - \builtin printf '\e[5n' + __zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd)" --interactive -- "{{ "${COMP_WORDS[@]:1:${#COMP_WORDS[@]}-2}" }}")" && { + \builtin bind '"\e[0n": redraw-current-line' + \builtin printf '\e[5n' + + \builtin bind -x '"\e[0n": __zoxide_z_complete_helper "${result}"' + \builtin printf '\e[5n' + } fi }