sebastiano.tronto.net

Source files and build scripts for my personal website
git clone https://git.tronto.net/sebastiano.tronto.net
Download | Log | Files | Refs | README

tmux-clipboard.md (2516B)


      1 # tmux trick #2: copy to clipboard
      2 
      3 *This post is part of a [series](../../series)*
      4 
      5 Recently, for reasons that I may explain in a future blog post, I went
      6 through some old configuration files of mine, and I found something in
      7 my `.tmux.conf` that I thought would be worth a second post in this
      8 series - only 2 years after the first episode, not bad!
      9 
     10 ## Copy mode
     11 
     12 [tmux](https://en.wikipedia.org/wiki/Tmux) has a feature called *copy mode*
     13 that allows one to visually select and copy text, all via your keyboard.
     14 By default you can enter copy mode by pressing `C-b [` (`Ctrl+B` followed
     15 by `[`); from there you can navigate the text that is currently on your
     16 terminal with arrow keys and [Emacs](https://en.wikipedia.org/wiki/Emacs)-style
     17 key bindings - or with hjkl and other
     18 [Vim](https://en.wikipedia.org/wiki/Vim_(text_editor))-style bindings if
     19 you have a `VISUAL` or `EDITOR` [environment
     20 variable](https://en.wikipedia.org/wiki/Environment_variable) set to `vi`.
     21 
     22 You can then start selecting text by pressing `Space`, and confirm the
     23 selection by pressing `Enter`. The selection will then be copied to
     24 an internal tmux buffer, and you can paste it by pressing `C-b ]`.
     25 
     26 Actually, tmux offers multiple copy-buffers, but honestly I have never
     27 used this feature. You can read more about this in its [manual
     28 page](https://man.openbsd.org/tmux).
     29 
     30 ## Copy to clipboard
     31 
     32 By default, tmux will copy the selection to its internal buffer, but you
     33 may want to paste that text somewhere outside of tmux - maybe in a chat
     34 application or in a web browser URL bar. And here is the trick: you can
     35 actually tell tmux to [pipe](https://en.wikipedia.org/wiki/Pipeline_(Unix))
     36 the selection to a custom command. For example, if you have this in your
     37 `.tmux.conf`:
     38 
     39 ```
     40 set -s copy-command "xsel -ib"
     41 ```
     42 
     43 tmux will not only copy the selection to its internal buffer, but also
     44 send it to the `xsel -ib` command. In case you did not know,
     45 [`xsel`](https://linux.die.net/man/1/xsel)
     46 is a command that copies its standard input to the X session
     47 [clipboard](https://en.wikipedia.org/wiki/Clipboard_(computing));
     48 this way you will be able to paste the copied text into any other
     49 graphical application with the usual `Ctrl+V`. Neat!
     50 
     51 ## tmux show-buffer
     52 
     53 Here one last trick: with the `tmux show-buffer` command you can print the
     54 current tmux selection to standard output. You can use this in shell scripts,
     55 for example, or in more complex tmux key bindings in your configuration file.
     56 And that's a teaser for the next tmux trick :)