DebugPointer
Published on

Tmux Conf - keyboard bindings

Tmux Conf - keyboard bindings

Tmux is one of the most useful tool when it comes to using a linux terminal.

Tmux is a terminal multiplexer, it allows a user to access multiple terminals (or windows), each running a separate program, while providing a single screen to work on. Tmux is useful for running more than one command-line program at the same time, increasing productivity and the way you work.

Let's consider the default prefix for tmux, which is Ctrl-B and let's go over the commands below using the same. In case you want to change the prefix of tmux you can do it by creating your own prefix or shortcut.

Let's look at how we can customise the ~/.tmux.conf to have a user-friendly way of using it. I'm a fan of peterforgacs's config. We will use the same config, eventually I'll update with changes to the config as I customise my tmux config file.

Tmux has default key bindings, which gives you complete tmux cheatsheet of default keyboard shortcuts.

Install tmux

Tmux is available as a package in most linux distributions and on mac via brew. You can install it by running the following command depending on your Operating System-

# Debian, Ubuntu
$ sudo apt-get install tmux

# RHEL/CentOS/Fedora
$ sudo yum install tmux

# MacOS
$ brew install tmux

Incase you are using a different package manager, in most cases the name of the package is tmux, so you can go ahead and install it in the Distro or OS that you are currently using.

tmux.conf - key bindings

Plugins

Install the following plugins that are useful to make your tmux experience even better-

tmux.conf

You can copy paste the following inside your ~/.tmux.conf using vi or emacs or any editor of your choice and then reload the tmux conf by running tmux source-file ~/.tmux.conf

# scrollback size
set -g history-limit 10000

################################
# Keybinds
###############################

# Changing ctrl b to ctrl a as the command button
unbind C-b
set -g prefix C-a

# set first window to index 1 (not 0) to map more to the keyboard layout
set -g base-index 1
set -g pane-base-index 1

# pass through xterm keys
set -g xterm-keys on

# Keep your finger on ctrl, or don't, same result
bind-key C-d detach-client
bind-key C-p paste-buffer

# Redraw the client (if interrupted by wall, etc)
bind R refresh-client

# reload tmux config with ctrl + a + r
 unbind r
 bind r \
     source-file ~/.tmux.conf \;\
         display 'Reloaded tmux config.'

# Vertical splits with g or C-g
unbind g
unbind C-g
bind-key g split-window -h
bind-key C-g split-window -h

# Horizontal splits with v or C-h
unbind h
unbind C-h
bind-key h split-window
bind-key C-h split-window


# Using the mouse to switch panes
set -g mouse-utf8 on
set -g mouse on

# Ctrl - t or t new window
unbind t
unbind C-t
bind-key t new-window
bind-key C-t new-window

# Ctrl - w or w to kill panes
unbind w
unbind C-w
bind-key w kill-pane
bind-key C-w kill-pane

# C + control q to kill session
unbind q
unbind C-q
bind-key q kill-session
bind-key C-q kill-session

# Switching panes with alt
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# Ctrl + a + n : New session
unbind n
unbind C-n
bind-key n new-session
bind-key C-n new-session

# Ctrl + a + Pagedown : Next window
#unbind Pagedown
unbind Pagedown
bind-key Pagedown next-window

# Ctrl + a + Pagup : Previous window
#unbind Pageup
unbind Pageup
bind-key Pageup previous-window

# Visual Activity Monitoring between windows
setw -g monitor-activity on
set -g visual-activity on

# Show tmux positions in titles
set -g set-titles on

# Set up a basic panel structure for command + ctr D
bind D source-file ~/.tmux/dev

# Attach to a session if runs otherwise create a new one
new-session -n $HOST

# Copy from tmux to system clipboard
# Needs xclip -> sudo apt install xclip
bind -t vi-copy y copy-pipe "xclip -sel clip -i"

#####################################
# Plugins
#https://github.com/tmux-plugins/tpm
####################################

#####################################
# tpm plugin manager
# https://github.com/tmux-plugins/tpm
#####################################
set -g @plugin 'tmux-plugins/tpm'

#####################################
# tmux-sensible - basline settings that get overwritten from .tmux.conf
# https://github.com/tmux-plugins/tmux-sensible
######################################
#set -g @plugin 'tmux-plugins/tmux-sensible'

#####################################
# tmux-resurrect - save and reload sessions and windows after a restart
# https://github.com/tmux-plugins/tmux-resurrect
# Default keybinds : save - command + c + s && restore command + c + r
######################################
set -g @plugin 'tmux-plugins/tmux-resurrect'
### tmux-resurrect extra settings ###
# keep vim sessions after reboot
set -g @resurrect-strategy-vim 'session'
# keep pane contents
set -g @resurrect-capture-pane-contents 'on'
# restore bash history
set -g @resurrect-save-bash-history 'on'
### /tmux-resurrect extra settings ###

######################################
# tmux-continuum - automatically save and restore tmux sessions
# https://github.com/tmux-plugins/tmux-continuum
#####################################
set -g @plugin 'tmux-plugins/tmux-continuum'
# Restore last saved enviroment
set -g @continuum-restore 'on'
# Set autosave interval
set -g @continuum-save-interval '5'
# Show continiuum state in statusbar
#set -g status-right 'Continuum status: #{continuum_status}'
# Starting tmux with system https://github.com/tmux-plugins/tmux-continuum/blob/master/docs/automatic_start.md

######################################
# tmux-yank - advanced copy mode
# https://github.com/tmux-plugins/tmux-yank
#####################################
set -g @plugin 'tmux-plugins/tmux-yank'

# initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

As mentioned above, after you save the above config file, make sure you run the tmux source command to reload the configuration-

tmux source-file ~/.tmux.conf

Keyboard shortcuts or key bindings

Now that we have updated the shortcuts in the above ~/.tmux.conf, let's look at how our new keyboard shortcuts looks like for tmux-

Keybinding Command
Ctrl + a Prefix
Prefix + t New window
Prefix + w Kill Pane
Prefix + q Kill session
Prefix + r Reload tmux config
Prefix + z Zoom to pane
Prefix + $ Rename session
Prefix + , Rename window
Prefix + v Split vertically
Prefix + h Split horizontally
Prefix + ? List keyboard shortcuts
Prefix + : Command Prompt
Prefix + s List Sessions
Prefix + Ctrl + r Reload session
Prefix + Ctrl + s Save Session
Prefix + Shift + i Install Plugins

Mouse mode

Now we have added ability to copy-paste using the mouse mode.

To Copy the content-

  • Enter scroll mode with the mouse wheel.
  • Select the text and press y.
  • This copies the text into the clipboard.
  • Exit scroll mode with escape.

Advanced copy-

  • Enter scroll mode with the mouse wheel.
  • Select the text and press Y.
  • This copies the text into the terminal buffer and the clipboard.
  • Exit scroll mode with escape.

I hope this article to configure your custom ~/.tmux.conf was useful. You can also check out the complete tmux cheatsheet of keyboard shortcuts and mouse shortcuts.