In zsh how do you bind Ctrl+backspace to delete the previous word?
Asked Answered
L

2

32

I'm trying to bind the command usualy binded to ^W with ctrl+backspace.

I have two problem here, one for each parameter of the bindkey command:

  • what is string to mean the ctrl+backspace
  • what is the command to delete the previous word
Leek answered 21/1, 2014 at 8:7 Comment(1)
While some control key combinations have straightforward ASCII equivalents (Control-H sending ASCII 0x08, for example), control-backspace does not. Your terminal emulator must be configured to generate a string, much like it does for a function key or arrow key.Mere
L
53

One may use bindkey '^H' backward-kill-word.

Note that, on old versions of GNOME terminal, it won't work; see How do I get Ctrl-Backspace to delete a word in vim within gnome-terminal? and Bug 420039 - VTE doesn't distinguish between backspace and control-backspace.
As reported by thorbjornwolf in his comment, commit 23c7cd0f fixed it.

Leek answered 21/1, 2014 at 8:9 Comment(4)
The related question's accepted answer was updated on 2015-11-09: The gnome terminal problem seems to be fixed.Hallah
Then whats the keybinding for ctrl+H ?Orvie
how to specify to delete everything (what i typed before hitting enter)?Triboluminescence
Try ^U for everything before the cursor on the current line, ^K for what comes after.Leek
I
8

As I pointed out here there is a chance that the keystrokes are different in some systems.

If the output of showkey -a is:

  • Ctrl+Backspace is ^?

then you should add the following line in your ~/.zshrc file:

bindkey '^?' backward-kill-word
Impulsion answered 12/4, 2022 at 19:19 Comment(1)
showkey -a is extremely useful!Killerdiller

© 2022 - 2024 — McMap. All rights reserved.