Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a hotkey to select all of the input text in the input line in OS X's Terminal.app?

share|improve this question
    
Do you want to select with the intent to copy or with the intent to replace? –  NReilingh Jan 8 '11 at 2:59
    
all of the above –  erikvold Jan 8 '11 at 3:20
    
If you want to copy and replace, I really suggest you look into edit-and-execute-command. It should be able to accomplish both, with a suitable minimal script as EDITOR. –  Daniel Beck Jan 8 '11 at 14:13
2  
This is going to be a bit fishy, since the command line really has no concept of selected text, nor would mouse-selected text be replaced when if you continued typing. The answer to your question is "no", but Daniel's suggestions might help you hack together a substitute. Some other things that might help: control-a and control-e can be used to jump to the beginning and end of whatever you've typed at the prompt, and you could design your own keystroke hotkeys for terminal using Automator, Services, and AppleScript GUI scripting. –  NReilingh Jan 8 '11 at 19:06
    
@NReilingh Pinging you; as you thought about my suggestions you might be interested in what I was able to come up with. –  Daniel Beck Apr 17 '11 at 9:14

1 Answer 1

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.

share|improve this answer
    
Have you (or anyone else) figured out any better way to get rid of the prompt string in the last examples? –  Lri Oct 9 '11 at 14:14
    
@Lri You could, in a separate bash process, echo $PS1 after loading the profile files to create a good template that is them removed from output. Unfortunately, stuff like $PWD and date will break this method. Alternatively, one could backup $PS1 into another variable temporarily, and restore it after executing the command. –  Daniel Beck Oct 9 '11 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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