Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Mar 19 '12 at 2:13

This question came from our site for professional and enthusiast programmers.

    
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. –  Daniel Andersson Mar 20 '12 at 15:45
    
@ Daniel Andersson: or alternatively - You can develop a way to transfer Your settings to the foreign machine. –  Adobe Mar 20 '12 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. –  Daniel Andersson Mar 20 '12 at 18:21
2  
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. –  Ezequiel Muns Dec 20 '12 at 6:30
    
I have the same problem with Chrome SShInATab. How do I do ctrl-W without ctrl-W? :) –  TSE Riccardo Apr 18 at 13:06

4 Answers 4

up vote 2 down vote accepted

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

share|improve this answer
2  
Because I used to Ctrl-Backspace in my Emacs. I knew about Ctrl-w. Alt-Backspace is new to me (+1). –  Adobe Mar 17 '12 at 18:34
    
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 '12 at 20:56
    
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 '13 at 4:29
    
@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 '13 at 13:01

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.

share|improve this answer
    
I found that my bash doesn't see backspace. But may be it's possible to make it see that. –  Adobe Mar 17 '12 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 '12 at 20:43
    
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 '13 at 1:06

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.

share|improve this answer
3  
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 '12 at 17:59

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.