Tmux with Powerline on Ubuntu - tutorial for beginners

avatar

Borislav Hadzhiev

Sun Feb 28 20213 min read

banner

Photo by Thomas Grams

Tmux installation, configuration and the basics of Tmux keyboard shortcuts

tmux #

Tmux runs your window in a process, that isn't tied to the session. Which means that if you ssh to a server, launch tmux, execute commands and the ssh connection crashes, the process doesn't end, you can just reconnect and continue working in the same session.

Installation and Initial config #

Let's start by installing tmux and powerline:

sudo apt update

sudo apt install tmux

sudo apt install powerline -y

# install the powerline fonts
git clone https://github.com/powerline/fonts.git --depth=1
cd fonts
./install.sh
cd ..
rm -rf fonts

# edit the .tmux.conf file

# add the powerline status bar to tmux
# edit the tmux config file and paste the below lines in it
vim ~/.tmux.conf

# enable the powerline status bar
run-shell 'powerline-config tmux setup'

# DON'T paste this line in, if you DON'T want vim keybindings
# Set tmux mode to vi (default is emac)
set-window-option -g mode-keys vi

Some default tmux shortcuts #

The default prefix for tmux is ctrl + b. Most of the commands we type will start with the prefix ctrl + b and be followed by another letter or symbol, after the ctrl + b. When you see % it translates to shift+5, " to shift+', etc.

create a new session, specifying a name:

tmux new -s mySessionName

create a new window in the session, places you in window 1. The windows are 0 index based. Notice the * next to the terminal name - it indicates the active window.

ctrl + b + c

create pane to the right:

ctrl + b + %

create pane to the bottom:

ctrl + b + "

navigate between panes, notice that the active pane has a green outline around it:

ctrl + b + `left-arrow` or `right-arrow` or `top-arrow` or `bottom-arrow`

toggle focus a pane, hiding the other panes - simply press it again to show/hide the other panes:

ctrl + b + z

move a pane to the right / left:

ctrl + b + }  # move pane to right
ctrl + b + {  # move pane to left

change layout between panes, click it until you get the desired layout:

ctrl + b + `space`

resize a pane, you have to HOLD ctrl while pressing the arrow keys:

#hold ctrl
ctrl + b + `left-arrow` or `right-arrow` or `top-arrow` or `bottom-arrow`

close a pane, alternatively you can type exit and press Enter:

ctrl + d

switch to window X, in our case switch to window 0. Notice the * symbol next to the active window name in the bottom bar:

# first create a new window
ctrl + b + c

# then switch between them
ctrl + b + 0

rename a window, after pressing the key combination delete the old window name and type in a new one:

ctrl + b + ,

close a window, alternatively you can type exit and press Enter:

ctrl + d

detach a session - brings you back to your default terminal. Saves the tmux terminal at the state from when you detached. These sessions are preserved until the system is rebooted:

ctrl + b + d

list tmux detached sessions:

tmux ls

attach to a previously detached session - notice that 0 is the default session name, it might be different if you rename your session. This command brings you back to the state from when you detached the terminal:

tmux attach -t 0

rename a session - notice that 0 is the default session name for session at index 0. **You can also tab for command-name, flag, or session name autocompletion.

tmux rename-session -t 0 newSessionName

delete a session - alternatively you can press ctrl + d inside an empty session and it kill it.

tmux kill-session -t sessionName

End #

These are some of the base tmux commands. It helps you manage terminal state, split terminal panes, create sessions inside of terminal windows.

You can detach from sessions and later on attach continuing where you left off. Note that a system reboot will kill your sessions. However when you work on a server, it's more common for your connection to the server to be dropped for some reason (like network connectivity issues), than for the server to completely crash and reboot.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee