41

These are the standard text editing keyboard shortcuts that I use constantly whenever editing text in, literally, any linux application other than terminal emulators:

  • + arrows to move left+right
  • ctrl+ or ctrl+ to move an entire word
  • home/end to move to start/end of line
  • ctrl+c/ctrl+v to copy/paste [some terminals can use shift+ctrl+c/shift+ctrl+v; this is a good substitute]
  • shift+ or shift+ to highlight text
  • shift+ctrl+ or shift+ctrl+ to highlight an entire word

I have never found a combination of shell plus terminal emulator that allows the last two items on this list, and it drives me nuts. Obviously terminal emulators support highlighting (the mouse can do it), and they support the use of the ctrl and shift keys as modifiers (they can be used to move the cursor an entire word, and to capitalize letters, respectively; [edit:] they can even be used together to copy/paste with shift+ctrl+c and shift+ctrl+v), so what is the issue preventing this functionality? I have several questions:

  • Is this an issue with my terminal emulator, or with my shell (bash, though I'm willing to change)?
  • Why do terminal emulators/shells not conform to this otherwise universal standard?
  • If there is an actual reason, is it ancient and obsolete, or is it still relevant to a significant number of desktop linux users?
  • Is there any kind of workaround?
  • Is there some obscure program I can use that supports this?
  • Is it feasible to modify the source of, say, gnome-terminal to support this?

I know text can be copied/pasted with the mouse, that's not what I'm asking about. I'm asking why I can't do these things with the keyboard in a terminal emulator.

5
  • Note that selecting text with the mouse does not change the cursor position. If you'd mix in text selection with entering regular commands on the keyboard, it'd just be really confusing beyond the boundaries of the command you just typed and not yet executed. Are you able to select text beyond the boundaries of the text box on a web page with the keyboard? Please also note that there are programs like screen that allow entering a different mode that has text selection for the entire terminal, but of course does no longer allow entering commands.
    – Daniel Beck
    Commented Feb 22, 2013 at 17:25
  • 2
    Daniel: the Matlab console, for example, is a CLI that allows text highlighting with the keyboard. It works perfectly in my experience; it allows selection beyond the current not-yet-executed command if desired, personally I have little use for that.
    – monguin
    Commented Feb 22, 2013 at 17:38
  • see also stackoverflow.com/questions/1536757/… Commented Jul 5, 2013 at 8:02
  • and here as well stackoverflow.com/q/312213/1172302 Commented Jul 5, 2013 at 8:03
  • 3
    I'll mention ble.sh since not enough people seem to know about it. It stands for Bash Line Editor, and allows you to have things like Ctr + Shift + Left/Right to highlight stuff in the way we want it to. It is very nice. Its written in Bash so as long as your not using zsh or fish it should be pretty portable. Commented Feb 14, 2021 at 23:58

9 Answers 9

6

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.

14
  • 13
    I appreciate the time you took to write out your answer, but somehow I don't feel like I have any greater understanding of why this is not an available feature in terminal emulators. You pointed out a new idea to me - that a terminal emulator is unique in that it can run other programs - but I don't understand why only a select subset of key combinations are impossible to make available to the user.
    – monguin
    Commented Feb 25, 2013 at 16:35
  • 2
    Terminal emulators emulate physical terminals. They were not created from scratch to provide a command-line interface. Things like Control-C had established meanings long before Microsoft decided to co-opt them as keyboard shortcuts, and terminal emulators must adhere to them.
    – chepner
    Commented Feb 28, 2013 at 20:53
  • 2
    I think the devil is in the details here. It's not impossible: screen has a mode to do what you want, I think. But to accomplish it, you have to switch into a "I want to select things" mode. gnome-terminal would have similar issues: they probably didn't implement it because it isn't exactly something you're doing. Keep in mind the shell isn't the only program running: any program could be running, and it could want to use any key sequence that you might use for copy/paste for it's own purposes. screen has ways to send the keys it overrides, and gnome-terminal tries to not step on toes.
    – Thanatos
    Commented Mar 1, 2013 at 21:58
  • 1
    Block select is a means of selecting a rectangle: Hold control and drag in gnome-terminal, and you should immediately see how it's different. Most GUI terminal emulators have this capability, and most advanced text editors to as well. (See Visual block in vim, for example.)
    – Thanatos
    Commented Mar 1, 2013 at 21:59
  • 1
    Ah, then I'm familiar with block select, but wasn't aware of the name. I'm also not aware of a GUI keyboard shortcut for that action, so it seems unnecessary to invent one for the sake of answering my question. I appreciate everyone's comments here, but as I'm still unsatisfied, I suppose all I can do now is dig into the source myself...
    – monguin
    Commented Mar 1, 2013 at 22:07
2

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:

  1. 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.
  2. 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 the ls -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.

2
  • Thanks, I will read that TTY article, but haven't yet. That might explain the gap in my understanding of the difference between "terminal emulators" and "desktop applications that handle text programs". My earlier example, the Matlab console, behaves exactly how I want. I don't understand why there can't be a desktop application with that behavior, where the text program running within it is something besides Matlab. Say I wanted to use only the most basic shell commands - ls, cd, cp, mv - then I don't see any conflict. Programs like screen or tmux, I understand might make things trickier.
    – monguin
    Commented Nov 9, 2018 at 16:22
  • Some good thoughts there - Ultimately, what you imagined for how a terminal might handle this did end up implemented mostly in the way you describe it in Windows Terminal's "mark mode" feature just recently. Commented Nov 17, 2022 at 15:58
1

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&copy.

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

1

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.

1
  • 1
    Thanks for the response. In the mean time I've become accustomed to OSX and Emacs bindings, and also just given up on the dream of ctrl+shift+arrow in a terminal...
    – monguin
    Commented Oct 28, 2016 at 2:50
1

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.

0

(EDIT: These are Windows' cmd terminal keys. I didn't notice you use Linux)

In case it helps:

The entire text can be coppied using Alt+Space, E, S, Enter (takes a second or less)
and of course a decent editor like VS Code can be used to pull the relevant part (takes a few seconds).

Coppied text can be pasted using Alt+Space, E, P.

And screen can be scrolled after using Alt+Space, E, L.

To answer the original question:

First, I completely agree that this feature should exist.

As to why the current terminal designers don't use it, Thanatos mentioned in his answer. But a terminal application might exist that uses it and has a workaround for the shell apps that need the keys.

As to why the old terminal designers created such a tradition (if they used the keys for text selection, then shell apps wouldn't use them) probably because when hard-copy terminals were superseded by video terminals and the first text editors were emerged, it was still some 15 years before this great UI idea of text selection came into existence! :)

2
  • 1
    Thanks for the input! Part of my frustration comes from not fully understanding the historical evolution of terminals and emulators, nor why we are still stuck using ALL of these old protocols. For example, why can't iTerm (popular OSX terminal app) have a "modern" mode that drops support for some old behavior, while adding support for newer features? It's more complicated than I can imagine, I'm sure. It's just hard for me to believe I'm the only one who wants such a thing.
    – monguin
    Commented Mar 13, 2020 at 19:33
  • I think we're in fact talking about shells, because it's a shell that makes the rules and terminals only comply. And it's a correct complain. Many of the shells' implementation details are not a good design practice, resulting in unwanted behaviors that need workarounds, and several design corners that need extensive amounts of time to check them in the docs and shoot the troubles that the yet-not-learned-ones make. A truly orthogonal shell is indeed a very revolutionary idea.
    – Small Boy
    Commented Mar 15, 2020 at 22:37
0

Until ubuntu 20, you could scroll up to the beginning of the output, start highlighting with the mouse, then press any key to bring the selection down to the bottom.
Now, pressing a key cancels mouse selection so the only way to copy a portion of multi-page output (besides saving to file and opening separately) is to bring the selection to the bottom of the screen and wait a minute or so for it to scroll to the bottom of the output.

1
  • There is another way. Right-click on the terminal window and select "Show Menubar". In the menubar, choose Edit -> Select All, and then copy that selected text to your clipboard. That will give you the entire contents of the terminal scrollback. You can then paste it into a text editor and trim out the portion you want. For this to work, you can't have a process currently producing output in the terminal window - it has to be sitting still. Commented Sep 8, 2021 at 19:06
0

I will answer just this part: "If there is an actual reason, is it ancient and obsolete, or is it still relevant to a significant number of desktop linux users?"

As seen from previous answers, there are actual reasons, but are not relevant to desktop linux users.

Ironically, on Windows you could use ConEmu as a console window that supports all the shortcuts you mention (also when using Bash in WSL, and when remote connecting to a Linux server), therefore it should not be impossible to achieve this also on Linux. So in conclusion those reasons are indeed "ancient and obsolete".

0

In my experience Terminal only has 1 buffer entry and you can only fill it if you "Cut"(CTRL-K), that word or phrase. If you CTRL-< to behind a word say, you can CTRL-K, which deletes it from view, then CTRL-Y will Re-Paste it. Note though, this is NOT saved in your 'External' Clipboard history.

My workaround is:-

  1. Use a clipboard manager with history, e.g "Diodon"
  2. use a shortcut to list your Diodon history.
  3. Select an item from Diodon list.
  4. Paste with CTRL-SHIFT-V.

Also, If you don't already, get to know your way around Terminal 'history' commands. Used in conjunction with this, can make things more efficient.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .