10

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!

  • Unrelated, but also good to know: Shift-Ins pastes the selection in an xterm, if you not only want to cut text, but paste as well. – dirkt Jan 8 '17 at 17:16
7

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).

  • This is great! I had no idea you could do this. VERY helpful! Is it possible to use it in konsole? – Sancho Pancho Jan 8 '17 at 16:00
  • 2
    I had a quick look at konsole's manual but it doesn't mention this sort of control. It is unlikely you'll find this in most terminal emulators as they prefer to provide settings that are simpler to manage through menus and dialogs. – meuh Jan 8 '17 at 16:29
  • The translations resource is the X Toolkit feature alluded to here and there which has no counterpart in other toolkits. However you can switch between sets of translations using the keymap feature (noting the comment about "You can only have one"). – Thomas Dickey Jan 9 '17 at 0:08
  • Thanks for all the comments. Still didn't find out about the ctrl-k/ctrl-y clipboard though, does anyone know how to access that or where does it actually save the cut text? – Sancho Pancho Jan 9 '17 at 12:04
  • @SanchoPancho Depending on your csh, it implements an internal kill-ring which keeps cut text and provides it back with the yank and yank-pop bindings. I dont think you have any other access to this ring. see man csh. – meuh Jan 9 '17 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.