To copy a line of text in the macOS Terminal application, you must use the keyboard shortcut Control-U to delete (kill) the current line into the system clipboard and then Control-Y to paste it back. This sequence effectively copies the content by removing it from the command prompt and immediately restoring it. This method relies on standard Emacs-style key bindings that are active by default in the Zsh shell used by modern versions of macOS.
Understanding Terminal Line Editing
The Terminal app on macOS uses an interactive command-line interface that processes input through a readline library. Unlike graphical applications where Command-C copies selected text, the terminal treats keyboard input as a stream of characters interpreted by specific editing modes. By default, macOS Terminal emulates Emacs bindings for navigation and text manipulation. This means that standard shortcuts like Control-C are reserved for sending interrupt signals to running processes rather than copying text. Consequently, users accustomed to GUI-based copy-paste workflows often find themselves unable to duplicate text directly within the terminal window without using these specific control sequences.
The Control-U and Control-Y Workflow
The most reliable way to copy a line is to treat the "copy" action as a cut-and-paste operation. First, place your cursor at the beginning or anywhere within the line you wish to copy. Press Control-U. In Emacs mode, this command kills the entire line backward from the cursor to the start of the line. The text is not lost; it is stored in the kill ring, which acts as the terminal's internal clipboard. Immediately after pressing Control-U, press Control-Y. This command yanks, or pastes, the most recently killed text back into the buffer at the current cursor position. You now have two identical lines: the original one that was just deleted and the new one created by the paste action.
# Example scenario
user@mac ~ % echo hello world
# Cursor is at the end of 'world'
# Press Control-U (line disappears)
# Press Control-Y (line reappears)
user@mac ~ % echo hello world
user@mac ~ %
This technique works regardless of whether you are using Bash or Zsh, although Zsh is the default shell on macOS Catalina and later. It is important to note that this only copies the currently typed line. If you need to copy output from previous commands, you cannot use this method because those lines are no longer in the editable buffer.
Configuring Key Bindings
If the Control-U and Control-Y shortcuts do not work as expected, your shell configuration might be overriding the default Emacs bindings. You can verify your current binding mode by checking if other Emacs keys like Control-A (move to start of line) and Control-E (move to end of line) function correctly. To ensure Emacs mode is active, add the following line to your
.zshrc or
.bash_profile file:
bindkey -e
For Bash users, the equivalent command is
set -o emacs. After adding this line, restart your terminal session or run
source ~/.zshrc to apply the changes. If you prefer Vi mode, the shortcuts change entirely, requiring different keys such as Escape followed by yy to copy, but sticking to Emacs mode provides consistency with the Control-U/Y workflow described here.
Alternative Methods for Copying Output
While Control-U/Y is ideal for duplicating the current input, you may occasionally need to copy text that has already been executed and printed to the screen. In these cases, you can select the text with your mouse and use Command-Shift-C to copy the selection to the system clipboard. Alternatively, you can pipe commands to the
pbcopy utility, which writes standard input directly to the macOS clipboard. For example, running
ls | pbcopy will save the directory listing to your clipboard, allowing you to paste it into any other application. This method bypasses the terminal's internal editing buffer entirely and interacts with the OS-level clipboard.
Another useful tip involves the history feature. If you want to copy a previously entered command, press the Up Arrow key to navigate through your command history until you find the desired line. Once displayed at the prompt, you can then apply the Control-U and Control-Y trick to duplicate it. This avoids the need to manually retype complex commands or search through logs.
Troubleshooting Common Issues
Some users report that Control-U deletes the line but Control-Y does not restore it. This usually happens if another command was executed between the kill and yank actions, overwriting the kill ring. Always perform the paste immediately after the cut. Additionally, ensure that you are not in a sub-shell or a program like
less or
vim, where terminal shortcuts behave differently. These programs manage their own input handling and ignore the parent shell's readline bindings.
Frequently Asked Questions
Can I use Command-C to copy text in Terminal?
No, Command-C is reserved for sending an interrupt signal to stop a running process in the terminal. It does not copy text to the clipboard in the same way it does in Finder or TextEdit. To copy selected text visually, you must use Command-Shift-C instead.
Why does Control-U delete my text instead of just copying it?
Terminal line editing uses a "kill ring" model where cutting text stores it in memory. There is no native "copy" command that leaves the original text intact. The Control-U command kills the line, storing it in the buffer, and Control-Y retrieves it. This two-step process is the standard way to duplicate content in this environment.
How do I copy multiple lines at once?
The Control-U command only affects the current line up to the cursor. To copy multiple lines, you would need to repeat the process for each line individually. Alternatively, you can highlight all desired text with your mouse and use Command-Shift-C to copy the entire selection to the system clipboard.
Does this method work in iTerm2?
Yes, iTerm2 also supports Emacs-style key bindings by default. However, iTerm2 allows you to configure custom key bindings more easily. If you prefer a direct copy shortcut, you can map a specific key combination to copy the current line in iTerm2 preferences under Keys > Keyboard Shortcuts.
What if I am using Vi mode in Zsh?
In Vi mode, the shortcuts are completely different. You would typically enter insert mode, type your command, and then use Vi-specific commands to manipulate the buffer. The Control-U/Y sequence will not work unless you switch back to Emacs mode using the
bindkey -e command in your configuration file.