zle reset-prompt not cleaning the prompt

1.2k Views Asked by At

I have a key binding to go up by one directory (very useful):

# C-M-u: up-directory
up-directory() {
    builtin cd .. && zle reset-prompt
}
zle -N up-directory
bindkey '\e\C-u' up-directory

It works well, except that the prompt is not really reset.

Example, starting in a Git repo (~/.dotfiles):

enter image description here

After C-M-u, I get:

enter image description here

So, I'm well one level up (into ~), but the Git info is still there, while not valid anymore -- I'm not in a Git repo anymore

How to fix this?

1

There are 1 best solutions below

1
On

You probably need to execute the precmds before resetting the prompt.

fzf's zsh integration does this:

# Ensure `precmds` are run after `cd`
fzf-redraw-prompt() {
  local precmd
  for precmd in $precmd_functions; do
    $precmd
  done
  zle reset-prompt
}

So, try something like this:

up-directory() {
    builtin cd ..
    if (( $? == 0 )); then
        local precmd
        for precmd in $precmd_functions; do
            $precmd
        done
        zle reset-prompt
    fi
}