12

I'd like to map Ctrl-w to kill-region in Bash 4.2. This key is bound to unix-word-rubout (delete word backward) by default. According to the manual, remapping should be possible with the bind command which has the options

-u function Unbind all keys bound to the named function.

-r keyseq Remove any current binding for keyseq.

I tried bind -r "\C-w" and bind -u unix-word-rubout but the key is not unset and, as bind -P | grep unix-word-rubout reveals, there was no change in the mapping.

I played with bind a little and I'm able to unbind other built-in keys but not Ctrl-w.

malana
  • 5,045
  • 3
  • 28
  • 41

2 Answers2

16

Ctrl-w is bound in stty to werase. You will need to unbind it there first.

stty werase undef
bind '"\C-w":kill-region'
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
15

By default, readline attempts to bind the control characters treated specially by the kernel's terminal driver to their readline equivalents. (To see how your terminal is configured, run

stty -a

.) Your terminal presumably has Ctrl-w set to werase, so bash binds it to unix-word-rubout. This binding takes precedence over any keybindings that you specify in ~/.inputrc.

To avoid this mapping, you need to set the readline variable bind-tty-special-chars to off in your ~/.inputrc file:

set bind-tty-special-chars off
mhagger
  • 2,687
  • 18
  • 13