Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh: refresh prompt after running zle widget

Tags:

zsh

zsh-zle

I have the following widget defined

function cdd()
{
    cd /
}
zle -N cdd{,}
bindkey "^R" cdd

After pressing the key combination the cwd is already changed, but the terminal prompt is not updated. Example, after doing this ()

~/tmp/todelete$ | # press key ^R  here; "~$" is the prompt; "|" denotes cursor

the terminal remains exactly unchanged. If I then type ls -ld ., it shows

~/tmp/todelete$ ls -ld .
dr-xr-xr-x 23 root root 4096 Sep 14 07:52 ./

/$ |

which means the cwd at the time ll is running is already /.

This is very confusion and may lead to serious errors. (e.g. If after pressing ^R I was interrupted to leave my desk and then come back, I might forget what I have done)

How can I let the terminal to redraw the prompt after pressing the key? Is there a zle function to do this?

like image 877
doraemon Avatar asked Sep 14 '18 05:09

doraemon


1 Answers

reset-prompt could rescue:

function cdd()
{
    cd /
    zle reset-prompt # XXX: added
}

reset-prompt

Force the prompts on both the left and right of the screen to be re-expanded, then redisplay the edit buffer. This reflects changes both to the prompt variables themselves and changes in the expansion of the values (for example, changes in time or directory, or changes to the value of variables referred to by the prompt).

Otherwise, the prompt is only expanded each time zle starts, and when the display as been interrupted by output from another part of the shell (such as a job notification) which causes the command line to be reprinted.

--- zshzle(1), reset-prompt, Miscellaneous, Widgets, zsh command line editor

like image 120
hchbaw Avatar answered Jan 03 '23 07:01

hchbaw