Make bash differentiate between Ctrl- and

2020-06-23 08:25发布

问题:

I was wondering whether there was any way of making bash send different codes for key combinations that include the shift key? for instance, (Ctrl+V shows me that) Ctrl+N and Ctrl+Shift+N are interpreted the same (^N). Or is there a terminal that can make the difference? Or can bash me modified so that it does?

回答1:

A terminal doesn't interact directly with your keyboard; it interacts with a stream of bytes that it receives, which are usually (but not necessarily) generated by your keyboard. For the printable ASCII values, there is an obvious correspondence between the value and a key (or combination) on your keyboard. ASCII 97 is a, ASCII 65 is Shifta, and so on.

However, there are the 32 non-printing control characters from ASCII 0 to ASCII 31, called which because they were intended to control a terminal. In order to enter them, the Control was added to allow you, in combination with the other keys, to generate these codes. A simple scheme was used. Pressing Control-x will generate the control code corresponding to subtracting 64 from x. Since @ generates ASCII 64, Control@ generates ASCII 0. The same mapping holds true for A through _ (consult your favorite ASCII reference to see the rest of the correspondences).

However, whether or not you need a shift key to generate ASCII 64 through ASCII 95 depends on your keyboard. On my US keyboard layout, only [ and ] can be typed without a shift key. (Remember, it's the uppercase-letter ASCII range we're using here, not the lowercase.) So to simplify, I suspect it was decided that Shift would be ignored in determining which keycode is sent with Control-x. (Note that if for some reason your keyboard had two of the characters between 64 and 95 generated by a key/Shift-key pair, your terminal would need to define an alternate mapping for the associated control character.)

All this is simply(?) to explain why ControlShift-x and Control-x are typically the same. Obviously, your modern operating system can distinguish all kinds of keyboard combinations. But out of the myriad possibilities, only 256 of them can send unique values to a terminal; the rest must necessarily duplicate one or more of the others. To be useful, they need to be configured to send some multiple-byte sequence to the terminal, typically beginning with ASCII 27 (ESC). When terminals receive that byte, they pause for a moment to see if any other bytes are coming after. Keys like function keys, arrow keys, etc. have fairly standard sequences they send, which the terminal interprets in various ways. Other keys (like ControlShiftn in your example) have no agreed-upon meaning, and so your terminal emulator must assign one. Most emulators should allow you to do this, but how they do so is, obviously, program-specific.



回答2:

There's are two great write-ups on keyboard shortcut customization in bash here:

Bash: call script with customized keyboard shortcuts?

In bash, how do I bind a function key to a command?



回答3:

iTerm2 allows you to map key combinations like Control+Shift+, (which should represent C-<) to an escape sequence. Emacs translates certain escape sequences to the expected key sequence by default. Therefore, by remapping the desired key combination in iTerm to the appropriate escape sequence, you can get the behavior you want. See this response for specifics.



标签: linux bash