It seems like <ctrl> + W deletes backwards one word at a time. That's great, but I'd really really this work with <ctrl> + <backspace> like most other apps.

What's the best way to do this? I'd need the change to affect gnome-terminal commands, command line vim and GUI vim.

share|improve this question
1  
In vim you can press D then, B and it will do the same thing. However, I too miss this feature in the terminal because you have to esc out and it doesn't work while operating in Insert mode. Awesome tip for CTRL + W. – mchid Nov 24 '15 at 5:45

Note that this will work in the terminal and the default emacs mode (which you are using if Ctrl+W deletes the previous word for you). It will not work in bash's vi mode and will not affect vi/vim itself. This is, therefore, only a partial answer but the best this emacs user can offer.


This sort of thing is managed by the readline library whose behavior can be controlled via ~/.inputrc. Edit (or create, if it doesn't exist) ~/.inputrc and add this line:

"\C-?":"\C-W"

Now, open a new terminal and it should work.

Explanation

The \C-? is actually Control+BackSpace. You can see keycodes by pressing Ctrl+V then, the key you are after. You will see that BackSpace returns ^?.

Now, it should be possible to bind Control+BackSpace to the bash function that deletes the previous word (backward-kill-word or unix-word-rubout) by using "\C-?": backward-kill-word. However, I can't get that to work. It works perfectly well with different keys ("\C-E": backward-kill-word, for example, makes Ctrl+E delete the previous word) but for some reason, I can't get it to work with BackSpace.

So, as a workaround, we just map Ctrl+Backspace to Ctrl+W which is already mapped to backward-kill-word. So, when you press Ctrl+Backspace, that will be translated to Ctrl+W which, in turn, will send backward-kill-word. I got the idea for this workaround from this SU post.

share|improve this answer

Up until very recently, gnome-terminal emitted the same code on Backspace and Ctrl+Backspace, hence you cannot distinguish the two in your application.

This was fixed in vte-0.42, which is part of Gnome 3.18 released this autumn.

If you're using Wily, you can find updated packages in Gnome3 Staging.

share|improve this answer
    
Super helpful! Thanks so much. – Costa Nov 26 '15 at 16:11
    
can I get this to run in trusty without adding that huge PPA ? – Ciprian Tomoiaga Mar 31 '17 at 22:37
    
Not without downloading, patching, compiling and installing the libvte 2.90 package that matches trusty. The patch is at bugzilla.gnome.org/show_bug.cgi?id=733246. – egmont Apr 1 '17 at 7:55

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.