Install and Configure Tmux
Table of Contents
What is Tmux #
Tmux is a “terminal multiplexer”. If you work frequently with headless servers, it can be a very useful tool. It allows you to establish multiple sessions and navigate between them easily. Additionally, it can create multiple panes, allowing you to view multiple sessions in a single window. This YouTube video is a good introduction What is TMUX?. Tmux is an awesome tool and I frequently use it in my development workflow along with other tools like Neovim or Lazygit. I have another post about Neovim here: Using Neovim as IDE with NvChad.
How to Install Tmux #
Installing Tmux is super easy, in Ubuntu/Debian you can do sudo apt-get install tmux
. That’s all!
Configuring Tmux #
The configuration is normally done in the file ~/.tmux.conf
. You can check my configuration in this
repository. The commands explained in this post are based in this configuration. The configuration includes some plugins, for them to be installed we need to install a plugin manager, in this case TPM. We can install TPM with:
git clone https://github.com/tmux-plugins/tpm.git ~/.tmux/plugins/tpm
Now we can enter in Tmux with the command tmux
and then press <prefix> shift+i
. The <prefix>
is ctrl+b
by default, but it is changed to ctrl+a
in my configuration. After that, the plugins would be installed. One of the plugins is related to move easily in between Neovim and Tmux panes, remember that you also have to install that plugin in Neovim. For example, if you are using Lazy as plugin manager in Neovim, you need to add this to your configuration:
-- Navigate in between Neovim and tmux panes
{
"alexghergh/nvim-tmux-navigation",
event = "VeryLazy",
config = function()
require("nvim-tmux-navigation").setup({
disable_when_zoomed = true,
-- defaults to false
keybindings = {
left = "<C-h>",
down = "<C-j>",
up = "<C-k>",
right = "<C-l>",
last_active = "<C-\\>",
next = "<C-Space>",
},
})
end,
}
Development Workflow #
In this section I describe briefly how I use Tmux and Neovim in my development workflow. I normally start creating a new session with tmux new -s <name_of_the_session>
. Then, I normally open some windows with ctrl+a c
and name them with ctrl+a ,
. A minimal workspace could be the following named windows:
- IDE: Editing code with Neovim.
- Git: I normally use Lazygit to manage changes in the git repository.
- Terms: Some terminals to deploy and execute commands
You can also create different panes in a window with ctrl+a |
to split vertically, or ctrl+a -
to split horizontally. You can move between windows with ctrl+a <number_of_the_window>
, and you can move between panes with ctrl+[hjkl]
. You can move in between Neovim and tmux panes with the same command thanks to nvim-tmux-navigation
plugin.
You can save sessions thanks to tmux-resurrect
plugin. tmux-continuum
plugin saves your session automatically, but I prefer to do it manually with ctrl+a ctrl+s
to save the session. To restore the session, open Tmux and press ctrl+a ctrl+r
. If you want to do some actions outside of tmux, you can detach with ctrl+a d
. You can attach again to a session with tmux a -t <name_of_the_session>
.
Remember that to see all the available commands you can press ctrl+a ?
.
Another useful functionality is the vim-like copy functionality. You can press ctrl+a [
, to move around the output of a pane, then go into select mode pressing v
, and copying the selected output with y
. To stop copying mode press ctrl+c
or <Enter>
.
Possible Errors #
Theme error #
Colors may not be shown fine, including the bar at the bottom or even when opening Neovim. Using tmux -2
seems to solve this. You can create an alias in ~/.bashrc
with alias tmux='tmux -2'
.
Another thing that solved the problem instead of that alias is to configure the following in .tmux.conf
:
set -g default-terminal 'tmux-256color'
set -as terminal-features ',xterm*:RGB'
This is already added in my configuration, but better to write it here in case someone faces the same issue. The solution is based on this Reddit comment.
Copy to clipboard form SSH and Tmux #
I am using MobaXterm in Windows 10 and connecting to a remote SSH server. In the server, I have installed Tmux and Neovim. I noticed that when I use y
to copy text in Neovim, I get it in my Windows machine clipboard. But the same does not happen with Tmux. In order to achieve this you needed to install xclip
so Neovim detects it as clipboard mechanism. However, Tmux does not do this automatically, so you need to configure that every time you press y
you want that to be sent to xclip
, so it can be sent to the local machine. Add the following lines to your .tmux.conf
.
# bind-key -T copy-mode-vi 'y' send -X copy-selection # copy text with "y"
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
You may also want to check tmux-yank. This could facilitate the clipboard functionality. However, I still prefer to add that one line to the configuration.