4 ms·
Some of this is not quite right, or right by accident of a particular virtual terminal's features and configuration. Having the Alt/Option modifier send a pref
by kps 3y ago
Some of this is not quite right, or right by accident of a particular virtual terminal's features and configuration.
Having the Alt/Option modifier send a prefix ESC is usually a terminal configuration option, though often the default. The most common alternative (other than doing nothing) is to set the 8th bit, which was useful in the ISO8859 days but not so much with UTF-8.
The vim section of this article depends on the prefix-ESC behaviour, but vim and neovim (and probably other non-ancient editors) will put a capable terminal into modifyOtherKeys mode. In this state the terminal sends a more detailed escape sequence for modifier key combinations, which permits binding combinations that are not distinguishable in the default ‘plain ASCII’ mode. For example, Control-D will send ESC [ 27 ; 5 ; 100 ~ while Control-Shift-D will send ESC [ 27 ; 6 ; 68 ~ (rather than EOT for both) and Alt-D will send ESC [ 27 ; 6 ; 68 ~ rather than ESC D.
(Oddly, the author seems to use vim as an editor but emacs editing mode rather than vi editing mode in the shell.)
The ‘special case’ control combinations are explained by the https://en.wikipedia.org/wiki/Bit-paired_keyboard https://en.wikipedia.org/wiki/Bit-paired_keyboard where Control does merely flip a bit, originally electromechanically.
- jmholla 3y ago> (Oddly, the author seems to use vim as an editor but emacs editing mode rather than vi editing mode in the shell.) I do the same. I gave vim bindings a try in my shell, but I just couldn't get the hang of it. I think vim excels when you are editing multiple lines. And, I can always get to vim to edit the commands if things are getting nuts with Ctrl+X Ctrl+E.
- mangamadaiyan 3y agoFunnily, these days the first thing I do when I start typing at someone else's terminal is "set -o vi". I cannot function effectively without Vi mode in my shell :)
- deleted 3y ago[deleted]
- susam 3y ago> or right by accident of a particular virtual terminal's features and configuration This is a fair point. Indeed different terminal configurations can produce different results. I have added a little note to my post to clarify this. Thanks for the feedback. > Oddly, the author seems to use vim as an editor I use Emacs. > The ‘special case’ control combinations are explained by the https://en.wikipedia.org/wiki/Bit-paired_keyboard https://en.wikipedia.org/wiki/Bit-paired_keyboard where Control does merely flip a bit, originally electromechanically. I don't think this explains the special cases in a general sense. Firstly, the Wikipedia article discusses shifted keys, not control codes. Secondly, the special cases still need to be explained on a case-by-case basis. It is clear that the control modifier flips a bit but which bit? In case of ctrl+@, ctrl+g, ctrl+h, etc. the 7th LSB is flipped. In case of ctrl+space, the 6th LSB is flipped. A more general rule that covers most of the cases is that the ctrl modifier resets the 6th and 7th LSBs. The exact implementation details, however, vary. As an aside, all of this still leaves how ^? represents DEL in stty and in other contexts requiring printable forms of control characters as a special case. In this case, again, we need to flip the 7th bit. Further, the fact that DEL (decimal 127) is a control character is itself a special case because it is the only control character that has its higher bits (6th and 7th LSBs) set.
- kps 3y agoThe original 1963 ASCII was upper-case only, so it's a mistake to think of Control as modifying lower-case letters. (It also had slightly different control character assignments, and ↑ ← rather than ^ _ for 0x5E 0x5F, but those don't matter here). Control consistently toggles the 0x40 bit — so Control+A = 0x40^0x41 = 0x01 and Control+? = 0x40^0x3F = 0x7F = DEL. Of course, ? is typed as Shift+/. Shift consistently toggles the 0x10 bit: Control+Shift+/ = 0x40^0x10^0x2F = 0x7F = DEL. This is why on upper-case-only bit-paired keyboards, right down to the Apple II, you type Shift+P = 0x50^0x10 = 0x40 for @. The manual for the ur-ASCII Teletype Model 33 is explicit about how this works in the keyboard section. Applying this to space (note Shift+0 is space on a bit-paired keyboard) would give you 0x60, which is out of range for 1963 ASCII. Evidently some early terminal decided this should wrap around to 0x00. (The TTY 33 didn't do this, since it's too complicated to do mechanically.) I had not heard of this before, and always used Control-@ (0x40^0x40 = 0). Incidentally DEL isn't a control code per se. It's 0x7F because a set bit is a hole on paper tape, so it's the only code such that punching DEL over any another code always results in DEL.