Terminal escape codes and key bindings

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
lmuserx4849

Terminal escape codes and key bindings

Post by lmuserx4849 »

Another LM user had asked a question regarding bash and history, but ultimately it was about terminal escape codes and key bindings.
I've always found konsole and bash keys/mode defaults to meet my needs with a few exceptions. Trying some commands brought up 3 questions. I'll stick to only 2 keys: up-key and key-pad up-key, but it could apply to all keys.
  1. My $TERM is equal to "xterm". "rxvt" came up a lot. Have you changed the value of TERM, and if so, to what and why?
  2. What command do you use to display the escape codes associated with keys?
  3. "\E[A" or "^[[A" are the same thing and I could have found that myself and created a keybinding. But how do I make the leap to "\EOA" for the key-pad up-key, since none of the commands below show it? Please see the commands and their output below.

    If I type bind -q previous-history, I get:
    previous-history can be invoked via "\C-p", "\eOA", "\e[A".
    If I type sudo showkey, and click the up-key and key-pad up-key, I get:
    ^[[Akeycode 103 press
    keycode 103 release
    ^[[Akeycode 72 press
    keycode 72 release
    If I type xev, and click the up-key and key-pad up-key, I get:
    keycode 111 (keysym 0xff52, Up)
    keycode 80 (keysym 0xff97, KP_Up)
    If I type infocmp -L1 | less, I can search for "\eOA", "\e[A" that was displayed by the first command.
    cursor_up=\E[A,
    key_up=\EOA,
    I saw users use all kinds of command variations of sed, hexdump, cat to get the escape codes, but they all had the same results for both keys ("\e[A" or "^[[A").

    ncurses site talked about normal and an application mode for terminals, where the normal send escape sequence beginning with escape [ and the application mode escape O. :? :) The tldp also mentions these modes and says the keypad can be set to send special codes.... This would explain the two escape sequences. But how does the lowly user, just trying to create some key bindings, display escape codes in these modes....?
    Numeric Keypad or Arrow Keys Sends

    The numeric keypad (the rectangle of mostly numeric keys to the right of the main part of the keyboard) can be set to send special codes which will do special things in certain application programs. Ditto for the arrow keys. There is thus a "normal" mode where they send what is shown on the keycap (or the normal code sequence for an arrow-key) and an "application" mode where special escape sequences are sent. In some cases there is a "hex" numeric mode which is almost like normal numeric mode except that 6 non-numeric keys send the letters A-F. Thus one may type for example "B36F" on the numeric keypad.
    References:
Update:
This really isn't a Script question, maybe Bash, but rather hardware, readline, console, shell --- which is not necessarily bash...

Before posting, I searched for "key bindings" and similar messages have appeared in: Software & Applications, Cinnamon, Newbie Questions.

I had posted this item in "Other topics", but it seems to have been moved to this section.

It would really be helpful if the Linux Mint Forum moderators could mark these postings as moved to not only inform users but also educate, and hopefully decrease their workload in the future.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Minterator

Re: Terminal escape codes and key bindings

Post by Minterator »

I had not seen this thread before. I posted mine in "Software & Applications" because Terminal is an application. Somebody moved it here even though it has nothing to do with scripting or bash, both threads are about Terminal options or customizing Terminal.


All I can add is that in ~/.inputrc both of these work:

# Up/Down recalls previous/next command
"\e[A": history-search-backward
"\e[B": history-search-forward

OR

"\C-[OA": history-search-backward
"\C-[[A": history-search-backward
"\C-[OB": history-search-forward
"\C-[[B": history-search-forward
Locked

Return to “Scripts & Bash”