I've enabled the macOS's terminal preference "Use Option as Meta key" but seem unable to have emacs behave in the terminal as it does when running the GUI.
I have the following use-package definition for drag-stuff:
(use-package drag-stuff
:ensure t
:bind (("M-<up>" . drag-stuff-up)
("M-<down>" . drag-stuff-down)))
When running the emacs GUI, option+shift+up/down correctly moves the line(s) up or down as expected.
However, in the terminal (emacs -nw
) the commands are not recognised. The mini buffer says Esc <up> is undefined
or Esc <down> is undefined
.
When I look to describe the key sequence from the terminal I see this:
Esc <up> (translated from Esc M-O A) is undefined
Doing the same in the GUI I'm presented with the following **Help**
window:
<M-up> (translated from <M-S-up>) runs the command drag-stuff-up
(found in global-map), which is an autoloaded interactive Lisp
function in ‘drag-stuff.el’.
It is bound to <M-up>.
[Arg list not available until function definition is loaded.]
Not documented.
I've seen that there are a number of questions on this kind of problem, but I've unfortunately come up short in finding something that works. It seems to be related to Shift+Up isn't recognized by Emacs in a terminal.
If I open the scratch buffer and attempt to see the escape sequence for shift+up
(i.e., key sequence C+q S-up
) I see ^[OA
and similarly ^[OB
for shift+down.
If I include the option (i.e., meta) key I see this for both option+shift+up
and option+shift+down
: ^[
, which is weird.
Any helps on making things as consistent as possible between emacs GUI and NW would be much appreciated. Thanks!
Full emacs.nix
config, for context, is defined here if that helps.
\033
,\e
, and\x1b
are all different ways of encoding the same escape character. 033 is in octal and 1b is in hexadecimal, but they both have the same value, which is 27 in decimal. CSI is actually the pair of characters\033[
. The control sequences you are describing here are documented in the XTerm Control Sequences document as well. They came from the VT510. – db48x Feb 10 '22 at 14:34terminal-app-function-keys
—the choice of the numbers is a bit weird. Back in the day, single bits of the numbers would instead be assigned to each modifier, so combinations would be made from several bits set, and checking for modifiers could be done by bit-and'ing the number with a mask. E.g. if 1 is shift and 2 is alt, then 3 is them both together, and 7 would be shift-alt-control. – aaa Feb 10 '22 at 17:38\033
,\e
, and\x1b
syntaxes are just different styles of escaping. Different contexts have different support for different escape sequences. You may be more familiar with the escape sequence\n
which is used to represent the newline character. These are used because the escape and newline characters are hard to type directly, and difficult to edit once included in a file. – db48x Feb 10 '22 at 20:45