You could try to filter
edit-and-execute-command() (manual) through a suitable
$ touch foo.sh
$ open foo.sh
foo.sh content to the following, and save:
# copy current command input to clipboard -- requires local session
cat $1 | pbcopy
# clear the prompt, so no command gets executed when closing this script
# comment out the following line to execute the command you entered instead
echo -n "" > $1
Then make executable and set to visual:
$ chmod +x foo.sh
$ export VISUAL=/Users/danielbeck/foo.sh
bash prompt (or any
readline powered prompt I think) you can copy the current command input by pressing
Ctrl-X Ctrl-E to invoke
~/.inputrc file to change the hotkey for that. Edit your
.bash_profile to keep this as your
$VISUAL -- drawback is obvious, you'll never have a proper
$EDITOR again, unfortunately
Alternatively, you could try replicating what this user is doing, but he's using
The Terminal itself is unaware what part of the current line is your prompt, and what part is your input. So accessing it's lines e.g. through AppleScript wouldn't work in general.
You can, however, read the complete output of the current tab and get its last line, and, through filtering, try to remove the prompt part of it.
My prompt ends with
$, so the following command works:
tell application "Terminal" tell selected tab of window 1 to do shell script "echo '" & history & "' | sed '/^$/d' | tail -n1 | cut -d$ -f2-"
It will get the current window's current tab's
history property, and filter it through
sed to remove all empty lines, then get the last line using
tail, and then remove everything up to and including the first
$ of the line (i.e. my prompt) using
Output of this program is your current command line. Really pointless exercise:
~ $ osascript Library/Scripts/get_line.scpt
To copy it to your clipboard at the end, change to the following:
tell application "Terminal" to tell selected tab of window 1 to do shell script "echo '" & history & "' | sed '/^$/d' | tail -n1 | cut -d$ -f2- | pbcopy"
You can use Automator to create a service that receives no input in application Terminal and performs a single Run AppleScript Script action consisting of the above code. Save, and optionally assign a keyboard shortcut in System Preferences » Keyboard » Keyboard Shortcuts » Services.