Terminal Question
I think the answers are in here! http://tldp.org/HOWTO/Text-Terminal-HOWTO.html
- https://en.wikipedia.org/wiki/POSIX_terminal_interface
- https://en.wikipedia.org/wiki/Terminal_capabilities
- https://en.wikipedia.org/wiki/ANSI_escape_code
- https://en.wikipedia.org/wiki/Termcap
- http://www.leonerd.org.uk/hacks/fixterms/
- https://catern.com/posts/terminal_quirks.html
I don't want to just find out how to set things up to acheive a concrete goal--I actually want to properly understand how things work.
-
control character: non-printing character e.g. LF. Note that the control characters in ASCII are lower down than the regular printing characters. The Ctrl key works by subtracting a 64 from the regular keypress so, e.g. you can get a "Backspace" by pressing "Ctrl-h". "h" is 72, 72-64 is 8: the backspace character. As far as terminals are concerned, Ctrl-H (Ctrl-Shift-h) is identical to Ctrl-h). Note that Ctrl-? matches DEL because '?' is 63, and if you mask -1 to 7-bits you get 127: DEL.
-
escape character: a character that changes meaning of following characters, i.e. the start character of an escape sequence
-
escape/control sequence: a string of characters, starting with an "escape character" that does something. The ASCII "escape" character is 27, which means you can type it with or
Ctrl-[
(displayed onscreen as^[
), and this is often used as an escape character for escape sequences. -
ANSI: American National Standards Institute. Created a number of standards in order to try to get terminals to use the same set of control sequences.
-
ANSI escape sequences begin
ESC
and are followed by a character in range 0x40–0x5F. Most are more than two characters and beginESC[
In 8-bit environments, the two-character sequences can alternatively be represented by a single byte in range 0x40–0x5F. On 8-bit systems,ESC[
can also be represented by the single byte: 0x9B.ESC[
more common though. -
ESC[
is the CSI (Control Sequence Introducer/Initiator) -
escape sequences sent TO terminal: clear screen, move cursor, etc
-
escape sequences received FROM terminal: arrow keys, Home key, etc
-
CSIs look like ESC[n;n;n;X (where the 'n's are numbers, and the X is some character.
-
Alt. Lots of terminals send alt k as Esc followed by k. (Some allow you to switch between this behaviour and sending it as a "Meta" modifier)
-
xterm, screen, screen-256color: terminal names. You can look up how they work in termcap/terminfo. Note that these can indicate an actual or emulated terminal (vt100, vt220), a software terminal (xterm) or even a terminal multiplexer (screen).
-
This can be quite convoluted: You run a terminal, connect to a different machine over ssh, and then run a terminal muliplexer: As far as programs running are concerned, $TERM is set to screen-256color. As far as screen is concerned, $TERM is xterm-256color, which was set up by SSH when connection made. screen will translate escape codes from the apps within from screen-256color type to xterm type for the surrounding terminal.
-
PuTTY actually emulates xterm, but it allows you to change the string sent to the server in case the server doesn't know about xterm.
-
termcap: a database explaining capabilities, (characteristics and input/output control sequences) used by each terminal. Now superceded by terminfo.
-
terminfo: new version of termcap.
-
escape sequences (^H, [1;2H, colours, etc)
-
$TERM: an environment variable which should match the actual emulated or real terminal being used.
-
things like backspace, arrow keys
-
BCE
-
ISO 2022 locking escapes
-
inputrc
-
Maybe also things like interactive shells, login shells, and bashrc/bash_profile etc.
-
infocmp: prints out the information from terminfo in human-readable form
-
Note that termcap/terminfo isn't used until you enter "keyboard-transmit" mode:
t_ks
in Vim,tput smkx
in the terminal. termcap:ks
terminfo:smkx
-
tput: looks up a capability in the terminfo database and prints it to stdout e.g.
tput colors
: looks up the number of colors advertised by the current terminal. If the thing you're looking up is a control sequence, rather than a characteristic, it will therefore perform the action. e.g.tput clear
: actually clears the screen -
tput Co
: "Co" is the termcap version of terminfo's "colors". However, some (maybe all?) versions of termcap will output "Co" if you ask it for the non-existing "colors" capability -
Terminal keycodes:
They are how the terminal represents a key.
These codes that are sent by the terminal to Vim.
To get a list of these keycodes you should use your terminal documentation. You can also see them by typing the command
cat
in your terminal and typing the key you want to know the keycode. For example on my terminal the key code forShift F1
looks like:^[[23~
-
tset
-
stty
-
the
tty
program/shell command -
setterm
-
locales, LANG, LC_ALL, LANGUAGE (If that's relevant)
-
Why does LC_ALL=C make case insensitive greps faster?
-
PuTTY / iTerm2 inconsistency. When I ssh home and run irssi in tmux in PuTTY, pressing
Esc 1
changes window to window 1. When I do so in iTerm2, it just inputs a '1' character. In order to change window I need to typeCtrl-v Esc 1
. Why? -
ttys/ptys
attrcolor b ".I" termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E38;5;%dm' defbce "on"
I suggest copy and pasting that without trying to understand it. No one understands termcap stuff.
Furthermore it is much more future-proof. Just as in 1970 nobody considered modifier keys, right now nobody has, say, pressure-sensitive keyboards, so nobody bothers reporting pressure information. Maybe in another 20 years time we'll all have keyboards that can recognise a gentle vs. hard press of a key, or multi-touch with two fingers, or whatever. It would be quite natural to use the 3rd CSI argument, say, to encode this.
Up CSI A Ctrl-Up CSI 1;5 A Forceful-Ctrl-Up CSI 1;5;3 A
A terminfo-driven string prefix parser will of course blow up here. The code in libtermkey, right now today, will not blow up. It won't -understand- that 3rd parameter, it cannot -report- that to its containing application, but it will correctly parse it to ignore it - reporting to its application the press of Ctrl-A, by ignoring the Forceful part.
-
http://vi.stackexchange.com/questions/2803/vim-printing-strange-characters-when-hit-home-end-stc
-
https://wiki.archlinux.org/index.php/Home_and_End_keys_not_working
-
https://groups.google.com/forum/#!topic/vim_dev/2bp9UdfZ63M[1-25]
-
What happens when I press
<C-L>
? Is it identical to clear (and if so, how?) How do we end up with a prompt onscreen? -
Keypad transmit mode
smkx
&rmkx
Vim'st_ks
&t_ke
. xterm arrows (:help raw-terminal-mode
)