4

I am using Terminal or iTerm (iTerm2). Let's say I've typed in a command but haven't hit enter yet. Or maybe I used the up arrow to navigate through my command history. How do I copy the current command to the MacOS clipboard? But I also don't want it to include the prompt. Is there a keyboard short cut to do this?

For example, let's say the terminal is showing this:

myhostname:Documents kevuser$ mkdir cool_directory_name

What is the keyboard shortcut to copy mkdir cool_directory_name to the Mac OS clipboard? Can I add that functionality somehow? It feels like every time I try to copy the whole line it includes the prompt part of the terminal, myhostname:Documents kevuser$, which I don't want to be copied and pasted.

2
  • Notice that the terminal does not know anything about prompts or commands; that is the domain of the shell (usually bash or zsh). Similarly, the shell, being a general Unix program, not a macOS-specific program, knows nothing about the macOS clipboard (but has a clipboard of its own!). – Ture Pålsson Apr 28 '20 at 6:19
  • 1
7

Just use the command

 echo !! | pbcopy

The double exclamation points is “last command”. It’s then piped to the pbcopy command which puts it in your clipboard.

There’s no shortcut, but you can make an alias in your ~/.bash_profile or in your ~/.zprofile files, depending on your shell, that will execute the command with as easy to remember name

alias cplastcmd=‘echo !! | pbcopy’

If you want to copy text that you’ve entered but not executed you can use the following

  • ^ ControlU : Cut the line before the cursor and put it in the clipboard.
  • ^ ControlK : Cut the line after the cursor and put it in the clipboard. Use with ^ ControlA to move to the beginning of the line.
  • ^ ControlY : Paste the last command from the clipboard

Note: The standard copy/paste keyboard shortcuts ⌘ Command or ^ Control C and V don’t work in Terminal.

Finally, if you have a mouse with a third/middle button, you can simply select the text and press the middle button to copy and paste the selected text.

3
  • 1
    This requires executing the command first; the question is about capturing a command that hasn't been executed yet. Also, this won't work right if there are quotes, escapes, wildcards, command substitutions, redirects, etc in the command, because it'll print the command after all shell parsing, redirects, etc happen. – Gordon Davisson Apr 28 '20 at 1:17
  • If the command hasn’t been executed, it wouldn’t be in the history to begin with. – Allan Apr 28 '20 at 1:23
  • Why is it so easy on my machine? I must have something set that makes the simple copy/paste work. See my answer below. it really works that way on my machine! – Natsfan Apr 28 '20 at 2:02
0

On my iMac running High Sierra, I can use the mouse cursor to select any part of a line I want and copy it. With a terminal window open, if I move my mouse cursor into the terminal window and click, the mouse cursor turns into the cross-beam cursor. I can then move it around the terminal window and select a part of any line I choose.

0

Aside from Allan's answer using pbcopy to capture the last executed command (which doesn't work for non-executed commands or those executed some time ago), the only way to copy the current line in Terminal is by highlighting and copying.

One option to make this easier is to add some extra whitespace to your prompt. This will not help you grab ONLY the command you want, because you may catch extra whitespace (which often won't matter), but it can make it easier to avoid grabbing extra text to the prompt.

Your prompt may be set in a number of different places depending on the setup and shell you're using (~/.bashrc, ~/.zshrc, etc). Somewhere it will appear looking like:

PS1="[\u@\h \W]\\$ "

Where PS1= is setting the prompt. You can modify the trailing space to add extra spaces.

Sorry there's not a better answer.

Alternatively, sometimes I double-click the first word/command of the line, then shift-click the end of the line, and that will gaurantee at least you won't gobble up any part of the prompt.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .