what is terminal escape sequence for ctrl + arrow (left, right,...) in TERM=linux
Asked Answered
S

4

13

I am building a terminal window in a browser (sth. like ajaxterm) and don't know which escape sequence to send to ssh tunnel (opened via paramiko.SSHClient().invoke_shell(term='linux')).

I have found a key logger and tried it in a terminal with $TERM == 'linux', but it returns the same sequence for ctrl+left and left (27,91,68).

If I try keylogger in another terminal (with $TERM == 'xterm') I get the codes (27,91,49,59,53,68). But these codes do not move generate the expected output from SSH channel (which would move cursor one word left on a normal linux shell). That is true even if I start paramiko with term='xterm'.

Any idea what sequence I should use? Or why the above sequence doesn't work?

UPDATE: I would be happy to use another terminal type (not "linux"), but unfortunately pyte works with VTxxx terminals only (I believe "linux" is vt220-like terminal - anyway, it works), so xterm doesn't work properly.

Standoff answered 14/10, 2011 at 12:38 Comment(3)
A typical default readline configuration (bash uses readline) has Alt-B for backward-word. terminfo doesn't even have an entry for Ctrl-←.Pentyl
But it works on xterm console... Could you please tell more about terminfo, preferably in an answer?Standoff
Linux console isn't even close to vt220. It's a subset of (the less capable) vt100. Each of the answers to this question has at least one error.Chatter
P
25

Terminals were hardware devices that consisted of a keyboard and an output device (initially a hardcopy printer, later a CRT monitor). A large computer could have several remote terminals connected to it. Each terminal would have a protocol for communicating efficiently with the computer, for CRT-based terminals this includes having special "control sequences" to change cursor position, erase parts of the current line/screen, switch to an alternate full-screen mode, ...

A terminal emulator is an application emulating one of those older terminals. It allows to do functions like cursor positioning, setting foreground and background colors, ... Terminal emulators try to emulate some specific terminal protocol, but each has its own set of quirks and deviations.

Unix systems have databases describing terminals and terminal emulators, so applications are abstracted away from the particular terminal (or terminal emulator) in use. An older database is termcap(5), while terminfo(5) is a newer database. These databases allow applications to query for the capabilities of the terminal in use. Capabilities can be booleans, numeric capabilities, or even string capabilities, e.g.: if a specific terminal type has/supports a F12 key, it will have a capability "key_f12" (long terminfo name), "kf12" (short terminfo name), "F2" (termcap name) describing the string that key produces. Try it with: tput kf12 | od -tx1.

Since programming directly with capabilities can be cumbersome, applications typically use a higher-level library like curses/ncurses, slang, etc...

There is a special environment variable called TERM that tells applications what terminal type they are talking to. This variable should be set to the exact terminal type if it exists in the database, for best results. This just tells the application which precise protocol and protocol deviations does the terminal understand. Changing the TERM variable does not change the terminal type, it just changes the terminal type the application thinks it is talking to.

All that said, Ctrl+arrow is a xterm behaviour (dependent on a configuration option) that is not reflected at all in the terminfo/termcap databases, so most applications will have no knowledge of it. Either way, either your terminal emulator (in your case pyte) supports it or it doesn't.

Assuming your main application is bash or some other application that uses the readline library, you may get away with using readline's backward-word (Meta-b/Alt-b/ESC b by default, configurable in inputrc) instead.

Pentyl answered 16/10, 2011 at 11:23 Comment(0)
O
12

A quick check with od -c reveals that gnome-termainal generates these values:

Left-arrow generates ESC-[-D.

Control-left-array geneates ESC-[-1-;-5-D

Ossie answered 14/10, 2011 at 14:23 Comment(5)
Ok, but gnome-terminal is TERM=xterm... what about linux?Standoff
The Linux console generates ESC-[-D for both keystrokes.Chorus
I know it does, I wrote so... :) Still, is this console-specific or terminal-specific?Standoff
@johndodo: what do you mean by console-specific or terminal-specific? xterm is a terminal emulator. pyte is a terminal emulation library. Konsole and Gnome-Terminal are also terminal emulators. The Linux console is a terminal emulator, implemented in the Linux kernel. A console is just really a special terminal emulator that the sysadmin designates so he can read system logs and initiate certain privileged action from there.Pentyl
@ninjalj: by "console" I meant a specific implementation (Linux console) of the terminal. To put it in other words: does Ctrl+Left generate the same sequence as Left in all TERM=linux terminals, or just in Linux console?Standoff
P
4

The Ctrl+arrow keycodes were introduced by xterm, and the likes of Gnome Terminal and KDE Konsole try to be compatible with xterm. Actual VT100 and VT220 terminals did not have separate keycodes for such combinations. As far as I know, the Linux console aims to be compatible with the VT100, with some additions, whereas xterm emulates the VT220, with lots of additions.

Patricia answered 15/10, 2011 at 19:5 Comment(1)
Linux console started off trying to emulate VT100, but a chunk of that (switching between character sets) was lost in the late 1990s when UTF-8 support was added. Linux console has implemented function keys (a feature not part of VT100) based on VT220 (or the xterm implementation in the early 1990s). Unlike xterm, the Linux function key feature was not extended.Chatter
K
0

Just to clarify Rob's answer (and noting Thomas Dickey's observation against the question about errors): using od -c, whilst being a very handy tool, only gives results when the Control Sequence Introducer (CSI), which is <esc> [, is in effect and the Cursor Keys are not in Application mode (or, for that matter, potentially other keys) and the Single Shift Select of G3 Character Set (SS3) is in use (that is, the introducer is <esc> O). And, even in application mode, some keys (e.g. PgUp, PgDn, Ins, Del, some function keys) always use the CSI, not SS3. For a really good 'overview' (it's a bit more than that!), check out Thomas Dickey's page on XTerm Control Sequences. (And, just to clarify a little further, in addition to the Cursor Keys switching to Application Mode (via CSI ? 1 h), the numeric keypad can also be switched to Application mode (via CSI =)).

Kaffiyeh answered 17/8 at 4:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.