You could try to filter edit-and-execute-command() (manual) through a suitable $EDITOR or $VISUAL:
$ touch foo.sh
$ open foo.sh
Set foo.sh content to the following, and save:
#!/usr/bin/env bash
# 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
exit 0
Then make executable and set to visual:
$ chmod +x foo.sh
$ export VISUAL=/Users/danielbeck/foo.sh
On a bash prompt (or any readline powered prompt I think) you can copy the current command input by pressing Ctrl-X Ctrl-E to invoke edit-and-execute-command().
Edit your ~/.inputrc file to change the hotkey for that. Edit your .bash_profile to keep this as your $EDITOR/$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 zsh, not bash.
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 cut.
Output of this program is your current command line. Really pointless exercise:
~ $ osascript Library/Scripts/get_line.scpt
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.
edit-and-execute-command. It should be able to accomplish both, with a suitable minimal script asEDITOR. – Daniel Beck Jan 8 '11 at 14:13