5

I'm trying to remap keys on the bash command line. I have a .inputrc file which I have reduced to:

"\C-w": forward-word

There were other settings, but I have reduced them to one for the sake of question clarity.

What happens is that on every new line in the shell, C-w resets to its default binding, unix-word-rubout. I can reload the .inputrc file with C-x C-r as I edit the line and C-w takes on the action I have assigned to it, forward-word. Then on the next line in bash, it resets to unix-word-rubout again.

If I run

> bind -P | grep C-w

I get

unix-word-rubout can be found on "\C-w".

But if I run

> bind -P | grep C-w 

and then reload .inputrc with C-x C-r before I hit enter, I get

forward-word can be found on "\C-w", "\ef".

So I can load .inputrc just fine and bash even acknowledges it. But it reloads the default settings on each and every new line.

JLine, the Java Readline clone doesn't have any problems using my .inputrc as intended.

I'm running bash on OS/X Yosemite. I'm running the Homebrew 4.4.12 version, but the Apple 3.2.57 version (from 2007) works exactly the same. It works the same in Terminal.app and iTerm2.

I'd like my settings to work on every line. How can I make them persist?

  • (1) Did you actually strip out all other lines from .inputrc and verify that it still behaves this way? (2) What is the value of $PROMPT_COMMAND and $PS1? – Wildcard Feb 1 '17 at 8:04
  • (Since you're on Mac OS X, if you start diving into the details of your PROMPT_COMMAND you may find my Apple stack exchange question and research interesting, though it isn't about .inputrc misbehaving.) – Wildcard Feb 1 '17 at 8:07
  • Wildcard: (1) Yes, I deleted all the other lines for testing. (2) My usual PS1 is mildly complicated, but if I set it to '>', PS1='>' the issue works exactly the same. set | grep PROMPT_COMMAND shows no matches. – Brian Feb 1 '17 at 8:13
  • 1
    Hmmm, odd. I can duplicate this problem on a clean CentOS VM. – Wildcard Feb 1 '17 at 8:27
  • What about if you re-bind unix-word-rubout to something else? – Wildcard Feb 1 '17 at 8:28
5

The problem is that the serial comunications also have some keys defined.
Use:

$ stty -a
speed 38400 baud; … …
swtch = <undef>; start = ^Q; stop = ^S; … … werase = ^W; …
-parenb …

There (depending on your system settings) you should see:

werase = ^W

That is the stty definition of word-rubout as the key ctrl W.

Just execute:

stty werase undef

To make such key as undef by the ssty controler.
Be careful with the changes you choose to make as you could make your terminal un-usable.

Then the key will be free to be re-asigned by the readline system the bash shell use.

To make the change (stty werase undef) persist, you may place it in /etc/profile for all shells that use that file (almost all). In /etc/bash.bashrc for all bash shells or in ~/.bashrc for "this user" interactive shells.

  • This is unbelievable. Who would knew there's another layer of key bindings... – Naitree Nov 6 '18 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.