How can I refresh the Bash prompt after changing directories from a keybinding?

0
0
Asked By MellowPine42 On

I'm using Bash keybindings with bind -x to mimic a file manager: Alt plus the arrow keys changes directories while I'm editing a command. The directory change works, but the PS1 prompt still shows the old location until the command line is redrawn manually. I'm looking for the Bash equivalent of zsh's prompt reset or line redraw functionality. I tried sending carriage returns and escape sequences, as well as assigning READLINE_LINE to itself, but either the line breaks, produces control-sequence output, or leaves the previous prompt visible. What is the proper way to refresh the current Bash command line and display the updated PS1?

2 Answers

Answered By QuietHarbor7 On

This may be easier to solve by focusing on the readline redraw rather than printing the prompt yourself. A bind -x function can update the directory and then force readline to redisplay by assigning the current buffer back to READLINE_LINE, while preserving READLINE_POINT. Avoid echoing PS1 directly, because readline is responsible for prompt layout and multiline cursor handling.

MellowPine42 -

That is the behavior I’m trying to get, but simply assigning READLINE_LINE back to itself still leaves the old prompt visible in my setup.

Answered By CopperLynx18 On

You can try saving and restoring the cursor around the redraw: include a save-cursor escape at the beginning of PS1, then print the expanded prompt and the current buffer after restoring it. However, this is fragile because terminal escape sequences do not tell readline about the prompt’s visible width. A readline-triggered redisplay is generally safer than manually printing PS1.

MellowPine42 -

I tried the save-and-restore escape sequence approach, but it did not work reliably and left remnants of the previous prompt on the line.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.