Why can't I highlight text in a linux terminal emulator with shift+arrow keys?
Solution 1:
I think it'd be most useful if I took this a piece at a time. The general problem is: who is the key press intended for? The terminal, or the program running inside the terminal?
As an example, "screen", which is kindof a terminal, uses Ctrl+A as a prefix for its commands, to distinguish them from things going to the running program itself. (And provides a way to send Ctrl+A.)
gnome-terminal
has several keys that it captures to do various things, including some of the ones you ask about.
Also keep in mind that a terminal's "highlighting" is separate from the terminal's cursor position. Some terminals have no ability to highlight at all.
Now, taking this key combinations at a time:
left+right arrows to move left+right ctrl+arrow to move an entire word home/end to move to start/end of line
Move what left and right? Bash can be configured to do this, and typically is by default. Typically, these move the cursor position.
ctrl+c/ctrl+v to copy/paste
First: does copy/paste even make sense? If you're at a VT, you don't really have a clipboard, especially if X isn't running.
Some terminals can copy text in the output, and some will also "paste" by simulating you typing the contents of the clipboard. Ctrl+Shift+V, for example, is paste in gnome-terminal
, which may help. (And Ctrl+Shift+C is copy.) As discussed earlier, the big problem with Ctrl+C and Ctrl+V is they overlap with common terminal/program commands. (Ctrl+C is send interrupt (SIGINT) and Ctrl+V is verbatim.)
Some terminals also support two modes of copying data: a more normal "just copy", and what's known as "block select" or "block copy". (Hold Ctrl, and then drag while in gnome-terminal
for example.)
Additionally, xsel -b
can be used to pipe clipboard contents around. Depends on the exact situtation whether xsel
or the terminal's version of paste is more useful. See man xsel
.
shift+arrow to highlight text shift+ctrl+arrow to highlight an entire word
Your terminal's highlight (if it has this capability) is separate from cursor position. Again, lack of available key combos is probably a factor. Keep in mind a highlight has two positions: either the start and end, or the upper left and lower right corners. How do you manage both?
Finally, note that many GUI terminals, double-clicking a word will highlight it. (And in X, copy to the primary selection.)
screen
, as an example, has keys to switch into a mode for moving around the buffer (previous output) and copy/pasting.
I think if you make adequate use of xsel
and the primary selection, you will find clipboard operations are both rare enough and complex enough to merit using the mouse.
Solution 2:
I'm not an expert of terminal emulators but...
applications like bash (readline) running into a terminal emulator know nothing about the running X window system and the X window they are within, they know stdin and stdout on a terminal device (ttyS/ttyUSB/tty/pts on linux).
The problem isn't show some highlighted text but how to let know the X window application (the terminal emulator) that text been selected, through these terminal devices.
I guess the X terminal application open one of those devices in input and output and then translate X key events to properly output (from X side) input (from bash side). Viceversa the bash output stream to the X terminal as input, here the X terminal process this input to move the cursor, fill the backround with some color, according with the bash application output.
For my knowledge escape codes may be used to control special behaviors, like clear, fill the background, move the cursor, and maybe some custom escape code could be added to let know the X terminal that a text from row,col to row,col has been selected, just an example, maybe instead the selected text could just be returned (an implementation detail).
I guess not being a standard definition you'll have to patch every application you want to support it to know about the key combination has pressed and output the appropriate escape code, the readline if you want it in the bash, the X terminal emulator on the other side to process properly the escape code (and finally send the information to the clipboard). Probably implementing this as a terminal capability would save you from patch every single application. I hope (and guess) the terminal device drivers in the kernel want to know less as possible about escape codes, so if you are lucky no patch will be required.
The X terminal draw the output, so it easily know, when you use the mouse, what text/characters are you selecting.
A graphic text widget know everything about its X window that why it is so easy to implement select©.
EDIT
Here this urxvt-9.16-image-display patch could be a good start point to understand what is needed to support new escape codes. http://lists.schmorp.de/pipermail/rxvt-unicode/2013q1/001736.html
Solution 3:
The answers are given are good explanations of why doing this is hard. Here is something you can do in gnome-terminal to setup ctrl-c and ctrl-v to copy and paste, while rebinding other keys in the terminal discipline with stty
to send SIGINT
and insert a character verbatim. This is not a complete solution because some programs disable the terminal discipline, and you won't be able to send them '^C' and '^V'. More info here.
In your shell startup script (e.g. ~/.bashrc
, ~/.zshrc
, ~/.rcrc
), do
stty intr '^Q' 2>/dev/null # To send SIGINT I will use ctrl-q
stty lnext '^A' 2>/dev/null # To insert a character verbatim I will use ctrl-a
Then in gnome-terminal Edit > Preferences > Shortcuts you can bind Copy and Paste to ctrl-c and ctrl-v. Note that the terminal will get the key events before anything is sent to the terminal device, so from then on you wont be able to send '^C' and '^V' to any process running on the terminal.
I just did this and I will see how it goes, and what problems it causes. I did the stty's conditionally to apply them only when I am running X.
Solution 4:
As Thanatos mentioned, there is a distinction to be made between the terminal emulator (running on X Windows or Wayland) and programs running within the terminal (let's call that the "shell", though it might not be); these two things are isolated from each other (see technical details).
The first items on your list (arrow keys, Home/End, etc.) are handled directly by the program inside the terminal, and so the cursor position is controlled by the program inside the terminal.
The copy and paste shortcuts (Ctrl+Shift+C and Ctrl+Shift+V), on the other hand, are handled by the terminal emulator, which understands the mouse (so you can select text with the mouse), it knows what is on the screen (so it can copy), and can send keypresses to the program inside (so it can paste).
In order to support Shift+Left and Shift+Right, either the terminal emulator or the shell would have to handle the keystroke. Either way we have a problem:
- The shell can't easily handle these keys because eventually you'll want to copy the selected text to the clipboard, and the clipboard is an X Windows concept that the shell doesn't necessarily have access to (but see
xclip
). And as far as I know, if the shell supported text selection, Linux does not define any mechanism to notify the terminal emulator about what is selected. - Meanwhile, the terminal emulator isn't in charge of the cursor's location. Even if the terminal emulator could change the cursor's location, it probably doesn't know where the current line of text begins and ends. For example, the terminal might contain text like
~ $ ls -l
and the terminal emulator is unaware that only thels -l
part belongs to the user.
It's not hard to imagine a terminal emulator that supports selection with Shift+Arrows, but I guess it would have to hide the shell's cursor and introduce its own "fake cursor" that exists temporarily to help you select something, and then you could press Ctrl+C / Ctrl+Shift+C / Ctrl+Ins to copy (or Esc to cancel) and show the real cursor once again. This wouldn't have all the capabilities of a normal selection of course - notably cut and delete wouldn't exist.
Solution 5:
These are CUA keyboard bindings you describe, a standard from IBM in the mid 80s:
https://en.wikipedia.org/wiki/IBM_Common_User_Access
They are typically implemented in all desktop environments created since. DOS, Windows, Motif, even Netware tools all converged on this standard. One exception is the Mac, which uses a quite similar but different set (Cmd instead of Ctrl) from the same time period.
Unix terminals (and later emulators) far predate this standard and mostly ignored it however. Also, if they did implement these key bindings, it might interfere with TUI programs that already use them for other functionality. So while technically doable, problematic too.
Apps:
The micro
text editor is the best I've seen at emulating a GUI text editor, similar to DOS edit
but with more modern features a la Sublime Text. ne
is an older one in Debian repos, and even nano
can be configured with sane keybindings. But the bare terminal/shell, no.
With libvte
it might be easy to build a rudimentary virtual terminal that handles these keybindings yourself. A lot of work for a tiny gain however.