Highlight selected text with cursor keys on Emacs in a Terminal.app terminal window on OS X (10.5.8 in this case):
Apparently, shift-arrow key selection (a.k.a
shift-select-mode) was enabled by default in Emacs 23.1 but when I was running
emacs in a Terminal.app terminal window on OS X it never seemed to work--moving with the arrow cursor keys while holding down shift just moved the cursor and didn't select anything. (No selection was visible.)
In turns out that by default the Terminal.app terminal emulator just sends not-modified cursor key escape sequences by default--so
emacs never knows that you're holding down the shift key.
In order to activate shift-select you need to modify the Keyboard settings via
Terminal > Preferences... > Settings > Keyboard. (Illustrated in "How do you move to the next or previous word in a Terminal in Mac OS?"--although the values didn't work for me.)
But, the question is what additional key binding/action to use in each instance. (I had existing values for "control cursor left" and "control cursor left" but in fact they didn't work--I don't know if they were default settings or not.)
After much looking around it appears the correct values are:
shift cursor down \033[1;2B
shift cursor left \033[1;2D
shift cursor right \033[1;2C
shift cursor up \033[1;2A
Note that some sources may list
\033[2D without the
1; prefix () but that is not sufficient to work--and indeed was the problem with the existing definitions. The
\033 is the octal for
^[--all of which makes searching for correct values a little difficult. :) So, you might see, for example, shift left arrow expressed as
^[[1;2D for example. But the preferences setting dialog uses the
\033 preference, so I've used that here.
With these new key bindings in place in the Terminal.app application
emacs responds correctly with shift-arrow key selecting text and highlighting it.
As an aside, while I was researching why it wasn't working I thought perhaps the selection was being made but just not visible--but trying to copy the text resulted in this error message:
The mark is not set now, so there is no region
Note that if you want to combine shift plus control modifiers, for example, you'll need to add some key bindings. e.g. extracting the values from from
(define-key map "\e[1;6A" [C-S-up])
(define-key map "\e[1;6B" [C-S-down])
(define-key map "\e[1;6C" [C-S-right])
(define-key map "\e[1;6D" [C-S-left])
(define-key map "\e[1;6F" [C-S-end])
(define-key map "\e[1;6H" [C-S-home])
FWIW, the correct values for the default two control arrow key combinations are:
control cursor left \033[1;5D
control cursor right \033[1;5C
You can find out the values currently sent by to terminal with
xxd--although it turned out I could use those commands in a Linux VM to get the actual key sequences, not just the ones Terminal.app was set up to send.
You may, like me, be wondering where these odd looking character strings originate from. In these case they seem to come from the
xterm code itself--although I suspect it goes further back than that given this extremely detailed information:
ESC [ character sequence is the "Control Sequence Introducer".)
The following were helpful when I was initially trying to find the correct character sequences:
The first source on my own machine that I could find for locating other values was
/usr/share/emacs/22.1/lisp/term/xterm.el.gz which can be viewed with:
Now, I don't know if I broke something in the process or if I was just out of the habit but either way, I couldn't skip words at the command line, so ended up adding a
.inputrc file to my home directory with the following bindings in it:
# Remember, in the shell: "the `C-x C-r' command re-reads this init file, thus
# incorporating any changes that you might have made to it."
(I started writing it by hand but then correctly figured someone else had probably already done it and found Get control-left and control-right to move between words on Mac OS X.)
But read on to see what happened next... :)
cursor-type in Emacs on Mac OS X in a Terminal.app terminal
There was one thing I found problematic about the visible selection with the shift-select and that's that the selection started with the character before the character under the block cursor.
The first advice I saw suggested setting
I tried a bunch of variations but couldn't get the cursor shape/type to change in
emacs in the Terminal.app terminal window.
When even setting the
visible-cursor did not change anything I discovered:
It turns out: You cannot change the cursor type on OS X Terminal.app from within the terminal.
But after much frustration I discovered the option to change from block to vertical line to underline is in fact buried in the Terminal.app preferences:
Terminal > Preferences... > Settings > Text as the
Cursor radio button.
I changed the cursor type to "Vertical Bar" and the selection now visually starts from the "correct" character.
I do wonder if it would be better to stick with the block character and change the select code to start from that character but I'll see how things go with the Bar cursor.
Given it was much harder to see the Bar cursor I've increased the darkness of the colour and turned on blinking (which kinda matches the visibility in a normal text edit area anyway). These changes make it more visible so we'll see how it goes.
Unfortunately this means you can not take advantage of features like "Changing Cursor Dynamically" unless you use a kinda hacky work-around like this
vim user. Which, while impressive, lacks a certain something. :)
You do not want to know how long it took me to sort all this out... :/