Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing Bash prompt after invocation of a readline bound command

My shell is GNU Bash 4.3.11, and I currently have M-h bound to cd .. by calling the builtin

bind -x '"\eh": "cd .."'

This gives me a nifty way to navigate up the directory tree, as I can repeatedly hit M-h instead of the incredibly time-consuming cd ... It has the downside, however, either of not resetting my $PS1 or of not redrawing my prompt, so I lose the context of my current working directory until I enter a new command.

One alternative I'm aware of is to put a macro like

"\eh": "\C-a\C-kcd ..\C-m"

in my .inputrc directly. This, however, has the downside of not only losing the context of any existing command I'm typing in (which I think can probably be worked around) but also of printing out cd .. (which I don't think can be).

My desired behavior is to be able to be in a directory ~/one/two with prompt ~/one/two$; hit M-h; and then be in ~/one and have the prompt be ~/one$, ideally keeping any command I had initially. How can I achieve this?

like image 635
pharazin Avatar asked Oct 18 '25 21:10

pharazin


1 Answers

Figured this out.

# maintain state
bind -x '"\200": TEMP_LINE=$READLINE_LINE; TEMP_POINT=$READLINE_POINT'
bind -x '"\201": READLINE_LINE=$TEMP_LINE; READLINE_POINT=$TEMP_POINT; unset TEMP_POINT; unset TEMP_LINE'

# "cd .." use case.
bind -x '"\206": "cd .."'
bind '"\eh":"\200\C-a\C-k\206\C-m\201"'
like image 156
pharazin Avatar answered Oct 20 '25 17:10

pharazin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!