15

Very often I use Shift+Up/Down to select a region in Emacs. It works fine in GUI mode, but when emacs is invoked with -nw option I can select lines only with Shift+Down. The Shift+Up doesn't work, I get this error message in the minibuffer:

<select> is undefined

How can I solve this issue?

0

3 Answers 3

16

A text terminal transmits only characters (more precisely, bytes), not keys. Keys and keychords (keys with modifiers) are encoded as character sequences. Keys that insert printable characters are sent as themselves; function keys are sent as escape sequences. Most escape sequences consist of the character Escape (?\e in Emacs syntax) followed by two or more printable characters. See Control and up/down keys in terminal for use by emacs and How do keyboard input and text output work? for more information.

Emacs has a mechanism to translate escape sequences into its own notion of keys. The translation table input-decode-map is initialized when Emacs starts (or more precisely, when a new frame is open: this variable has a different value on each terminal). Sometimes Emacs doesn't know all the escape sequences sent by the terminal.

In your case, it appears that Emacs has the wrong interpretation for the escape sequence sent by Shift+Up. You need to tell it to interpret it as S-up rather than select. First, figure out what the escape sequence is. In the *scratch* buffer, press Ctrl+Q then Shift+Up. The command C-q causes the next character, which is the escape character, to be inserted literally, followed by the rest of the escape sequence. Let's say that Shift+Up sends \e[1;2A on our system, then you would need to redefine the corresponding entry in input-mode-map:

(define-key input-decode-map "\e[1;2A" [S-up])

In principle, input-decode-map should be set per terminal. In practice, it's very rare to have two terminals in which the same escape sequence encodes different keys, so a global setting will work fine.

4
  • Your answer seems to answer my question here - emacs.stackexchange.com/questions/33988. Are the (define-key ...) commands supposed to go into init.el? Is there a detailed example somewhere I can look at?
    – vfclists
    Jul 6, 2017 at 21:50
  • @vfclists define-key commands go into your init file. That can be ~/.emacs.d/init.el or a couple of other locations. You'll find plenty of examples of uses of define-key by searching for define-key, but finding which map to bind is another matter. Several maps are involved: global-map, input-decode-map, function-key-map, keyboard-translate-map. I don't know how to answer your question because I don't know Spacemacs. I do not recommend the use of packages such as Spacemacs that messily redefine standard interfaces. Jul 6, 2017 at 22:36
  • I have switched to regular emacs to learn org-mode and I managed to get working with input-decode-map after finding some examples. Can I take it that input-decode-map applies when the user is entering text into a buffer?
    – vfclists
    Jul 6, 2017 at 23:02
  • @vfclists input-decode-map is one of the keymaps that apply. See the manual for details. Jul 6, 2017 at 23:06
4

In emacs, the region is delimited by the point (cursor) and the mark (some previous position of the cursor, which has been "marked").

Thus, to "select a region", you need to place that mark, then move your point around.

The easiest way to place the mark is to press C-SPC. Then just move the cursor around, you'll see the "region" extend as you do.

To "deselect the region", you can press C-SPC again.

Since "shift+movement" is a common keybinding in other software, by default, it emulates this "select region" behavior. However, it doesn't work in terminal, because shift cannot be sent to emacs as a modifier key.

Note that generally, the C-SPC method is much more powerful than other methods for selecting a region: you are not limited to up/down/left/right for movement (you can use search for example...), and it doesn't require that you maintain an uncomfortable key combination for a long time.

0

I created a package which allows configuring Emacs and many terminal emulators to recognize any combination of key/modifiers (including arrow keys with any combination of Shift/Control/Alt/etc.):

https://github.com/CyberShadow/term-keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.