How to copy text from command line to clipboard without using the mouse

clipboardcommand linepastexclipxdotool

I'm trying to figure out a way to copy the current text in a command line to the clipboard WITHOUT touching the mouse. In other words, I need to select the text with the keyboard only.
I found a half-way solution that may lead to the full solution:

Ctrl+a – move to the beginning of the line.

Ctrl+k – cuts the entire line.

Ctrl+y – yanks the cut text back.

Alternatively I can also use Ctrl+u to perform the first 2 steps.

This of course works, but I'm trying to figure out where exactly is the cut text saved. Is there a way to access it without using Ctrl+y ?
I'm aware of xclip and I even use it to pipe text straight to the clipboard, so I was thinking about piping the data saved by Ctrl+k to xclip, but not sure how to do it.

The method I got so far is writing a script which uses xdotool to add echo to the beginning of the line and | zxc to the end of the line, and then hits enter (zxc being a custom alias which basically pipes to xclip). This also works, but it's not a really "clean" solution.

I'm using Cshell if that makes any difference.

EDIT: I don't want to use screen as a solution, forgot to mention that.

Thanks!

Best Answer

If using xterm or a derivative you can setup key bindings to start and end a text selection, and save it as the X11 primary selection or a cutbuffer. See man xterm. For example, add to your ~/.Xdefaults:

XTerm*VT100.Translations: #override\n\
    <Key>KP_1: select-cursor-start() \
            select-cursor-end(PRIMARY, CUT_BUFFER0)\n\
    <Key>KP_2: start-cursor-extend() \
            select-cursor-end(PRIMARY, CUT_BUFFER0)\n

You can only have one XTerm*VT100.Translations entry. Update the X11 server with the new file contents with xrdb -merge ~/.Xdefaults. Start a new xterm.

Now when you have some input at the command prompt, typing 1 on the numeric keypad will start selecting text at the current text cursor position, much like button 1 down on the mouse does. Move the cursor with the arrow keys then hit 2 on the numeric keypad and the intervening text is highlighted and copied to the primary selection and cutbuffer0. Obviously other more suitable keys and actions can be chosen. You can similarly paste the selection with bindings like insert-selection(PRIMARY).