banner
orion

orion

中国科学技术大学研究生;数据库内核开发工程师;生产力爱好者;

How to use tmux for terminal multiplexing

When developing, maintaining servers, or running scripts, we often need to open multiple windows in the terminal for operations. However, if each window requires opening a new terminal, it not only wastes time but also makes the operation chaotic. At this time, terminal multiplexer tools are very useful. Among them, tmux is one of the commonly used terminal multiplexer tools.

Installing tmux#

On Ubuntu, you can use the following command to install tmux:

sudo apt-get install tmux

Starting tmux#

Starting tmux is very simple, just enter the following command in the terminal:

tmux

This will open a new tmux session.

tmux shortcuts#

One of the great features of tmux is its shortcuts, which allow us to use it more efficiently.

Here are some commonly used tmux shortcuts:

  • Ctrl+b ": Create a new horizontal panel in the current window
  • Ctrl+b %: Create a new vertical panel in the current window
  • Ctrl+b arrow key: Switch to a different panel in the current window
  • Ctrl+b c: Create a new window
  • Ctrl+b ,: Rename the current window.
  • Ctrl+b number key: Switch to the window with the corresponding number
  • Ctrl+b d: Detach from the current tmux session
  • tmux attach: Reconnect to the previous tmux session

More shortcuts can be viewed using the tmux list-keys command.

tmux configuration file#

The configuration file for tmux is ~/.tmux.conf. By modifying this configuration file, we can change the default behavior of tmux and add custom shortcuts. Here are some commonly used configurations:

# Change Ctrl+b to Ctrl+a
set-option -g prefix C-a

# Start window and panel numbering from 1
set-option -g base-index 1
setw -g pane-base-index 1

# Switch windows using Alt key
bind-key -n M-h select-pane -L
bind-key -n M-l select-pane -R
bind-key -n M-j select-pane -D
bind-key -n M-k select-pane -U

# Create a new window with Ctrl+a c
bind-key C-a c new-window

How to configure alacritty terminal to automatically start tmux in Arch Linux with i3wm#

  1. Install tmux

    On Arch Linux, you can use the following command to install:

    sudo pacman -S tmux
    
    
  2. Create tmux configuration file

    Create a file named .tmux.conf in the user directory and add the following content:

    # Change Ctrl+b to Ctrl+a
    set-option -g prefix C-a
    
    # Start window and panel numbering from 1
    set-option -g base-index 1
    setw -g pane-base-index 1
    
    
  3. Modify alacritty configuration file

    Open the alacritty configuration file ~/.config/alacritty/alacritty.yml and add the following content:

    shell:
      program: /usr/bin/tmux
      args:
        - new-session
    
    

    This will automatically start a new tmux session when launching the alacritty terminal.

    If you want to immediately enter tmux after launching alacritty, you can add the following content:

    shell:
      program: /usr/bin/tmux
      args:
        - new-session
        - -A
    
    

    The A option means that if a tmux session already exists, it will directly connect to that session.

  4. Restart i3wm

    Execute the following command to restart i3wm:

    i3-msg restart
    
    

    Then, when launching the alacritty terminal, it will automatically start a tmux session.

Conclusion#

tmux is a very useful terminal multiplexer tool that allows us to use the terminal more efficiently. By mastering the common shortcuts and modifying the configuration file, we can personalize the use of tmux. I hope this article is helpful to you!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.