What Is Terminal Escape Sequence for Ctrl + Arrow (Left, Right,...) in Term=Linux

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

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.

How do I find out what escape sequence my terminal needs to send?


XTerm

Since you are advertising that you are using an xterm (via your TERM value), you will most likely want to arrange to send the sequences that xterm would send for these keys. The ctlseqs documentation from xterm describes these particular modified key sequences at the bottom of the PC-Style Function Keys section:

… xterm recognizes function key modifiers which are parameters appended before the final character of the control sequence.

    2 Shift
3 Alt
4 Shift + Alt
5 Control
6 Control + Shift

Examples

F5 sends ^[[15~, so Shift-F5 should send ^[[15;2~ (i.e. add ;2 before the final character)

The arrow keys and the first four function keys are a bit different. They often use SS3-based sequences (starting with ^[O); these will need to be changed to CSI-based equivalents (starting with ^[[) since SS3 sequences can not have parameters. Also, the normal sequences for the keys do not usually have a numeric parameter, so a placeholder(?) 1 parameter is also added:

Up sends ^[[A or ^[OA, so Shift+Up should send ^[[1;2A (i.e. switch to CSI, and add 1;2 before the final character)

F1 sends ^[OP, so Shift+F1 should send ^[[1;2P (i.e. switch to CSI, add 1;2 before the final character)

You might also like to look at the source code of various terminal emulators to see whay they do. For example, the relevant bits of tmux are in its xterm-keys.c file.

Configuration

Since your terminal emulator is not already sending all the sequences you want to support, you will have to configure it to do so. The built-in terminal emulator that comes with OS X, Terminal, has a few keys preconfigured, but you will probably have to add most of them yourself. You can get to the list of keys by invoking the Terminal > Preferences… menu item, selecting the top-level Settings tab, picking the appropriate profile, and switching to its Keyboard tab. From there you can add and remove key definitions. For example, you could define F2 with shift to send string to shell: (sic) and type ESC followed by [1;2Q into the string text box (the ESC will show up as \033, this is okay).

Note: Changing the definition of Option-Right and Option-Left might affect how some programs (e.g. shells and Emacs) work with those keystrokes. The default configuration sends ESC-f and ESC-b, which are Emacs-style keystrokes for backward-word and forward-word that many programs will understand by default. This configuration seems backwards to me; the shell (and other programs) should be configured to recognize proper modified arrow key sequences instead.

You might also want to try a different terminal emulator. iTerm 2 has built-in support for most of these modified keys (maybe not the Control+Fn variants), so there would be less to manually configure.

Also, note that OS X has some system-wide shortcuts defined for some of the Control-Fx combinations (see System Preferences, Keyboard, Keyboard Shortcuts, Keyboard & Text Input). You will have to disable some of these shortcuts to make sure that Terminal or iTerm has a chance to “see” your desired key combinations.

When you press right arrow in terminal during a process ^[[C shows up. What it means anyway?

Actually, that is from a terminal emulator. The Linux console produces different characters.

In either case, those are generally referred to as ANSI escape sequences, which are sent by special keys (function-keys or cursor-keys), usually in the same type of "archaic" form which applications use to control the terminal.

The particular set you have quoted are documented in XTerm Control Sequences, and are recognized by terminal applications such as ncurses. The corresponding information in ncurses is stored in its terminal database, e.g., this entry (you may have to follow a few links to see all of this).

With that, you may have enough keywords to use with web-searches.

Determine escape sequence independent from terminal type

If you're wanting to read terminal keypresses, you likely want to look at something like libtermkey , which abstracts the general problem away for you. Internally it uses a combination of terminfo lookups, or hardcoded knowledge of the extended xterm-like model for modified keypresses, so it can understand things like Ctrl-Up, which a regular curses/etc... cannot.

while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_VIM);
printf("You pressed key %s\n", buffer);

if(key.type == TERMKEY_TYPE_FUNCTION &&
!key.modifiers &&
key.code.number = 2)
printf("Got F2\n");
}

How to distinguish between Escape and Escape Sequence

I think the problem is that you're using getchar() — a function from the standard I/O library — where you need to use file descriptor I/O (read()).

Simple example

Here's a straight-forward adaptation of your code (tested on a MacBook Pro running macOS High Sierra 10.13.2), that produces the answer you and I want.

#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

enum { ESC_KEY = 27 };
enum { EOF_KEY = 4 };

int main(void)
{
// put terminal into non-canonical mode
struct termios old;
struct termios new;
int fd = 0; // stdin
tcgetattr(fd, &old);
//memcpy(&new, &old, sizeof(old));
new = old;
new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(fd, TCSANOW, &new);

// loop: get keypress and display (exit via 'x')
//int key;
printf("Enter a key to see the ASCII value; press x to exit.\n");
while (1)
{
char key;
if (read(STDIN_FILENO, &key, 1) != 1)
{
fprintf(stderr, "read error or EOF\n");
break;
}
if (key == EOF_KEY)
{
fprintf(stderr, "%d (control-D or EOF)\n", key);
break;
}

// check if ESC
if (key == 27)
{
fd_set set;
struct timeval timeout;
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int selret = select(1, &set, NULL, NULL, &timeout);
printf("selret=%i\n", selret);
if (selret == 1)
printf("Got ESC: possible sequence\n");
else if (selret == -1)
printf("error %d: %s\n", errno, strerror(errno));
else
printf("esc key standalone\n");
}
else
printf("%i\n", (int)key);
if (key == 'x')
break;
}

// set terminal back to canonical
tcsetattr(fd, TCSANOW, &old);
return 0;
}

Sample output (program esc29):

$ ./esc29   # 27 isn't a 2-digit prime
Enter a key to see the ASCII value; press x to exit.
115
100
97
115
100
selret=1
Got ESC: possible sequence
91
68
selret=1
Got ESC: possible sequence
91
67
selret=0
esc key standalone
selret=0
esc key standalone
selret=0
esc key standalone
100
100
4 (control-D or EOF)
$

I pressed the left/right arrow keys and got 'possible sequence' reported; I pressed the ESC on the touch strip and got 'ESC key standalone'. Other characters seem plausible, the code was rigged to break when control-D is pressed.

Complex example

This code reads up to 4 characters at a time, and processes those that are received. There are two nested loops, so I use a goto end_loops; (twice!) to break out of the both loops from the inner loop. I also use the atexit() function to do most of what can be done to ensure that the terminal attributes are reset to the sane state even if the program doesn't exit via the main() program. (We can debate whether the code should also use the at_quick_exit() function — it's a feature of C11 but not of POSIX.)

If the code reads multiple characters, it scans through them, looking for ESC (escape). If it finds one and there is any data left, then it reports the escape sequence (presumably a function key sequence). If it doesn't find any more characters, it uses select() as before to decide whether there are more characters in an ESC sequence or if this is a standalone ESC. In practice, the computer is hugely faster than a mere human, so it either reads a single character or the complete sequence. I use an array of length 4 since I think that's longer than the longest key sequence generated from the keyboard; I'd be happy to increase it to 8 (or any other larger number). The only downside to this is that the buffer must be available where characters need to be read in the unlikely event that several characters are read (e.g. because the program was computing while input was accumulating). There's also a chance that the ESC from a function key or arrow key will be the last character that fits in the buffer — in which case, extra reading is necessary. Good luck in demonstrating that with this program as written — you're not a fast enough typist. You'd need to add sleep code somewhere to allow characters to accumulate before it reads them.

So, this mainly shows a couple of extra techniques, but it could be useful as an alternative way of thinking about the processing.

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>

enum { ESC_KEY = 27 };
enum { EOF_KEY = 4 };

/* These two need to be set in main() but accessed from reset_tty() */
static int fd = STDIN_FILENO;
static struct termios old;

// set terminal back to canonical
static void reset_tty(void)
{
tcsetattr(fd, TCSANOW, &old);
}

int main(void)
{
struct termios new;
tcgetattr(fd, &old);
new = old;
new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(fd, TCSANOW, &new);
atexit(reset_tty); // Ensure the terminal is reset whenever possible

printf("Enter a key to see the ASCII value; press x to exit.\n");
char keys[4];
int nbytes;
while ((nbytes = read(fd, keys, sizeof(keys))) > 0)
{
for (int i = 0; i < nbytes; i++)
{
char key = keys[i];
if (key == EOF_KEY)
{
fprintf(stderr, "%d (control-D or EOF)\n", key);
goto end_loops;
}
else if (key == ESC_KEY && nbytes > i + 1)
{
printf("Got ESC sequence:");
for (int j = i; j < nbytes; j++)
printf("%4d", keys[j]);
putchar('\n');
break;
}
else if (key == ESC_KEY)
{
fd_set set;
struct timeval timeout;
FD_ZERO(&set);
FD_SET(fd, &set);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int selret = select(1, &set, NULL, NULL, &timeout);
printf("selret=%i\n", selret);
if (selret == 1)
printf("Got ESC: possible sequence\n");
else if (selret == -1)
printf("error %d: %s\n", errno, strerror(errno));
else
printf("esc key standalone\n");
}
else
printf("%i\n", (int)key);
if (key == 'x')
goto end_loops;
}
}

end_loops:
return 0;
}

Sample output (program esc67):

$ ./esc67
Enter a key to see the ASCII value; press x to exit.
65
90
97
122
selret=0
esc key standalone
Got ESC sequence: 27 91 65
Got ESC sequence: 27 91 66
Got ESC sequence: 27 91 67
Got ESC sequence: 27 91 68
Got ESC sequence: 27 79 80
selret=0
esc key standalone
97
Got ESC sequence: 27 91 67
97
Got ESC sequence: 27 91 67
120
$

MSysGit Bash - how to enable Ctrl+Left / Right arrows?

At your Bash prompt, press Ctrl-v Ctrl-Left-Arrow and Ctrl-v Ctrl-Right-Arrow and make note of the output. You should see something like: ^[OD and ^[OC or similar. Add the following lines to your ~/.inputrc:

"\eOC": forward-word
"\eOD": backward-word

where you will substitute \e for escape (^[) and the rest of the characters you got (OD, OC or similar).

To re-read the file and make the changes active immediately, press Ctrl-x Ctrl-r.

What is terminal key code for Ctrl+Backspace

The cursor- and home/end (editing-keypad) keys are different from the backspace key. Those always send some type of escape sequence. The backspace key does not. It sends a single control character, which makes it a control sequence, rather than an escape sequence. This distinction is used in the xterm manual.

Control/backspace (like any case where a control-modifier is applied to a control character) is a feature that may be provided by a terminal, but has no general meaning (much less a standard meaning). rxvt (during the 1990s) used this combination to switch between ASCII BS (^H or 8) and DEL (^?, or 127). xterm incorporated that in 1998 (patch #83) for the same purpose. By the way, as a VT220 emulator, xterm refers to this as the "backarrow key" (VT200s had that, not a backspace key).

PuTTY does not appear to provide that feature; its behavior for control/backspace is the same as without the control-modifier.



Related Topics



Leave a reply



Submit