5

!! | pbcopy would also run the commands again and couldn't be used with interactive commands.

This relies on the prompt always being $ :

tell application "Terminal" to tell window 1 to history
do shell script "/bin/echo " & quoted form of result & ¬
    " | ruby -e 'puts $<.read.split(/^\\$ .*?$/)[-2][1..-1]'"
set the clipboard to result

Does anyone know any better options?

  • 3
    And !! not good for command with side effects – Rich Homolka Nov 21 '11 at 0:41
  • 2
    Can you add a real example of what you're trying to accomplish with this? Fill in the blanks "I want to copy the output of the last command, because the last command was ________ and I want to paste it into _______" – Doug Harris Nov 21 '11 at 18:40
  • @DougHarris I'd just want to assign a keyboard shortcut to a general purpose script for copying the previous output. That fill in the blanks thing is insanely patronizing BTW. – Lri Nov 21 '11 at 19:36
  • You might consider it patronising, but it's often the best way to succinctly understand what a user wants. – Chris Down Nov 22 '11 at 16:23
  • You could make exec script $( date +%Y%m%d-%H%M%S ) your Terminal startup command to log everything to a dated log file. One unfortunate side effect is that script is a lousy shell (e.g. always 80 wide) in my limited testing. – Daniel Beck Nov 28 '11 at 20:06
3

If you are using at least el Capitan you can use Cmd-shift-A to select the output of the last command and Cmd-C to copy it. Unfortunately this doesn't work for previous versions.

  • Nice! Is there an iTerm2 equivalent? – Juuso Ohtonen Aug 1 '17 at 10:17
  • Sorry I don't use iterm – Enrico Aug 9 '17 at 16: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.