Copy and paste text from a tmux session

Bonnie Eisenman
InstructorBonnie Eisenman
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 years ago

One non-obvious, but extremely useful, feature in tmux is copy-pasting text between panes. This also allows you to scroll back in a tmux sessoin. We'll cover how to use copy and paste in tmux using C-b [ and C-b ], for both emacs- and vim-style keybindings.

[00:00] You may have noticed while using tmux that your default method of scrolling doesn't work. In order to scroll back, we're going to type Ctrl-a and the left bracket key. Remember that I've remapped my prefix key to Ctrl-a. If you haven't done so, you'll still want to use Ctrl-b.

[00:15] You'll see here that this adds this gold-colored little bit of information display to the top which lets us know that we're in copy mode. Now, if I use my arrow keys, I can scroll back up and read the previous output from within this tmux pane. To leave copy mode, you press q, no prefix key. That will bring you right back to your current prompt.

[00:37] If you're on OS X, your default copy and paste should work. For example, if I highlight Vanilla JS here, push Command-C, and type Ctrl-a and the right arrow, I should be able to paste that back in with Command-V in my other pane.

[00:53] However, especially if you're working on a remote machine or something that's configured a little bit differently, you want to be able to use tmux's native copy and paste capabilities. There are two different sets of key bindings for using copy and paste in tmux. There's Emacs-style key bindings and Vim-style key bindings.

[01:09] To start with, we'll look at the Emacs key bindings. I'm going to type the prefix key Ctrl-a and colon, which puts me into command mode. Then I'm going to type set-window-option mode-keys emacs. I'm going to hit Enter.

[01:26] In order to copy paste text, we start by entering copy mode with Ctrl-a and the left square bracket. Then we can use our arrow keys to navigate to something that we want to copy. We set our mark by typing Ctrl-Space. Then I can use my arrow keys to select which bit of text I want to copy. To copy it, I'm going to type Ctrl-w.

[01:51] Depending on your system, this might be Alt-w or Option-w. It depends on your Emacs configuration. You'll notice that this takes me out of copy mode and puts me back at the prompt. Now, if I type the prefix key and the right bracket, you can see that it copied the text I had selected.

[02:11] Next, let's look at copy and paste using Vim key bindings. I'm going to type the prefix key and colon to get into command mode. Then I'm going to type set-window-option mode-keys. Now I'm going to set it to vi instead of Emacs. As before, I enter copy mode with the prefix key and the left bracket. I can navigate around with my arrow keys. I can also navigate with Vim key bindings.

[02:38] To set my mark for where I want to start copying, I press Space. I used standard Vim navigation to select where I want to copy. Let's say I want to copy to there. In order to actually execute the copy, I press Enter to select the copied region. This returns me back to the prompt. Now, if I type the prefix key and the right square bracket, you can see it copied it to my prompt.

[03:03] Remember also that you can use your tmux.conf file to change this default behavior. You can set it up to use Emacs- or Vim-style bindings by default, whichever is more comfortable for you.

egghead
egghead
~ 8 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today