Why not just use Alt+Backspace or Ctrl+W that are already mapped in most terminals. Not sure about Console. Xmodmap (man xmodmap) may be used to remap other custom keys as well. (Paradoxically, xmodmap can’t remap Backspace very well because the terminal overrides the mappings, but it works well with most keys…).
Just edit your
~/.inputrc (you might need to create one or copy the one in
/etc/inputrc there) so that it contains:
# Ctrl-Delete: delete next word "\e[3;5~": shell-kill-word # Ctrl-Backspace "\C-H": shell-backward-kill-word
This will map also Ctrl+Delete to delete the word next to the cursor.
I found this thread via google, but the answer wasn’t what I wanted to hear.
So I played around:
On my terminal, normal backspace sends
^H, while ctrl+backspace sends
So it should simple be a case of rebinding
^? to delete a word, which by default is available via Ctrl+W.
First (unsuccessful try):
$ bind -P | grep 'C-w' unix-word-rubout can be found on "\C-w".
So therefore this should work:
$ bind '"\C-?":unix-word-rubout'
However it does not… anyone able to explain?
Second (successful) try:
$ bind '"\C-?":"^W"'
^W is a literal/raw
^W (press ctrl+V then ctrl+W to make it appear).
There are some good answers here, but I solved it in Konsole with Settings->Edit Current Profile->Keyboard->Edit, then adding a mapping from
\x17. (I found the ASCII code for Ctrl-w using
That depends on what bash sees. On regular terminal interaction, bash does not see what key modifiers you pressed, only the resulting character. Backspace is already the same as control-H, for example. Holding down shift or control makes no difference.
HOWEVER, your terminal application (xterm? cmd? Terminal? Putty? Depends on your OS) can see your keypresses, and may have a way to map the control-backspace key combination to something bash can tell apart from backspace.
Settings -> Edit current profile
- click the ‘Keyboard’ tab ->
- click the
[Add]button and under the
Key Combinationcolumn type
Backspace+Ctrl. In the
0x08(which is conventionally the backspace character)
Now you should be able to map that key to
# ~/.bashrc bind '"\x08":backward-kill-word'
Optionally add the following to ~/.inputrc to make it permanent:
Works on bash 4.3.11 in Kubuntu 14.04
To add to most of the answers here, and to explain why most answers have a comment saying “didn’t work for me”:
Most terminal emulaters/programs will send a different ‘character’ to represent ctrl+backspace. Some will not send anything different – pressing ctrl + backspace will send the same thing as backspace. That means if you have a different emulator to what the answerer is using, there’s a good chance the character is different.
Some answers have mentioned modifying (via settings) what character gets sent. If you don’t want to do that, the way to find out which character bash is receiving from the emulator when you press ctrl + backspace is:
- At the bash input, press ctrl + v (this key combination might change, but this is a very common one). This will pass through the next character without bash intepreting it.
- press ctrl + backspace
- the sequence that appears at the prompt is the character sequence sent by the emulator for ctrl + backspace.
- repeat the process with regular backspace to confirm it is different from ctrl + backspace.
- If it is the same, you will have to try the method of changing the terminal settings.
On most terminals CTRL+W should already delete a word backwards. This works with xterm and I guess it works with konsole.
CTRL+W is a standard key binding on text-based program that will work with vim in insert mode, emacs, and every shells.
This command worked for me:
stty werase ^H