JustToThePoint English Website Version
JustToThePoint en español
JustToThePoint in Thai

How to improve your productivity in your terminal with Tmux

A terminal multiplexer is a software application that can be used to split a single terminal into one or more virtual sessions. Basically, we can have multiple sessions inside one single terminal and attach and detach these sessions when required.

It is useful for dealing with multiple command line tools and applications and for separating programs from the Unix shell’s sessions that started them, that’s particularly important when we want a remote process or program to continue running even when the connection is closed or down.

Let’s say you have to administer a remote server via ssh but the connection is poor and unreliable. That means you have to reconnect often and you don’t want to lose all your job, and restart it all over again. Terminal multiplexers offer the possibility of saving your sessions and then disconnect. Once you log on again, you can reattach to your last session and continue just where you left. A terminal multiplexer

A terminal multiplexer

 

Tmux

  1. Installation: sudo apt install tmux tmuxinator.
  2. All commands in Tmux start with a prefix, which by default is: Ctrl + b. You can start your first Tmux session by typing: tmux, but it is a better idea to create a new named session (you should use a descriptive name): tmux new -s session_name. You can detach from the session and return to your normal shell (Ctrl + b + d, detach), show all sessions (tmux ls), and attach to the last session (tmux attach) or to a named session (tmux a -t mysession). Finally, you kill the session: tmux kill-session -t mysession
  3. When you start a new session, it creates one window and a single panel inside. You can create a new window (kind of virtual terminals or desktops) by typing: Ctrl + b + c (create). You can switch to the previous window (Ctrl + b + p, previous), the next window (Ctrl + b + n, _n_ext) or directly to a particular window (Ctrl + b + number). You can close the current windows by typing: Ctrl + b + &.
  4. We can split the window into panes horizontally (Ctrl + b + “) or vertically (Ctrl + b + %). Next you may want to switch between panes: Prefix + ←, ↑, →, ↓. or kill a pane (Ctrl + b + x).
  5. Customizing Tmux (A more detail explanation in Making tmux Pretty and Usable by Ham Vocke). For that purpose, you must edit its configuration file: nvim ~/.tmux.conf
    # remap prefix from 'Ctrl-b' to 'Ctrl-a'
    unbind C-b
    set-option -g prefix C-a
    bind-key C-a send-prefix
    
    # Mouse compatibility
    set-option -g mouse on
    set -g set-clipboard on
    
    # split panes using Ctrl-h and Ctrl-v
    bind h split-window -h
    bind v split-window -v
    unbind '"'
    unbind %
    
    # switch panes using Alt-arrow without prefix
    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
    
    # reload tmux conf with Prefix + r
    bind r source ~/.tmux.conf \; display "~/.tmux.conf sourced!"
    # command delay? No
    set -g escape-time 10
    ############################################
    ### DESIGN CHANGES (/u/dothebarbwa)      ###
    ############################################
    
    # loud or quiet?
    set -g visual-activity off
    set -g visual-bell off
    set -g visual-silence off
    setw -g monitor-activity off
    set -g bell-action none
    
    #  modes
    setw -g clock-mode-colour colour5
    setw -g mode-style 'fg=colour1 bg=colour18 bold'
    
    # panes
    set -g pane-border-style 'fg=colour19 bg=colour0'
    set -g pane-active-border-style 'bg=colour0 fg=colour9'
    
    # statusbar
    set -g status-position bottom
    set -g status-justify left
    set -g status-style 'bg=colour18 fg=colour137 dim'
    set -g status-left ''
    set -g status-right '#[fg=colour233,bg=colour19] %d/%m #[fg=colour233,bg=colour8] %H:%M:%S '
    set -g status-right-length 50
    set -g status-left-length 20
    
    setw -g window-status-current-style 'fg=colour1 bg=colour19 bold'
    setw -g window-status-current-format ' #I#[fg=colour249]:#[fg=colour255]#W#[fg=colour249]#F '
    
    setw -g window-status-style 'fg=colour9 bg=colour18'
    setw -g window-status-format ' #I#[fg=colour237]:#[fg=colour250]#W#[fg=colour244]#F '
    
    setw -g window-status-bell-style 'fg=colour255 bg=colour1 bold'
    
    # messages
    set -g message-style 'fg=colour232 bg=colour16 bold'
    
  6. We are going to use tmuxinator to create and manage tmux sessions easily.
  7. Let’s create a new project: tmuxinator new mySession. We can edit it later on with: tmuxinator edit mySession
  8. Layouts can be customized. If you want to get the custom layout for your current window, just type: tmux lsw -F “#{window_active} #{window_layout}” | awk ‘$1{print $2}’. In my case, it was 4a38,192x55,0,0{101x55,0,0,2,90x55,102,0,8}.
    # /home/$USER/.config/tmuxinator/mySession.yml
    name: mySession # Session's name and project
    root: ~/
    
    windows: # The windows option allows the specification of any number of tmux windows. We define three windows: first_window, system_info, and entertaining.
      - first_window:
          layout: main-horizontal # You can arrange panes in one of the five preset layouts: even-horizontal, even-vertical, main-horizontal, main-vertical, or tiled.
          panes: # Panes are optional and are children of window entries. We define the panes that get created inside each window. 
            - nvim myfile.txt # Under each pane, then we can give a command to execute.
            - cal
            - date
      - system_info:
          layout: tiled
          panes:
            - free -h
            - df -h
            - sensors
            - htop
      - entertaining:
          layout: 4a38,192x55,0,0{101x55,0,0,2,90x55,102,0,8} # We use the previous layout.
          panes:
              - is_justtothepoint_alive:
                  - ping -c 4 justtothepoint.com : # You can execute many commands in a pane, e.g., ping -c 4 justtothepoint.com, is justtothepoint.com alive?
                  - newsboat
              - music:
                  - radio
    
  9. Start a session: tmuxinator mySession
  10. Finally, we can remove a project (tmuxinator delete [project]), list all the projects (tmuxinator list), and kill the session (tmux kill-session -t mysession)
Bitcoin donation

JustToThePoint Copyright © 2011 - 2022 PhD. Máximo Núñez Alarcón, Anawim. ALL RIGHTS RESERVED. Bilingual e-books, articles, and videos to help your child and your entire family succeed, develop a healthy lifestyle, and have a lot of fun.

This website uses cookies to improve your navigation experience.
By continuing, you are consenting to our use of cookies, in accordance with our Cookies Policy and Website Terms and Conditions of use.