# Select text in urxvt terminal using Shift+Arrow or Ctrl+Shift+Arrow

I'm using zsh + rxvt-unicode as a terminal simulator in Archlinux. I've successfully setup navigation in the terminal using Ctrl+Arrow keys, but I can't setup the terminal to be able to select text when pressing Shift + Arrow (or Ctrl + Shift + Arrow to select the whole word). After several days of searching for solution I couldn't find one. Enabling keyboard-select in .Xresources didn't do the trick. So I'm interested in what should I do to be able to select text in the console without using the mouse just like it is done in all text editors?

-
Do you want zsh to receive the Ctrl+Shift+Arrow key presses, or do you want to select text displayed in the terminal to copy it to the X clipboard? What key sequences do Ctrl+Arrow and Ctrl+Shift+Arrow send? –  Gilles Jan 14 '13 at 0:24
@Gilles I want to select text to be able to copy it. Just like this is done in ubuntu - pressing Ctrl + Arrow highlights the text in terminal and pressing Ctrl+Shift+C copies it to clipboard. ^[Od - Ctrl + Left ^[[d - Ctrl + Shift + Left ^[Oc - Ctrl + Right ^[[c - Ctrl + Shift + Right –  RomanKapitonov Jan 14 '13 at 0:34
You seem to be describing a feature of Gnome Terminal. I don't think rxvt has this feature. –  Gilles Jan 14 '13 at 0:43
@Gilles I always thought this is a kind of basic feature –  RomanKapitonov Jan 14 '13 at 0:56
Beware that targeting resources to rxvt-unicode can be rather tricky. Are you able to set any OTHER resources except keyboard-select (and see the effects)? –  dan3 Jan 14 '13 at 8:57

This is in three steps how I made it work for me:

Step 1. Getting the meta key working:

Put URxvt*altSendsEscape: true in ~/.Xresources, then xrdb ~/.Xresources in ~/.xinitrc.

For xterm, in ~/.Xresources, put xterm*metaSendsEscape: true.

For rxvt, my intuition tells me it looks identical or very similar.

Step 2. Patching cursor movement

This will give you the Emacs meta key, so you won't have to reach for Escape. I set this up when using bash, and got it to work, and then switched to zsh; it still worked, but, as for word movement with the Emacs keybindings, it seems zsh and bash are somewhat different: the closest I got to Emacs behaviour, I got with this patch. If you're used to Emacs, it is very handy to have the same cursor movement in the shell.

Now, for example, set the mark with C-SPC, mark the previously typed word with M-b, kill it with C-w (or, without erasing it, with M-w), yank it with C-y, and so on.

OK, I know you wanted Ctrl-Left and so on, but that is a huge downtrade: that way, you have to reach for every cursor movement, and reach back to resume typing. There is a big annoyance and productivity drop! The "new" (but better) shortcuts are stiff in the beginning, but I'd advice anyone, just force yourself to use them, in no time (as you type all the time!), they'll come naturally.

Step 3. To copy to the X clipboard, I setup this (in ~/.zshrc):

# clipboard
X_CLIPBOARD='xclip -d ":0" -selection clipboard'
alias xi="$X_CLIPBOARD" # -i (or -in) is default alias xo="$X_CLIPBOARD -o"

Now, you can pipe output to xi, and paste it anywhere (works even in a non-X tty because of the -d, if X is running); to use it in a shell command, just backtick xo.

To use it in Emacs (running in a tty), I had to make a script duplicating the functionality of the xo alias above:

#!/bin/zsh
xclip -d ":0" -o -selection clipboard

And then, in ~/.emacs:

(defun pst ()
"Inserts the X clipboard (xclip -d ':0' -o -selection clipboard)
at point. M-1' (the '(1)) to insert in the current buffer."
(interactive)
(shell-command "pst" '(1))
(end-of-line) )

The reason for this is, I couldn't find a way to hook Emacs to a zsh alias, although I suspect that is possible. Anyhow, invoke with M-x pst.

EDIT

I added this workaround to make Emacs forward-char as many characters as you just inserted. (The first piece of code below is the script - see above - that has changed, too.) I use an intermediate file to do this - though, as a programmer, this goes against my intuition (as a bad habit), but, well, it seems to work. Obviously, the end-of-line (in my first answer) wasn't good as sometimes you want to insert a single word in the middle of a sentence.

#!/bin/zsh

CLIP=xclip -d ":0" -o -selection clipboard
CLIP_LEN=echo -n $CLIP | wc -c echo "(set-variable '*clip-len*$CLIP_LEN)" > ~/.clip_len
echo -n \$CLIP

(setq *clip-len* 0)

(defun pst ()
"Inserts the X clipboard (xclip -d ':0' -o -selection clipboard)
at point. M-1' (the '(1)) to insert in the current buffer."
(interactive)
(shell-command "pst" '(1))