iTerm2 Mouseless Copy

3D printed mouse by Creative Tools is licensed under CC BY

Where possible, I try to avoid using the mouse. I heavily use Vim while editing which allows for keyboard navigation. I have taken such a liking to the Vim keyboard-bindings that I even use Vimium (a Google Chrome extension) for keyboard-driven navigation in my browser (for as much as I can). In addition, I use BetterSnapTool for MacOS, which allows me to move/resize windows around solely from my keyboard.

One optimization within my terminal that I have been avoiding is tmux – it is unnecessarily complicated for what I need. For the most part, I am able to use iTerm2 to open tabs, split panes and navigate around. To be honest, I don’t really need the session keeping functionality that tmux or screen provided. The big gain I was missing from tmux was the famed copy mode.

A quick snippet from iTerm2’s documentation of highlights for text selection:

  • You can use the mouse.
  • You can use the find feature’s “mouseless copy” feature.
  • You can use Copy Mode.

The first one we’re not interested in as it’s the standard way to select and copy text. We will cover others two in the sections that follow.

Find and Copy

So one approach to copying anything within the iTerm2’s session is to use the default search. It is an interesting approach, to say the least:

Search copy

Essentially, you initiate the search with cmd+f and you can use the enter and tab to move your selection around and to control how much of the text you want in your selection. It works in a pinch, but if you mess up the amount of text in your selection, you basically have to restart the process. In addition, I found the shift+tab command cycled the selected search result, leading to some confusion.

To be honest, I don’t use this search copying approach very often. I find it difficult to get right, as you cannot really afford any mistakes. The following is a summary of this approach:

  • Searching for some text using cmd+f.
  • Use enter to move to the next search result.
  • Use shift+enter to move to the previous search result.
  • Navigate until you are on the desired location.
  • Use tab to expand your search to the next word.
  • Use shift+tab to expand your search to the previous word.
    • Although it moves to the previous search result if one exists.
  • When your search term is selected, use cmd+c to copy the selection.
  • Use esc to exit search, and now you have the selection in your clipboard.

Copy Mode

This iTerm2 mode attempts to emulate as much of the tmux copy mode as possible, allowing you to make text selections using the keyboard. It is a mode very much like Vim’s insert and normal modes. It is important to note that the session within the pane will stop updating when you enter copy mode.

Copy mode

I highly recommend reading the documentation on iTerm2’s copy mode as it completely covers the keyboard shortcuts and features. Copy mode, in my opinion, is the superior of the two approaches for mouseless copying. I highly recommend giving it a shot the next time you reach for your mouse. The following is a quick summary of copy mode:

  • Enter copy mode with cmd+shift+c.
  • Basic Vim keybinding, many keystrokes can active different actions.
    • v to select by character.
    • shift+v to select by line.
    • ctrl+v for rectangular selection.
    • ctrl+space to stop selecting.
    • y to yank/copy the selection (also exits copy mode).
    • q to exit copy mode.
  • Can chain off of iTerm2’s search feature.