11

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!

terdon
  • 234,489
  • 66
  • 447
  • 667
Sancho Pancho
  • 123
  • 1
  • 6

2 Answers2

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

meuh
  • 49,672
  • 2
  • 52
  • 114
  • This is great! I had no idea you could do this. VERY helpful! Is it possible to use it in konsole? – Sancho Pancho Jan 08 '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 08 '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 09 '17 at 00: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 09 '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](https://linux.die.net/man/1/csh). – meuh Jan 09 '17 at 13:35
  • I'm kind of going out of scope with this question, but is there a way to perform editor-commands without using the keybindings? For example: can I yank without pressing Ctrl-y? – Sancho Pancho Jan 10 '17 at 07:52
  • I'm not a csh expert, but I don't think so. – meuh Jan 10 '17 at 17:26
0

If you're willing to execute the command first, the following pattern works. Grab the last command from fc, remove the history number, save it to your clipboard. (This happens to be Zsh on a Mac today, but adjust for your setup):

# Call the save-to-clipboard function something like "save"
% save() { fc -l -1 | gsed -r 's/^\s*[0-9]+\s+//' | pbcopy }

# Execute your interesting saveable command
% some-interesting-command with args...

# Put the command onto your clipboard
% save

You could also bind it to a key-combo to avoid typing save.

Micah Elliott
  • 1,007
  • 11
  • 7