Go back to the main page

Increased Developer Productivity with Tmux, Part 8: Copy mode.



Tmux's copy mode

In Part 7 I finished up showing how using Elinks allows you to reduce context switching. In this episode I go over getting proficient within copy mode. Copy mode is a state where you can "freeze" a pane or window with the intention of reading the output. In this mode you can scroll, search, and copy the text of a pane or window. Subduing copy mode is an absolute must if you are going to maximize your productivity with Tmux.

Subduing copy mode is an absolute must if you are going to maximize your productivity with Tmux.

  1. To start I open my Tmux conf file at ~/.tmux.conf. I have vi-bindings enabled in copy mode.
  2. If you are a Vim user these binding are a must, allowing you move around efficiently in copy mode. There are Emacs bindings as well.
    This section is at the 1:05 mark in the screencast.
  3. I switch over to a Tmux window within a Rails console, in which I will demonstrate a simple copy and paste.
  4. To enter copy mode I issue a <prefix> + [
  5. The gold highlight in the upper right corner of a window or pane signifies that copy mode is on.
  6. Using the motion keys I scroll up to the capital "T" and hit the space key to start a highlighted block (i.e. "begin selection"). I use movement keys to get to the end of the command and hit "Enter". That will save the highlighted text to the Tmux clipboard.
  7. Note: Enter saves to the Tmux clipboard not the OS system clipboard.
  8. To paste from the Tmux clipboard type <prefix> + ]
  9. Pasting from Tmux clipboard.
    Minimul says —

    Tmux operates in a client server design, therefore, the Tmux clipboard can be centrally accessed from any of the client sessions. That said, this ability only goes so far. It is vital that there is a way to copy to the system or OS clipboard. Keep reading and I will show you how for OSX.

  10. Most times we are not concerned with copying and just want to see the output within a pane or window.
  11. To be sure to capture all of the output from say a stack trace or XML dump, set the scroll history high in the config file.
  12. I recommend 30000 as it has served me well.
    The topics of moving around and searching in copy mode are at the 3:31 mark.
  13. As previously mentioned, I can use familiar Vim bindings to navigate around copy-mode. Here are some that I use regularly.
    • Control-U — pages up
    • Control-D — pages up
    • ^ — goes to the beginning of a line
    • $ — goes to the end of a line
    • g — goes to the beginning of scroll history
    • G — goes to the end of scroll history
    • / — search down
    • ? — search up
    Being able to search in copy mode is massively useful.
    Minimul says —

    Let's get back to the topic of copying and pasting to the system clipboard. For OSX I want to recommend this thoughtbot article, which leverages the Tmux 1.8 copy-pipe feature. Copying to the system clipbord starts at 7:01 mark in the screencast.

  14. I switch to my Elinks window and hit the v key to start a selection.
  15. I then select y key to "yank" the text into the system clipboard.
      # This is what the "y" key is bound to in my .tmux.conf file
    # Copy to OSX clipboard
    bind-key y run-shell "tmux save-buffer - | reattach-to-user-namespace pbcopy"
  16. To prove that the selection is in the system clipboard and not just the Tmux clipboard. I paste the selection into a MacVim, which is running outside of Tmux.
  17. Presto! Tmux and the system clipboard are integrated.
  18. Lastly, here is a Vim leader mapping for pasting from the system clipboard easily.
  19. The leader saves you from having to run through 3 or 4 keystrokes each time you paste from the system clipboard into Vim.

That concludes installment 8. Tmux copy mode

Copy mode is a mode you are going to be in very often, therefore, make it a point to get proficient within it. Memorize the keys for getting into copy mode as well as the keys for movement. Ideally, you are using Emacs or Vim from within Tmux and can use their bindings for copy mode movement. If not try to get some bindings that make moving around in copy mode painless. Finally, remember that Control-C gets you out of copy mode and is another important key combination to muscle-memorize.

Next in part 9, which will be the final in the series, I instruct on using Tmux within Terminal.app. While you are waiting for the article check out the screencast that is already cooked.

If you like this article and screencast go to the dedicated page for this series at http://minimul.com/teaches/tmux.

  • Pushed on 07/14/2014 by Christian