39

When in shell (bash) - I want to have Ctrl-Backspace binded to "delete word backward". Is it possible?

Edit:

I'm using konsole - terminal at KDE.

6
  • 3
    The question is straight forward, no complains about that. I just want to comment that it is often worth a lot to facilitate "standard" key bindings that already exist in many applications, instead of customizing away for no real gain and the certainty of never seeing the behaviour on foreign systems. Ctrl+w is what you should get accustomed to, imho. Mar 20, 2012 at 15:45
  • @ Daniel Andersson: or alternatively - You can develop a way to transfer Your settings to the foreign machine.
    – Adobe
    Mar 20, 2012 at 17:39
  • Sharing dotfiles is a solved problem, but it doesn't help much on other than more or less local computers where you have quite open privileges. If you are on a foreign system you most likely don't have the freedom to reconfigure things like that (at least not without getting bashed by colleauges who also use the same machine :-) ). Also, every new application will need special treatment. Mar 20, 2012 at 18:21
  • 7
    I've got the inverse problem, I'm used to Ctrl-w as "delete previous word" in the console, and do it by instinct even in Eclipse or Chrome where it means close tab/close current file. Quite annoying. Dec 20, 2012 at 6:30
  • 2
    I have the same problem with Chrome SShInATab. How do I do ctrl-W without ctrl-W? :) Apr 18, 2014 at 13:06

11 Answers 11

20

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...).

4
  • 18
    Because I used to Ctrl-Backspace in my Emacs. I knew about Ctrl-w. Alt-Backspace is new to me (+1).
    – Adobe
    Mar 17, 2012 at 18:34
  • 1
    Good point, @hellork. You could intercept the control+backspace at the X server (via xmodmap) and map it to some unused character (such as alt-backspace).
    – alexis
    Mar 17, 2012 at 20:56
  • 15
    The annoying thing about this is that ctrl+backspace is a really common mapping for "delete word backwards" in most GUI apps on most major operating systems. Likewise, Ctrl+W is wide-spread for "close window". Alt+w is rarely used for anything in GUIs, that I can think of. Standardisation would be great. I wonder what would be the bigger disruption, changing terminals, or changing GUIs?
    – naught101
    Feb 25, 2013 at 4:29
  • 1
    @naught, Control-W is already the default word-erase key on unix terminals. Yeah it conflicts with the Windows/GUI convention, but in fact it came first--by many years. Blame Microsoft!
    – alexis
    Apr 7, 2013 at 13:01
24

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.

2
  • 1
    Doesn't work for me.
    – Jonathan
    Oct 12, 2016 at 8:19
  • 1
    C-H worked for me with unix-word-rubout, for WSL with the standard windows console app. Dec 27, 2020 at 17:44
14

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"'

Where the ^W is a literal/raw ^W (press ctrl+V then ctrl+W to make it appear).

3
  • Your first solution worked for me, actually! Second did not. May 19, 2015 at 19:57
  • The second try seems to replace backspace itself rather than Ctrl-Backspace?
    – user541686
    Nov 16, 2019 at 11:13
  • C-? didn't work for me, but C-H did with unix-word-rubout (see Treviño's answer). I'm on WSL with the standard windows console app. Dec 27, 2020 at 17:44
10

There are some good answers here, but I solved it in Konsole with Settings->Edit Current Profile->Keyboard->Edit, then adding a mapping from Backspace+Control to \x17. (I found the ASCII code for Ctrl-w using showkey --ascii.)

3
  • Thanks for pointing out the thing with showkey. Thus I figured out that ALT+D for delete word in front of the cursor would correspond to \x1b\x64.
    – loki
    Aug 8, 2018 at 10:02
  • Thank you, it solved my problem. In my case I need to remap the Alt+Backspace and its sequence is \x1b\x7f.
    – DDMC
    Jun 27, 2019 at 12:09
  • To show/hide the menu in Konsole, Ctrl+Shift+M. May 3, 2022 at 17:54
4

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.

3
  • I found that my bash doesn't see backspace. But may be it's possible to make it see that.
    – Adobe
    Mar 17, 2012 at 17:32
  • @adobe, what do you mean? Type 'stty' at your terminal and then read the manual about it. If ^H is set to be your erase character, you can set it to something else... if you have a good reason.
    – alexis
    Mar 17, 2012 at 20:43
  • 1
    There's a good example of how to do this at github.com/calj/gtconf/blob/master/zsh/zshrc. Konsole lets you do it. Copy one of the *.keytab files from /usr/share/kde4/apps/konsole/ into ~/.kde/share/apps/konsole and modify it with the suggestions in the link above, deleting the "key Backspace" line and adding: key Backspace-Shift-Control : "\x7f" and key Backspace-Shift+Control : "\E[9;3~" Also change the title line while you're at it: keyboard "Xfree4 Modlist", for example. You can then add "\e[9;3~": backward-kill-word" to ~/.inputrc
    – Adam
    Mar 11, 2013 at 1:06
4

In Konsole:

  • click Settings -> Edit current profile
  • click the 'Keyboard' tab -> [Edit...]
  • click the [Add] button and under the Key Combination column type Backspace+Ctrl. In the Output column type 0x08 (which is conventionally the backspace character)

Now you should be able to map that key to backward-kill-word via

# ~/.bashrc

bind '"\x08":backward-kill-word'
2
  • Thanks. Works for me, in xfce4-terminal at least.
    – limon
    Jun 8, 2019 at 4:35
  • 1
    Try \x08 as output if 0x08 doesn't work.
    – sean
    Mar 24, 2020 at 4:35
2

Try this:

bind '"\C-_":backward-kill-word'

Optionally add the following to ~/.inputrc to make it permanent:

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

Works on bash 4.3.11 in Kubuntu 14.04

3
  • 3
    Control-backspace is unlikely to be the same as Control-underscore. Dec 20, 2015 at 14:48
  • 1
    Out of all the things I tried in this thread, this worked! Using "GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)" on bash on windows/WSL Aug 17, 2017 at 9:31
  • This works great for me in cygwin, using GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin).
    – zagyi
    Dec 5, 2018 at 9:20
2

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:

  1. 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.
  2. press ctrl + backspace
  3. the sequence that appears at the prompt is the character sequence sent by the emulator for ctrl + backspace.
  4. repeat the process with regular backspace to confirm it is different from ctrl + backspace.
  5. If it is the same, you will have to try the method of changing the terminal settings.
1

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.

1
  • 5
    I have Ctrl-w binded to "close tab" in Emacs, FireFox, Krusader - so I set it to be a tab-closing combination in Konsole also.
    – Adobe
    Mar 20, 2012 at 17:59
1

This command worked for me:

stty werase ^H
1
  • This works fine for me on Windows (git bash), Linux and macOS.
    – jobor
    Oct 17, 2023 at 14:05
0

You can use AutoKey with a Window Filter to remap <ctrl>+<backspace> for your favorite terminal application: https://unix.stackexchange.com/a/669238/492533

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .