What is tmux?
TMUX is a Terminal MUltipleXer that acts as a windows manager for your terminal. You can open multiple windows and split-views (panes) within one terminal window. Because of this, you can have multiple commands/applications running side-to-side without the need to open multiple terminals.
Installing tmux
Installing tmux is very straightforward. Type the following command on your OS Terminal app:
- On Ubuntu and Debian
sudo apt-get install tmux
- On CentOS and Fedora
sudo yum install tmux
- On macOS
brew install tmux
Customization
The default tmux configuration is good enough to get started, but we can customize many things to fit our preferences. Fortunately, customizing tmux is as easy as writing on a text file. This article will be about how I have configured mine and how you can configure yours to your preferences.
First of all, create a configuration file .tmux.conf
on your home directory if it's not already there.
Open the file you just created with any editor you are comfortable with. I will be using nano
for simplicity.
Now we can customize tmux behaviour to our preference. Just add, remove, or modify the following lines according to your preference.
Setting Index to Start at 1
By default, the windows/panes number starts at index 0. So you need to set their indexes to 1.
Changing Prefix Combo
Since you will be pressing the prefix combo a lot, we will change the default combo (Ctrl + b) to something more natural (Ctrl + s). You can assign it to whichever key works best for you. Add the following line on the configuration file you just opened.
Changing Split Commands
By default, the '"' and '%' are the keys for vertical and horizontal split, respectively, which is not very intuitive. We will be changing that to '\' and '-'. Also, split panels open up in a home directory instead of the current directory we are working from by default, so we will also change that.
Change Windows/Panes without Prefix
You will be changing between different windows/panes a lot. So, pressing the prefix combo and other command keys becomes tedious and counterproductive.
After adding the lines above to the config file, you can use Shift + Arrow Keys (←→↑↓) to change windows and Alt + Arrow Keys (←→↑↓) to change panes.
For those who are familiar with vim
, you can also add the following lines to change between panes using vim navigation keys.
Changing Copy/Paste
By default, copying contents from the tmux session to the system clipboard is complicated. So we will be changing it.
For this, you will need to have xclip
installed.
You can install xclip using sudo apt-get install xclip
on Ubuntu/Debian, sudo yum install xclip
on Fedora/CentOS, or brew install xclip
on macOS.
Once xclip is installed, add the following line to the tmux configuration file.
## Capture current tmux buffer and copy it to system clipboard with prefix + 'Ctrl + c'
bind C-c run "tmux save-buffer - | xclip -i -sel clipboard"
## Optional - paste from system clipboard to tmux session with prefix + 'Ctrl + v'
bind C-v run "tmux set-buffer "$(xclip -o -sel clipboard)"; tmux paste-buffer"
For me, those above keys are not very intuitive. So let's modify them to mimic vim
key-bindings.
Now, copying the output from the tmux session to the system clipboard is a lot easier.
First, press 'Prefix Combo' + ESC to enter copy mode. If you see a yellow visual indicator on the top right of your tmux window, you can now move around the output using vim navigation keys.
Now, to highlight the text you want to copy, press Space or 'v', then use your arrow keys or vim navigation keys to select the desired text. Then, at last, "yank" (copy) the highlighted output with 'y'. You can paste using normal shortcuts, but pressing 'p' should also do the work.
Reload tmux Config Without Closing Current Sessions
Add the line below to allow you to press 'Prefix Combo' + r to reload and apply your changes to the tmux config file without closing your current sessions.
Miscellaneous Changes
The code above contains some miscellaneous customization from my tmux config file. Feel free to add these lines to your own config to tinker with them to find something that fits you.
Here's what my tmux looks like currently.
Wondering why my TMUX looks different than yours? Stay tuned for part 2 to find out.