14

How to configure zsh such that Ctrl+Backspace kills the word before point? How to achieve that Ctrl+Delete kills the word after point?

I use urxvt as terminal emulator.

3
  • 1
    Firstly, you should define "word". Is it only alphabetic characters? All non-space characters?
    – Mikel
    May 7, 2011 at 9:19
  • word like it is used in emacs terminology
    – student
    May 7, 2011 at 9:31
  • If none of the solutions below work refer to: stackoverflow.com/questions/21252419/….
    – AP.
    Sep 20, 2019 at 2:02

6 Answers 6

15

I'll focus on Ctrl+Delete first.

The zsh command to delete a whole word forwards is called kill-word. By default it is bound to Alt+D.

How to make Ctrl+Delete do it too depends on which terminal emulator you are using.

On my system, this works in xterm and Gnome Terminal:

bindkey -M emacs '^[[3;5~' kill-word

and for urxvt, you should do:

bindkey -M emacs '^[[3^' kill-word

If that doesn't work, try typing Ctrl+V Ctrl+Delete to see what the value is on your system.

You could even add both of those together to your .zshrc, or use the output of tput kDC5 instead of hard-coding the sequence.

Ctrl+Backspace seems harder.

On my system, pressing that is the same as pressing just Backspace.

If yours is the same, I think your best option is to use Alt+Backspace or Ctrl+W instead.

5
  • I use urxvt as terminal emulator
    – student
    May 7, 2011 at 9:32
  • Ctrl + Backspace doesn't work
    – student
    May 7, 2011 at 9:33
  • 1
    You need ^[[3^ for urxvt. Ctrl+V Ctrl+Delete would show you this.
    – Mikel
    May 7, 2011 at 9:39
  • 1
    Thanks, I have added bindkey '^[[3^' kill-word bindkey and '^H' backward-kill-word to my .zshrc. Now it works!
    – student
    May 7, 2011 at 10:45
  • @user5289: Ah, sorry, we misunderstood each other. I don't think ^H or ^? will work for Ctrl+Backspace. In fact it seems such a binding is not possible. Alt+Backspace or Ctrl+W might be adequate replacements.
    – Mikel
    May 8, 2011 at 3:43
13

Add the following to your ~/.zshrc

bindkey '^H' backward-kill-word
bindkey '5~' kill-word
3
  • 1
    I'm using xfce-terminal and this works but not the accepted answer
    – raven
    Aug 23, 2020 at 4:55
  • Works for me on gnome-terminal
    – S P Sharan
    Oct 6, 2021 at 13:31
  • Can confirm that this works on kitty too Oct 11, 2021 at 0:53
6

On urxvt, for the deleting backwards part, I simply have in my .zshrc the following:

bindkey '^H' backward-kill-word

and it allows me to delete the previous word with ctrl+backspace

2

I've got this in my .Xresources:

URxvt.keysym.C-BackSpace:       \033[33~

and that in my .zshrc:

bindkey -M main -M viins -M vicmd   '^[[33~'    backward-kill-word

This kills each whitespace seperated word. Note that '^[' is the Escape character. You have to xrdb -load .Xresources, open a terminal and then hit ^V followed by ^BackSpace.

1

Type showkey -a in your zsh, and enter the key combination(s) that you want (e.g. Ctrl + Backspace). I'm also using zsh and for me:

Ctrl + Backspace is ^H

Ctrl + Delete is ^[[3;5~

Then in your .zshrc file, add these lines:

# Ctrl+Backspace: kill the word backward
bindkey -M emacs '^H' backward-kill-word
bindkey -M viins '^H' backward-kill-word
bindkey -M vicmd '^H' backward-kill-word

# Ctrl+Delete: kill the word forward
bindkey -M emacs '^[[3;5~' kill-word
bindkey -M viins '^[[3;5~' kill-word
bindkey -M vicmd '^[[3;5~' kill-word
0

In fact the output of showkey -a on my Kali is:

  • Ctrl+Backspace is ^?

  • Backspace is ^H

So the accepted answers here just made my Backspace to delete words.

For anyone that happens to have the same behavior, add in your ~/.zshrc file this line:

bindkey '^?' backward-kill-word

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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