Home My tmux configuration and workflow
Post
Cancel

My tmux configuration and workflow

Follow tmux wiki for more information

Install tmux

1
2
3
4
5
# ubuntu
sudo apt install tmux

# mac
brew install tmux

Tmux configuration file

After tmux is successfully installed, we can customize tmux shortcut keys and other configurations by creating a ~/.tmux.conf file and adding configuration options in this file.

Here is my ~/.tmux.conf file, feel free to copy it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
###########################
#  Configuration
###########################

# use 256 term for pretty colors
# set -g default-terminal "screen-256color" # making tmux slow
set -g default-terminal "xterm-256color"
# Needs this line also to overrides the default color
set-option -ga terminal-overrides ",xterm-256color:Tc"


# Enable mouse support
set -g mouse on

# increase scroll-back history
set -g history-limit 5000

# use vim key bindings
setw -g mode-keys vi

# decrease command delay (increases vim responsiveness)
set -sg escape-time 1

# increase repeat time for repeatable commands
set -g repeat-time 1000

# start window index at 1
set -g base-index 1

# start pane index at 1
setw -g pane-base-index 1

# highlight window when it has new activity
setw -g monitor-activity on
set -g visual-activity on

# re-number windows when one is closed
set -g renumber-windows on

# Fix nightfly color scheme to display correctly inside tmux
set -ga terminal-overrides ',xterm-256color:Tc'

# enable pbcopy and pbpaste
# https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard/blob/master/README.md
# set-option -g default-command "reattach-to-user-namespace -l zsh"

###########################
#  Key Bindings
###########################

# tmux prefix
set -g prefix C-b

# paste
unbind C-p
bind C-p paste-buffer

# window splitting
unbind %
bind | split-window -h
unbind '"'
bind - split-window -v -p 20

# quickly switch panes
unbind ^B
bind ^B select-pane -t :.+

# switch panes using Alt-arrow without prefix
bind -n M-h select-pane -L
bind -n M-l select-pane -R
bind -n M-k select-pane -U
bind -n M-j select-pane -D

# swap windows
bind-key -n C-S-Left swap-window -t -1
bind-key -n C-S-Right swap-window -t +1

# Quickly view system & process info in htop
bind-key h split-window -h "htop"
# Quickly edit todo list
bind-key w split-window -h "vim ~/vimwiki/index.md"

# rails specific
bind-key -n M-s split-window -h "bundle exec sidekiq"
bind-key -n M-r split-window -v -p 20 "redis-server"

# force a reload of the config file
unbind r
bind r source-file ~/.tmux.conf \; display "Reloaded!"

###########################
# Status Bar
###########################

# set refresh interval for status bar
set -g status-interval 30

# center the status bar
set -g status-justify left

# show session, window, pane in left status bar
set -g status-left-length 40
set -g status-left '#[fg=green]#S#[fg=blue] #I:#P #[default]'

# show hostname, date, time, and battery in right status bar
set-option -g status-right '#[fg=green]#H#[default] %m/%d/%y %I:%M #[fg=red]#(battery discharging)#[default]#(battery charging)#{net_speed}'

###########################
# Colors
###########################

# color status bar
set -g status-style bg=colour235
set -g status-style fg=white

# highlight current window
set-window-option -g window-status-current-style bg=black
set-window-option -g window-status-current-style fg=green

# set color of active pane
# set -g pane-border-style bg=colour235
# set -g pane-border-style fg=black
# set -g pane-active-border-style bg=green
# set -g pane-active-border-style fg=black
# border colours
set -g pane-border-style fg=blue
set -g pane-active-border-style bg=default,fg=blue


# Resize pane
bind-key J resize-pane -D 5
bind-key K resize-pane -U 5
bind-key H resize-pane -L 5
bind-key L resize-pane -R 5

###########################
# Plugins
###########################
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sidebar'
set -g @plugin 'tmux-plugins/tmux-net-speed'

# Display internet download and upload speed

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

Install tmux plugin manager (TPM)

Learn more about TPM. And here are the list of plugins

Installation

Installing TPM is easy, just need to clone repo inside ~/.tmux/plugins/tpm/ directory and we are done

1
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

With that you are all done with setting up tmux in your machine.

Here are the shortcut keys as per my configurations:

KeyDescription
C + bPrefix key
C + ppaste buffer
prefix + |split window horizontally
prefix + -split window vertically
prefix + btoggle switch pane
alt + hselect left pane
alt + lselect right pane
alt + jselect down pane
alt + kselect up pane
C + Shift + ←Swipe window to left
C + Shift + →Swipe window to right
prefix + hQuickly view htop info
prefix + rReload tmux configuration changes

Tmux workflow

With tmux you can easily build a workflow for daily task you do inside tmux terminal. Here is my workflow for my rails application that I always need to do before starting my work.

You can copy this file either in one of the executable PATH directory or you can also set and alias command to run it.

Create alias

I use zsh, so I will add it inside ~/.zshrc you can put it inside ~/.bashrc or any shell you are using

1
alias tmuxworkflow="sh ~/.config/tmux/myworkflow.sh"

Now create a file inside ~/.config/tmux/myworkflow.sh and add your configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SESSION=work

tmux new-session -d -s $SESSION
tmux new-window -t $SESSION:1 -n 'webserver'

tmux select-window -t $SESSION:1
tmux send-keys 'rails server' C-m

tmux split-window -h
tmux send-keys 'bundle exec sidekiq' C-m

tmux split-window -v
tmux send-keys 'elasticsearch' C-m

tmux attach -t $SESSION

Basically this will

  • create a new tmux session and name it work
  • Create 2 windows
  • Switch to first window
  • Then in the first window it will run rails server
  • Add horizontal split pane and run bundle exec sidekiq in newly splitted pane
  • Add vertical split pane and run elasticsearch server in that pane
This post is licensed under CC BY 4.0 by the author.