47

I'm running Ubuntu 11.04. I installed the Terminator Terminal Emulator 0.95, and Zsh, version 4.3.15.
I have (commonly known) problems with my keys inside the Zsh. At least these:

  • Home/End, nothing happens
  • Insert/Delete/PageUp/PageDown: a "~" is typed

I already tried some configurations for .zshrc which should solve the problem, but no approach really worked so far. Maybe this is related to the combination of Terminator and Zsh. I took the 2 configs on this page: https://bbs.archlinux.org/viewtopic.php?pid=428669.

Does oneone have a similar configuration (especially Terminator and Zsh) and figured out what needs to be inserted into the .zshrc to fix the key settings?

pdm
  • 2,248
  • 16
  • 23
Wolkenarchitekt
  • 17,351
  • 28
  • 102
  • 166

4 Answers4

122

To know the code of a key, execute cat, press enter, press the key, then Ctrl+C.

For me, Home sends ^[[H and End ^[[F, so i can put i my .zshrc in my home dir

bindkey  "^[[H"   beginning-of-line
bindkey  "^[[F"   end-of-line
bindkey  "^[[3~"  delete-char

These codes could change with the terminal emulator you use.

autoload zkbd ; zkbd will create a file with an array of keycodes to use, like bindkey "${key[Home]}" beginning-of-line, and you can source a different file depending on the terminal.

pdem
  • 3,264
  • 1
  • 19
  • 31
lolesque
  • 7,618
  • 4
  • 32
  • 35
  • 4
    The cat thing did not work for me, but this [blog post over here](http://blog.andrewbeacock.com/2007/08/how-to-get-home-end-keys-working-in.html) shows `od -c`which did the trick for me – flq Jul 09 '13 at 19:35
  • 1
    `read` works like `cat` for this. Just remember that `^[` is escape code, `\e` in `.inputrc`. – Victor Sergienko Jan 04 '15 at 10:22
  • I use '^V' followed by desired keystroke. Seems to work anywhere. – rr- Apr 09 '15 at 14:58
  • 1
    A list of available widgets such as `beginning-of-line` is available [here](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Standard-Widgets). For instance, `delete-char` is the widget normally associated with the delete key. – Ruud Aug 07 '15 at 19:50
  • 1
    Have been trying to fix this for an hour and realized that other sollutions didn't work because my keyboard is set to swedish. Your cat trick fixed that, thanks! – Johan Bjäreholt Oct 24 '15 at 11:03
  • How would one switch out the different keyfiles created by zkbd for different shells, eg. ssh'ing into a box from terminator vs running zsh directly on the machine vs using PuTTY yields different keycodes – Aaron_H Apr 27 '16 at 02:31
  • Coming from windows to mac has been so painful and mapping these keys in zshrc worked first shot. Just remember to source ~/.zshrc and then get working – httpete Feb 24 '21 at 21:31
25

Thanks to @lolesque, but the solution doesn't work with me. After using zkbd to check my key binding, I came out the below solution. BTW, my $TERM is xterm.

bindkey  "^[[1~"   beginning-of-line
bindkey  "^[[4~"   end-of-line

Hope it helps.

pdm
  • 2,248
  • 16
  • 23
  • 1
    This works for me when I am ssh'ed to zsh via hyper.js on windows – Steven Jun 14 '19 at 03:20
  • 1
    if you read the answer you'll see that he provided the complete solution with `cat` command. And my keys is same as yours. Thank you. – Long Sep 07 '20 at 12:03
2

For me on WSL2 (Windows 10) this was it

bindkey "^[OH" beginning-of-line
bindkey "^[OF" end-of-line

The cat trick did not work for me as it printed ^[[H as in the accepted answer but I was able to find out the ^[OH sequence by typing Ctrl+v Home in the new Window Terminal running zsh.

Epeli
  • 16,564
  • 10
  • 63
  • 76
  • Confirmed this on WSL2. I still haven't been able to see those characters, for some reason, but this was exactly the fix I needed. Now I'm curious. What's your `$TERM`? – CJ Harries Jun 02 '20 at 22:53
1

Setting the TERM variable in .zshrc caused this for me.

Fergal Moran
  • 4,240
  • 3
  • 34
  • 52
  • This worked for me. Add `export TERM=xterm` in `.zshrc`, and re-run the terminal with `zsh` to make it work. – Hansimov May 25 '21 at 03:01