5

enter image description here

gsettings set org.gnome.desktop.interface clock-show-date true

I want to copy this command into the clipboard so that I can paste it in a file or an Ask Ubuntu question easily.

I know I can copy this by selecting it and pressing Ctrl+Shift+C, but I want to get a mouse free answer.

  • @SergiyKolodyazhnyy That question was asked about terminal output, but my question is about terminal input(the command). – alhelal May 25 '18 at 17:08
  • There's no mouse-less solutions for terminals. There are keyboard shortcuts to copy/paste within terminal itself, but nothing that copies into GUI clipboard, because none of those features interface with GUI clipboard anyway. The "Copy" feature from right click menu is feature of GUI windows, which use same mechanism as what Ctrl+Shift+C. So easiest way is to echo the command via pipe to either of xclip or xsel as explained in the linked post. – Sergiy Kolodyazhnyy May 25 '18 at 17:18
  • As for input, xclip -sel clip alone reads from stdin, so you can just type in your command, then Ctrl+D, and it'll go to clipboard. – Sergiy Kolodyazhnyy May 25 '18 at 17:20
5

You can use bash’s History Expansion, to copy the previous command line to your clipboard without performing command substitution, expansions etc. do:

echo !!:q | xclip -sel clip # or respectively
echo !!:q | xsel -ib

!! is a shortcut for !-1, referring to the previous command. Colon : precedes the modifier q, which lets the expansion quote the substituted words with single quotes to prevent further substitutions.

Example run

$ uname -r                        # run your command
4.10.0-35-generic
$ echo !!:q | xclip -sel clip     # copy the previous command to clipboard 
echo 'uname -r' | xclip -sel clip # this line shows what’s done
$ xclip -sel clip -o              # print clipboard content (just for testing)
uname -r
$ $(xclip -sel clip -o)           # run command stored in clipboard (just for testing)
4.10.0-35-generic

To simplify this you can create an alias or set a keyboard shortcut in the settings. Read man bash/HISTORY EXPANSION and man history to learn more about history expansion.

  • @alhelal Added. :) – dessert May 26 '18 at 11:08
  • This works for some easy cases, but as soon as input/output redirection, command substitution or similar things (i.e. anything the shell interprets) come into play it won't copy the literal command to clipboard. – Lienhart Woitok May 26 '18 at 11:23
  • @LienhartWoitok You’re totally right, luckily there’s the modifier q to [q]uote the substituted words, escaping further substitutions – I edited my answer. Thanks a lot for this helpful remark! – dessert May 26 '18 at 11:58

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.