How can I select a line in the macOS terminal with keyboard shortcuts?

8,510

Emacs-style shortcuts

To delete all you've written, you can press Ctrlu.

If you are at the start of the line, press Ctrlk.

To copy text, you can use the kill ring:

  • Type something in your terminal
  • Press Ctrlu to delete everything (or Ctrlk)
  • Press Ctrly to paste (“yank”) what you've just deleted

These are some of the Emacs keybindings that macOS implements for (most) text fields. There are plenty of those, and it's very useful to learn them, since with the additional Ctrl key, you have access to more navigational shortcuts.

Note that the copy-paste operation described above does not use the system clipboard. To select the entire line, you can triple-click it. But this will also include any prompt prefix (e.g., $) that you might have configured.

Alternative: iTerm2

I'd ditch Terminal.app and use iTerm2 instead. Apart from many other features that Terminal.app does not have, it offers Copy Mode (CmdC), which allows you to use your keyboard to make selections.

For example, you can:

  • Press CmdC
  • Press v to select and the entire line
  • Press Ctrlk or y to copy the selection
  • Press Esc to leave copy mode

Now your native macOS clipboard will have the contents of what you just copied.

Share:
8,510

Related videos on Youtube

Trent
Author by

Trent

Updated on September 18, 2022

Comments

  • Trent
    Trent almost 2 years

    I've recently just switched over to macOS from Windows and I previously used PowerShell. In PowerShell, I could type out a command and hit ctrl/command a and it would select whatever I wrote, so that I could copy/delete it.

    When I press command a in the macOS terminal, it selects the whole window and doesn't let me copy/delete the line I was writing.

    Anybody know how to just select what's written and copy/delete it?