Bash – Readline .inputrc resets to defaults with every line

bashosxreadline

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?

Best Answer

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.