views:

687

answers:

5

Bash uses readline, and readline can delete the word to the right of the cursor with "kill-word".

The problem is in recognizing the keypress of control-delete. When I press them in bash, "5~" is output on the screen. I could just bind for this, but it would mean that one day I need to type "5~", and it deletes a word to the right instead! So I'd much rather discover the correct control sequence.

I have googled, and quite a few resources discuss the "delete" key, but none that I've found discuss "control-delete" key. I've experimented with many variations, but nothing works.

The worst is the hours I've spent on this tedious, mindless grind, when it really should be a non-problem.

EDIT: It's through X, so maybe there's a solution with xev and xmodmap

+1  A: 

If you type ^Q^V (that's Control-Q followed by Control-V, releasing the Control key between them is fine), and then press Control-Delete, do you get the output you mentioned? I just tried it, and at least using Putty I don't get a response at all. Perhaps the behvior is different on an actual Linux console, though.

For other keys, readline prints a longer sequence, often including a special "command sequence introduction" character, which is hard to type by mistake. Try it, and see if you get a longer sequence with the ^Q^V command (which is, btw, called quoted-insert).

For example, if I press ^Q^V and then Delete (without control held down), readline prints ^[[3~. This tells me I can bind stuff to the Delete key by saying \e[[3~. It seems highely likely that the CSI character is present for you, but you're not seeing it since you're not asking readline to quote the input properly.

unwind
Thanks, that's what I needed :-). ^delete gives: ^[[3;5~ and plain delete gives: ^[[3~ (same as you). Incidentally, CSI is an apt abbreviation for something revealed by a forensic tool :-).
13ren
BTW: ^V has the same effect as ^Q^V for me. I'm guessing that there are some cases where quotation (^Q) makes an important difference, but my specific case of ^delete isn't one of them.
13ren
btw: typing ^[[3~ does not trigger the same action: I'd expect there to be some escaping involved somewhere to prevent them from ever being confused with those characters typed in explicitly.
13ren
Good, glad you could solve it. I think typing is not the same; that ^[ sequence represents the single character "escape". Try Escape, then an opening square bracket, a 3 and a tilde ...
unwind
+2  A: 

What you see is not the whole truth. It's probably <ESC>5~ or something like that. Try Ctrl-V Ctrl-Delete. The Ctrl-V means "do not interpret the next thing".

So binding <ESC>5~ that should be pretty safe.

Aaron Digulla
thanks for the ctrl-v - being able to see really helps!
13ren
BTW: I think perhaps some special characters you entered didn't appear?
13ren
Thanks for the heads-up; I missed to check the text after posting.
Aaron Digulla
+4  A: 

On my machine, pressing Ctrl-V, Ctrl-Delete outputs this:

^[[3;5~

The ^[ escape character can be replaced with \e, so you can then use bind like this for bash (in your ~/.bashrc for example):

bind '"\e[3;5~":kill-word'

Or, you can add the following to your ~/.inputrc so Ctrl-Delete does kill-word in any program that uses readline:

"\e[3;5~": kill-word

This will bind only the Ctrl-Delete key, you don't have to worry about what will happen if you need to type 5~.

Chris AtLee
Might want to mention this goes in ~/.bashrc and there is a different format for ~/.inputrc, which is better to use if you want this to work for other apps.
Ian Kelling
I would write `"\e[3;5~": kill-word` into ~/.inputrc instead of putting `bind ...` into ~/.bashrc -- it makes this keybinding available for all readline-using programs.
ephemient
Now it works, thanks! BTW: when I read your answer the first time, I didn't notice the "Ctrl-V", and it was only the other answers that focussed on this (the key piece of the puzzle that I was missing) that made it click.
13ren
A: 

Ctrl-W deletes words.

Ctrl-u deletes lines.

They're based on Emacs (M-w and M-u).

supercheetah
A: 

Alt+D deletes one word to the right of the cursor Ctrl+W deletes one word to the left of the cursor (both are based on Emacs, I believe)