How to copy text from command line to clipboard without using the mouse?
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)
.
Related videos on Youtube
Sancho Pancho
Updated on September 18, 2022Comments
-
Sancho Pancho over 1 year
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 toxclip
, but not sure how to do it.The method I got so far is writing a script which uses
xdotool
to addecho
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 toxclip
). 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!
-
dirkt over 7 yearsUnrelated, 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.
-
ctrl-alt-delor about 4 yearsWhat terminal are you using (it may matter).
-
-
Sancho Pancho over 7 yearsThis is great! I had no idea you could do this. VERY helpful! Is it possible to use it in konsole?
-
Marius over 7 yearsThe
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 thekeymap
feature (noting the comment about "You can only have one"). -
Sancho Pancho over 7 yearsThanks 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?
-
meuh over 7 years@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.
-
Sancho Pancho over 7 yearsI'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?