Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind zoxide to a keypress on zsh #357

Closed
omares opened this issue Mar 1, 2022 · 5 comments
Closed

Bind zoxide to a keypress on zsh #357

omares opened this issue Mar 1, 2022 · 5 comments

Comments

@omares
Copy link

omares commented Mar 1, 2022

Hey, I would like to bind the interactive version of zoxide to the key combination of ctrl+g in my z-shell.

I managed to make the keybind work, the interactive window pops up but the highlighted path is not navigated to when pressing enter. Has anybody experienced this? Not sure if it is my setup or an issue with zoxide.

The bindkey setup I have so far. The predefined aliases is changed to cd.

function _run-cdi { cdi; zle redisplay; }
zle -N _run-cdi{,}
bindkey "^G" _run-cdi

Thanks for your help!

@tomtomdurrant
Copy link

I have a similar issue but I find that I have to press enter twice. Once to select and then once to navigate.

@ajeetdsouza
Copy link
Owner

@omares AFAIK, that's because ZLE runs widgets in a subshell, so you can't cd from there. Try this instead:

function _run-cdi {
  result="$(zoxide query -i)"
  BUFFER="cd ${(q-)result}"
  zle redisplay
}

@tomtomdurrant the same limitation comes into play here, which is why the double-enter is necessary.

@kohane27
Copy link

kohane27 commented Jun 9, 2022

I'm using zsh, ranger and zoxide.

What the following script does when you press Ctrl-f:

  1. invoke zoxide to pick the target dir
  2. open the target dir in ranger
  3. when you leave ranger, stay in the last visited dir

Put the following to .zshrc:

zoxide_to_ranger () {
    eval 'ranger "$(zoxide query -i)" --choosedir=$HOME/.rangerdir < $TTY'
    LASTDIR=$(< ~/.rangerdir)
    cd "$LASTDIR" || exit

    local precmd
    for precmd in $precmd_functions; do
      $precmd
    done
    zle reset-prompt
}

zle -N zoxide_to_ranger
bindkey '^f' zoxide_to_ranger

Reference:

  1. zoxide query -i: Bind zoxide to a keypress on zsh #357 (comment) (@ajeetdsouza thanks!)
  2. < $TTY: https://unix.stackexchange.com/questions/475310/how-to-bind-a-keyboard-shortcut-in-zsh-to-a-program-requiring-stdin
  3. ensure precmds are run after cd: https://stackoverflow.com/questions/61075356/zle-reset-prompt-not-cleaning-the-prompt/63017856#63017856

Cheers!

@omares
Copy link
Author

omares commented Jul 22, 2023

Hey, after browsing the fzf source code, I stumbled across their cd-widget code and adapted it to solve my initial problem and it works!

This snippet shows the interactive zoxide selection when pressing CMD+G and changes directly into the selected directory after pressing enter, no need to re-confirm the cd.

_run-cdi() {
    local dir="$(eval "zoxide query -i")"

    if [[ -z "$dir" ]]; then
        zle redisplay
        return 0
    fi

    zle push-line
    BUFFER="builtin cd -- ${(q)dir}"
    zle accept-line
    local ret=$?
    unset dir
    zle reset-prompt
    return $ret
}

zle -N _run-cdi
bindkey "^G" _run-cdi

@DanielRivasMD
Copy link

Awesome !! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants