Ctrl-Backspace and Ctrl-Delete in bash

There are two line editors at play here: the basic line editor provided by the kernel (canonical mode tty line editor), and bash's line editor (implemented via the readline library). Both of these have an erase-to-previous-word command which is bound to Ctrl+W by default. The key can be configured for the canonical mode tty line editor through stty werase; bash imitates the key binding that it finds in the tty setting unless overridden in its own configuration.

The werase action in tty line editor cannot be configured. It always erases (ASCII) whitespace-delimited words. It's rare to interact with the tty line editor — it's what you get e.g. when you type cat with no argument. If you want fancy key bindings there, you can run the command under a tool like rlwrap which uses readline.

Bash provides two commands to delete the previous word: unix-word-rubout (Ctrl+w or as set through stty werase), and backward-kill-word (M-DEL, i.e. Esc Backspace) which treats a word as a sequence of alphanumeric characters in the current locale and _. If you want Ctrl+Backspace to erase the previous sequence of alphanumeric characters, don't set stty werase, and instead put the following line in your .inputrc:

"\C-h": backward-kill-word

Note that this assumes that your terminal sends the Ctrl+H character for Ctrl+Backspace. Unfortunately it's one of those keys with no standard binding (and Backspace in particular is a mess for historical reasons).

There's also a symmetric command kill-word which is bound to M-d (Alt+D) by default. To bind it to Ctrl+Delete, you first need to figure out what escape sequence your terminal sends, then add a corresponding line in your .inputrc. Type Ctrl+V then Ctrl+Delete; this will insert something like ^[[3;5~ where the initial ^[ is a visual representation of the escape character. Then the binding is

"\e[3;5~": kill-word

If you aren't happy with either definition of a word, you can provide your own in bash: see confusing behavior of emacs-style keybindings in bash


Try

alt+backspace

In elementary OS it works like the way you want it (just like ctrl+backspace)