o3o3o

点滴记录

0%

Tmux Tips

Tmux introduction

Here are some basic introduction links:

Advanced configuration

I have used tmux for several years and had tried to many features. Only some useful and frequently configurations I used have been retained.

Include:

  • alias: to simplify command
  • Use vim shortcut in tmux
  • Workspace session save/restore with tmux

Alias

If you use oh-my-zsh, you can directly add tmux for the key plugins in the .zhsrc file. It enables some aliases like:

1
2
3
4
5
6
7
ta='tmux attach -t'
tad='tmux attach -d -t'
tkss='tmux kill-session -t'
tksv='tmux kill-server'
tl='tmux list-sessions'
tmux=_zsh_tmux_plugin_run
ts='tmux new-session -s

Enable vim shortcuts in tmux

Edit your .tmux.conf file:

1
2
3
4
5
6
7
8
9
10
11
set-window-option -g mode-keys vi

bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

split-window -c "#{pane_current_path}"
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"

  • vim’s direction keys: hjkl
  • switch panel C + b + [h/j/k/l]
  • split panel and new window:
    C + b + ": split panel verticaly
    C + b + %: split horizonaly
    C + b + c: new window

Session saving and restoration

In general, you have to open several tmux sessions when you are working and don’t want to reboot computer for updating system just because your tmux session will disapear so that you have to recreate your work tmux session by hand.

So where there is a need, there is a plugin.
It is practial. Now let’s start to install it.

  1. You’d better use the tmux plugin manger to install tmux plugin easily just like Bundle in vim.

    1
    git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
  2. Now we can configure about session saving plugin in .tmux.conf

1
2
3
4
5
6
7
8
9
set -g @tpm_plugins '            \
tmux-plugins/tpm \
tmux-plugins/tmux-continuum \
tmux-plugins/tmux-resurrect \
'
run-shell '~/.tmux/plugins/tpm/tpm'

set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @continuum-save-interval '1440'
  • tmux-resurrent plugin is the dependence for continuum and to help you auto save your work session with executing c + b + s to save session manually.

  • continuum-save-interval is set to 1440 stands for tmux-continuum plugin will save you current session with 1440 seconds (half a day). 1440 is set depends on my using experience, because setting too low value eats more cpu and effect your workflow when you have many tmux sesison, setting too high value means that you have to take high risk to lose your last work session.

  1. Reload configuration to enable it

    1
    tmux new -d -s delete-me && tmux run-shell ~/.tmux/plugins/tpm/scripts/install_plugins.sh && tmux source-file ~/.tmux.conf && tmux kill-session -t delete-me
  2. Restoring your session quickly with alias

The continuum-restore option works only when the tmux server starts so there will be a useless temporary session to close later.
So here is an alias to restore session without other things. Just add it into your .zshrc or .bashrc.

1
alias ,tmuxr='tmux new -d -s delete-me && tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh && tmux kill-session -t delete-me && tmux attach || tmux attach'

You just execute ,tmuxr to recover your last work session.

Tips

If there a some unknown reasons that your session wasn’t saved last time, you can go to the directory ~/.tmux/resurrect and make a soft link to last file.

1
ln -s tmux_resurrect_2020-02-26T15:27:02.txt last

Install quickly in Ubuntu

Here is my dots to install some essential tools (zsh,tmux,docker etc..). Welcome to fork it :).