hlissner / dotfiles

And I say hey, what's going on?
https://youtu.be/ZZ5LpwO-An4
MIT License
1.62k stars 98 forks source link

What is `bindkey -M vicmd "^[[3~" delete-char` for? #2

Closed kutsan closed 6 years ago

kutsan commented 6 years ago

I was reading your dotfiles and came across these lines. I tried to search for it but couldn't find anything. Do you care to explain what these lines for? Actually, what is ^[[3~? Is there any resource I can learn about them more?

bindkey -M vicmd "^[[3~" delete-char
bindkey "^[[3~"  delete-char
bindkey "^[3;5~" delete-char
hlissner commented 6 years ago

^[[3~ is the escape code for the delete key. In zsh, this key doesn't delete-forward as I would expect. Instead, it acts as if I've pressed ~e in command mode (or g~e in vim), which flips the case of the word after the cursor.

Not sure if that's a quirk of zsh's vi mode, but I manually bound delete to delete-char to fix it.

^[3;5~ is actually a typo. It's supposed to be ^[[3;5~, which is the escape code for ctrl + delete. There's also ^[[3;2~ for shift-delete. I don't think either is necessary though. I'll likely remove that.

If you're wondering how I got those codes, I ran cat, then pressed the key I wanted, and it prints out the escape code, e.g.

$ cat
^[[3~

Hope that helps!

kutsan commented 6 years ago

So much thanks! You and man {terminfo,infocmp,zshzle} (those were not just for this topic) helped me a lot. Here is what I learned. As I understand, there is no quirk here. ZLE applies everything it receives. In this case it's ^[[3~:

  1. ^[ It's escape character, so switches to normal mode.
  2. [ Since it's undefined-key, so ZLE doesn't know what to do with.
  3. 3~ Flips the next 3 letters.

I tested it with other undefined-keys like FN + RightArrow. cat outputs ^[[4~ for that key sequences. It exactly flips the next 4 letters like I said above.

I ran cat, then pressed the key I wanted, and it prints out the escape code, e.g.

cat trick is nice but I assume we can achieve the same result with pressing C-v and hitting a key, or sed -n l.

Thanks again. <3

hlissner commented 6 years ago

That makes perfect sense! It hadn't occurred to me that the escape code was being evaluated literally. The sed trick was new to me too.

In any case, thank you for sharing and I'm happy it helped!

snowman commented 5 years ago

Yeah, Tha's why iTerm2 map key cmd+← to hex code: 0x1B 0x08 or 0x17

$ echo -n '^[^H' | xxd
00000000: 1b08                                     ..

$ echo -n '^W' | xxd
00000000: 17                                       ..