Is there a hotkey for selecting all the text I have typed in a unix command?
In some shell, such as the terminal on Mac OS X, I want to select all the text that I have entered on the command line. For example, I typed this on the command line and haven't hit enter yet. I want to do everything (like ctrl A or apple A) so that I can cut or copy this command (ctrl X / C or apple X / C).
$ grep -r "test" .
Is there a way to do this without using the mouse and actually selecting the entire command? It would make life easier when I need to copy a unix command to send to someone else.
This is how it works on my OSX laptop.
bash$ echo "this is a command I haven't run yet" # press Ctrl-u here
bash$ # blank line
bash$ pbcopy <<< # now I press Ctrl-y
bash$ pbcopy <<< echo "this is a command I haven't run yet" # press Enter
bash$
From here the command is on the clipboard and I didn't use a mouse or set any keyboard shortcuts. This will work with standard bash, so you don't need to configure MacPorts.
Your terminal might provide something like this. Apps like screen
and tmux
also sometimes provide things like this (although generally for use in a shell session I think, not the system clipboard). There's a problem with layering with just about everything it does.
I don't believe there is a terminal shortcut to select all text. One alternative might be to use the pbcopy command to copy the last command to the system board.
fc -ln -1 | pbcopy
Then you can use Command-V to paste somewhere else on your computer. It's probably not faster than using a mouse, but if you have to do it a lot, you can create an alias.
Not with OS X by default bash. (It's just 3.2).
But if you install bash 4.0+ (I recommend using MacPorts ) you can use the following function:
tie
-x keyseq: command shell
Call a shell command that will be executed whenever keyseq is entered. When a command shell is executed, the shell sets the READLINE_LINE variable to the contents of the Readline line buffer and the READLINE_POINT variable to the current insertion location point. If the executable command changes the READLINE_LINE or READLINE_POINT value, these new values will be reflected in the edit state.
So, you can bind the sequence CTRL- X c(press CTRL-X followed by a simple "c") to pbcopy
with the following command bind
:
bind -x '"\C-xc":pbcopy <<<"$READLINE_LINE"'
Now everything that you entered into bash, after pressing "CTRL-X c" will be copied to the clipboard.
If you are not like the sequence, you can also set sequince in Terminal.app -> Preferences -> Settings -> Keyboard like in the following screeenshot
where I have bound "CTRL-X c" \030c
to CTRL- ALT- LEFT_ARROWso now I have both bindings.
You can use a history file for this. Make your bash write all commands to the history file at once, as described here
Then make a key binding to run the script tail ~/.bash_history -n 1 | pbcopy
. Or you can integrate this script directly into the PROMPT_COMMAND variable as described in the link.
Not tested:
PROMPT_COMMAND="history -a; history -c; history -r; tail ~/.bash_history -n 1 | pbcopy; $PROMPT_COMMAND"