How to Remap Control-w in Bash

bashinputrc

I have read a number of answers to similar questions both in this forum and elsewhere, but I cannot make it work for me.

I am trying to remap Ctrl+W to delete the word forward rather than backward. I know that I can use Esc+D do do this, but this is also a learning exercise for me.

Using what I have read, I have added the following in ~/.inputrc:

$include /etc/inputrc
Control-w: kill-word
  • The first line is, apparently, required if I want to include the other bindings.
  • For the second, I have tried Control-w and "\C-w", and for the command, I have tried kill-word and shell-kill-word.
  • To try out the changes I have tried bind -f ~/.inputrc, Ctrl+X, Ctrl+R, bash -l and simply opening up a new terminal.

I assume that I have missed some important step, or simply misunderstood, but I’m running out of ideas.

I am on Centos 8.

Best Answer

The readline library (which is what inputrc configures) sets up bindings corresponding to the bindings of the terminal driver for cooked input. These are commonly known as stty keys because they can be configured with the command stty. Ctrl+W is the binding for deleting the previous word (werase) in the cooked terminal interface.

Those bindings take precedence over explicit bindings in .inputrc. To turn off this behavior, add this line to your .inputrc:

set bind-tty-special-chars off

If you haven't configured non-default cooked terminal key bindings with stty, I think this won't affect your bash bindings.