0

I can highlight text I've entered in the terminal with my mouse and then use ctrl + shift + c to copy to the clipboard and that was fine for a while. But I've tried highlighting text by pressing shift and ctrl + shift like you can do in text editors. Neither seems to work. Having a keyboard shortcut for copying terminal commands I've entered would be much easier than dragging the mouse everytime I want to copy something. Is there any way to do that using the keyboard? I've tried using ctrl + u followed by ctrl + y, but that doesn't copy text to the clipboard, so I can't use that anywhere but the terminal.

  • Doesn't arrow up help? or do you need part of the result? You can always redirect the result of a command to a file, edit the file, add execute permission and run it. Otherwise you can also use pipes to 'redirect' the output of one command to the next command. – Falk Apr 11 '16 at 18:03
  • I want to find a way to select and copy the command to the clipboard, so I can use it outside of the terminal. – Maaverik Apr 11 '16 at 19:07
  • Could you give an example? I'm not sure if I'm understanding what you mean with outside of the terminal. – Falk Apr 11 '16 at 19:12
  • I just mean copying something from the terminal to a text file or copying it to search for it online. – Maaverik Apr 12 '16 at 5:34
1

There is a set of shortcuts for terminal , and they are organized around the current cursor position.

  • You can use CtrlK shortcut to cut the text from cursor to end of line
  • CtrlU cuts from current position to beginning of line.
  • Paste with CtrlY

These two are pretty useful in particular when you want to either copy the command or its arguments.

If you are proficient with vim text editor, you can edit the command you want in a more powerful way by evoking vim with fc command.

For using the command outside the terminal, you might want to use xclip command (not installed by default) . For instance,

$ echo "some_command" | xclip -sel clip

Once you have xclip you can add the following function to your .bashrc file

to_clipboard() {
    xclip -sel clip <<<"$@" 
} 

What this does is it will copy whatever you put in front to clip board. You can use that in combination with the shortcuts above to cut test, paste it in front of the function, and it will be added to your clipboard. Small example

$ to_clipboard echo 'hello world'
  • Yeah, that works inside the terminal, but it doesn't copy the commands to the clipboard. I can't use what I've copied anywhere but inside the terminal. – Maaverik Apr 12 '16 at 5:35
  • @Maaverik that's why I've suggested you use xclip :) That tool copies whatever you give it into clipboard. That's how I copy most of my scripts and commands from terminal to here – Sergiy Kolodyazhnyy Apr 12 '16 at 5:36
  • @Maaverik here, I added a function that will basically serve as clipboard copier for you. – Sergiy Kolodyazhnyy Apr 12 '16 at 5:41
  • It works great, but I was actually hoping there was a keyboard combination I didn't know about that would let me highlight things I needed in the terminal. After that, I could just 'ctrl + shift + c'. I guess your method might be the only way though. Thanks. – Maaverik Apr 12 '16 at 9:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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