With Tmux, you can manage multiple sessions/terms and navigate quickly. There are advanced terminals like iTerm2 from which you can accomplish most of the tasks that can be done by tmux, but since iTerm2 is available only to macOS, you won’t be able to leverage those benefits in other systems like Debian, Fedora or WSL2 in Windows. Tmux is available on all platforms, so investing time in learning tmux will help you to use these customizations across all platforms.

Below is the screenshot of the customized tmux.

Tmux
Customized Tmux

Installing Tmux

  • For macOS, you can use brew install tmux
  • For Debian based use sudo apt install tmux
  • For Fedora-based use sudo yum install tmux

Customizing Tmux

Below is how you can customize tmux.

  • Create ~/.tmux.conf if it does not exist.
  • Enable 256 colors.
      # tmux display things in 256 colors
      set -g default-terminal "screen-256color"
      #  enable terminal compability
      set-option -ga terminal-overrides ",tmux-256color-italic:Tc"
    
  • Changing default prefix from Ctrl+b to Ctrl+a
      # unbind default prefix and set it to Ctrl+a
      unbind C-b
      set -g prefix C-a
      # if we want C-a to send to applications like emacs, bash
      bind C-a send-prefix
    
  • Add shortcut Ctrl+a r to apply tmux configuration while inside tmux session
      # reload config file
      bind r source-file ~/.tmux.conf \; display "Config Reloaded!"
    
  • Add shortcut Ctrl+a | for vertical split, Ctrl+a - for horizontal split
      bind | split-window -h -c "#{pane_current_path}"
      bind - split-window -v -c "#{pane_current_path}"
    
  • Add shortcut Ctrl+a y to synchronize panes (meaning, if you have 2 or more panes in same window, you can send command to all panes at once)
      # synchronize all panes in a window
      bind y setw synchronize-panes
    
  • To move between panes - left (Ctrl+a h) , down (Ctrl+a j), up (Ctrl+a k), right (Ctrl+a l).
      # pane movement shortcuts
      bind h select-pane -L
      bind j select-pane -D
      bind k select-pane -U
      bind l select-pane -R
    
  • To move between windows - left (Ctrl+a Ctrl+h), right (Ctrl+a Ctrl+l)
      bind -r C-h select-window -t :-
      bind -r C-l select-window -t :+
    
  • Enable mouse and vi mode
      # enable mouse support for switching panes/windows
      setw -g mouse on
      # set vi mode for copy mode
      setw -g mode-keys vi
    
  • Copy & paste in vi mode and attach OS clipboard.
      # more settings to make copy-mode more vim-like
      unbind [
      # on escape go to copy mode
      bind Escape copy-mode
      # on v start selection
      bind -Tcopy-mode-vi v send -X begin-selection
      # y in copy mode takes selection and sends it to system clipboard via pbcopy​
      bind -Tcopy-mode-vi y send -X copy-pipe-and-cancel "pbcopy"
      # paste the buffer
      unbind p
      bind p paste-buffer
    

    For Linux you can use xclip. sudo apt-get install xclip

     bind C-c run "tmux save-buffer - | xclip -i"
     bind C-v run "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer"
    
  • To quickly jump between panes - Ctrl+a a # to show all panes
      bind a choose-tree
    

    Note: by default, Ctrl+a w shows all windows in all sessions, but you need to go to each window to expand window. With above you will get all panes expanded.

Tmux Windows
C-a a demo
  • You can customize the theme like below
      ##############################
      ### Color & Style Settings ###
      ##############################
      set -g status-left-length 32
      set -g status-right-length 150
      set -g status-interval 5
    
      tmux_bg='#202328'
      tmux_fg='#bbc2cf'
      tmux_aqua='#3affdb'
      tmux_beige='#f5c06f'
      tmux_blue='#51afef'
      tmux_brown='#905532'
      tmux_cyan='#008080'
      tmux_darkblue='#081633'
      tmux_darkorange='#f16529'
      tmux_green='#98be65'
      tmux_grey="#8c979a"
      tmux_lightblue='#5fd7ff'
      tmux_lightgreen='#31b53e'
      tmux_magenta='#c678dd'
      tmux_orange='#d4843e'
      tmux_pink='#cb6f6f'
      tmux_purple='#834f79'
      tmux_red='#ae403f'
      tmux_salmon='#ee6e73'
      tmux_violet='#a9a1e1'
      tmux_white='#eff0f1'
      tmux_yellow='#f09f17'
    
    
      # default statusbar colors
      set-option -g status-style fg=$tmux_fg,bg=$tmux_bg
    
      # default window title colors
      set-window-option -g window-status-style fg=$tmux_grey
      set-window-option -g window-status-format " #I #W"
    
      # active window title colors
      set-window-option -g window-status-current-style fg=$tmux_blue,bg=$tmux_bg
      set-window-option -g  window-status-current-format "#[bold]#I #W"
    
      set-window-option -g status-left-style fg=$tmux_bg,bg=$tmux_blue
      set-window-option -g status-right-style bg=$tmux_blue
    
      set-window-option -g status-position bottom
    
      # pane border
      set-window-option -g pane-active-border-style fg=$tmux_blue
      set-window-option -g pane-border-style fg=$tmux_grey
    
      # message text
      set-option -g message-style bg=$tmux_bg,fg=$tmux_magenta
    
      # pane number display
      set-option -g display-panes-active-colour $tmux_magenta
      set-option -g display-panes-colour $tmux_blue
    
      # clock
      set-window-option -g clock-mode-colour $tmux_magenta
    
      tm_icon="🙈 🙉 🙊"
      #tm_session_name="#[bold] $tm_icon #S #[fg=$tmux_bg]"
      tm_session_name="#[bold] $tm_icon  #S #[fg=$tmux_bg]"
      set -g status-left $tm_session_name' '
    

    Or you can use theme plugins like below (Refer below on how to use Tmux plugin manager)

      # To manage themes
      set -g @plugin 'wfxr/tmux-power'
      set -g @tmux_power_theme 'gold'
    
  • You can use Tmux Plugin Manager to install tmux plugins; below are some interesting plugins.
      # List of plugins
      set -g @plugin 'tmux-plugins/tpm'
      set -g @plugin 'tmux-plugins/tmux-sensible'
      set -g @plugin 'tmux-plugins/tmux-sessionist'
      set -g @plugin 'tmux-plugins/tmux-copycat'
      set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
      set -g @plugin 'christoomey/vim-tmux-navigator'
      set -g @plugin 'tmux-plugins/tmux-open'
      set -g @plugin 'tmux-plugins/tmux-resurrect'
      set -g @plugin 'tmux-plugins/tmux-continuum'
    
      set -g status-right "#{prefix_highlight}"
    
      # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
      run '~/.tmux/plugins/tpm/tpm'
    
    • tmux-sensible sets most common settings needed for tmux.
      • This plugin also gives option to Prefix + C-n & Prefix + C-p to move between windows without releasing Ctrl.
    • tmux-sessionlist gives options to manage tmux sessions.
      • Prefix + Shift+c prompts for creating new session.
      • Prefix + Shift+x kill current session.
    • tmux-copycat gives options to copy text in copy mode.
      • Prefix + C-f to search for files
      • Prefix + / to search for text
    • tmux-prefix-highlight shows Prefix on right side corner of tmux when pressed.
    • christoomey/vim-tmux-navigator to quickly move between tmux and vim panes.
    • tmux-plugins/tmux-open to quickly open files. Prefix o to open in default application. Prefix C-o to open in $EDITOR
    • tmux-plugins/tmux-resurrect to save and re-open the session.
      • Prefix C-s to save the current sessions.
      • Prefix C-r to reload the last saved session.
      • You can customize to which applications to load
          set -g @resurrect-processes 'vi vim nvim emacs man less more tail top htop irssi weechat mutt "caffeinate -d"'
        
    • tmux-plugins/tmux-continuum will continuously save the last sessions every 15 minutes. You need to ensure that you have set -g @continuum-restore 'on' in ~/.tmux.conf
    • tmux-copy-toolkit gives a bunch of options to navigate tmux.
      • Prefix + Esc + s to jump based on one char
      • Prefix + Esc + S S to jump based on two char
      • Prefix + Esc + S n to jump to line
      • Prefix + S easy copy
      • Prefix + Q quick copy
      • Prefix + P quick open

    Note: You can find more plugins here tmux-plugins. When you install new plugins, either reload tmux Prefix + r or do Prefix + shift + I to install plugin. All plugins will be stored in ~/.tmux/plugins, in order to update you can press Prefix + shift + U and to remove plugin just remove the plugin from tmux.conf and do Prefix + alt + u.

Terminal to tmux.

Command Action
tmux new -s session-name Create new session
tmux kill-ses -t session-name Kill session
C-a d detach session, go back to terminal
tmux a re-attach to the previous session
tmux a -t session-name re-attach to given session
tmux ls list all tmux sessions

Inside Tmux

Command Action
C-a ? Show all shortcuts
C-a Esc Go to Copy Mode
C-a : Command Mode

Sessions

Command Action
C-a shift + c prompt for new session
C-a shift + x prompt to kill session
:new -s newsession to create new session
C-a ) go to next session
C-a ( go to the previous session
C-a s show all sessions
C-a $ rename session
C-a d exit tmux session go back to terminal

Windows

Command Action
C-a c create new window
C-a , rename window
C-a & kill window
C-a C-w display all sessions and windows
C-a C-l move to next window in current session
C-a C-h move to the previous window in current session

Panes

Command Action
C-a bar create vertical pane
C-a - create horizontal pane
C-a x kill pane
C-a space re-arrange panes
C-a ; toggle between panes
C-a o move to next pane
C-a l - move right
C-a h - move left
C-a j move up
C-a k move down
C-a z zoom to pane (toggle)
C-a ! convert pane to window
C-a f search all panes for given text

Buffers

Command Action
C-a # list buffers
C-a = list buffers to copy and / to search in buffers
: capture-pane capture visible details in buffer
:save-buffer buf.txt to save
Command Action
C-a / to search in current pane
C-a f prompts to search all panes

Copying previous command output

You can use the code below to enable Prefix C-c to copy the previous command output; this is referred from https://ianthehenry.com/posts/tmux-copy-last-command/. Below will work if you have “➜” in your command prompt; if not, then change it accordingly. If you are using zshrc with robbyrussell theme, below will work.

########
# copy previous command output
########
bind -r C-c {
  copy-mode
  send -X clear-selection
  send -X start-of-line
  send -X start-of-line
  send -X cursor-up
  send -X start-of-line
  send -X start-of-line

  if -F "#{m:*➜\u00A0*,#{copy_cursor_line}}" {
    send -X search-forward-text "➜"
    send -X stop-selection
    send -X -N 2 cursor-right
    send -X begin-selection
    send -X end-of-line
    send -X end-of-line
    if "#{m:*➜\u00A0?*,#{copy_cursor_line}}" {
      send -X cursor-left
    }
  } {
    send -X end-of-line
    send -X end-of-line
    send -X begin-selection
    send -X search-backward-text "➜"
    send -X end-of-line
    send -X end-of-line
    send -X cursor-right
    send -X stop-selection
  }
}
Tmux copy previous command output
C-a c demo

Quickly copy text from the buffer

I created a plugin to copy text from previous command outputs—a tmux plugin to copy text using fuzzy search. Here is a demo where this plugin can be helpful, for example, if you want to grab pod name (which often changes, so auto completions might not help) and ssh to it. Prefix + v to search for words or lines containing text. You can find the plugin here. tmux-fzf-copy

Demo showing quick copy
C-a + v quick copy

Conclusion

Using the above steps, you now know how to customize tmux. For complete tmux configuration files, you can refer to my tmux dotfiles. If you want to learn more, you can read the book tmux 2 by Brian P. Hogan. – RC