29

It seems like ctrl + W deletes backwards one word at a time. That's great, but I'd really really like that this works 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.

1
  • 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, 2015 at 5:45

5 Answers 5

22

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-H":"\C-W"

Now, open a new terminal and it should work.

Explanation

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

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-H": 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.

10

Use Alt+Backspace instead: it works just like Ctrl+Backspace and doesn't require any configuration.

2
  • This is great, because I don't have to configure again when I use a new system Jul 3, 2021 at 5:29
  • It's so annoying when switching programs to go from Ctrl+Backspace to Alt+Backspace though. It keeps messing me up.
    – E.M.
    Sep 22, 2023 at 20:25
7

[ for gnome-terminal ]

use stty tool, good for visualzing what bash keyboard shortcuts are binded.

see your current bash keyboard shortcuts (my system output):

stty -a

my output:

speed 38400 baud; rows 41; columns 76; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon
-ixoff -iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc

werase is responsbile for deleting an entire word.

running this will set ctrl + backspace to erase a word when pressed.

stty werase \^H

!note: this will set this option only to your current terminal/bash session. in order to apply this permanently you need to add previous command to .bashrc file at the end of it.

settings from .bashrc will be loaded every time you open gnome-terminal.

after this you are ready to go. enjoy.

[ for vim and neovim ]

just paste this in init.vim or vim.rc:

" Map Ctrl-Backspace to delete the previous word in insert mode.
" solution: https://vim.fandom.com/wiki/Map_Ctrl-Backspace_to_delete_previous_word
noremap! <C-BS> <C-w>
noremap! <C-h> <C-w>

this will work in insert mode, ofc. thats the main point of defining this, for insert mode.

works in neovim 0.5 and lower versions and also in standard vim.

2
  • Woohoo! Thanks : ) Apr 15, 2021 at 17:30
  • 1
    my pleasure to help. i will add the part with vim when i will learn to edit that.
    – alexzander
    Apr 15, 2021 at 18:42
4

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.

2
1

It works on my machine. Just make sure you use tmux from master and nvim from linuxbrew and control+backspace works in bash and vim too!

(Get bash from linuxbrew too)

You must log in to answer this question.

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