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 operations chaotic. At this time, terminal multiplexing tools become very useful. Among them, tmux is one of the commonly used terminal multiplexing tools.

Installing tmux#

On Ubuntu, you can install tmux using the following command:

sudo apt-get install tmux

Starting tmux#

Starting tmux is 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 pane in the current window
  • Ctrl+b %: Create a new vertical pane in the current window
  • Ctrl+b arrow key: Switch to different panes in the current window
  • Ctrl+b c: Create a new window
  • Ctrl+b ,: Rename the current window.
  • Ctrl+b number key: Switch to the corresponding numbered window
  • 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 tmux configuration file is ~/.tmux.conf. By modifying this configuration file, we can change tmux's default behavior and add custom shortcuts. Here are some commonly used configurations:

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

# Start numbering windows and panes from 1
set-option -g base-index 1
setw -g pane-base-index 1

# Switch windows using the 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 tmux to Start Automatically When Launching Alacritty Terminal in Arch Linux's i3wm#

  1. Install tmux

    On Arch Linux, you can install it using the following command:

    sudo pacman -S tmux
    
    
  2. Create a tmux Configuration File

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

    # Change Ctrl+b to Ctrl+a
    set-option -g prefix C-a
    
    # Start numbering windows and panes from 1
    set-option -g base-index 1
    setw -g pane-base-index 1
    
    
  3. Modify the 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 enter tmux immediately after starting 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 connect directly to that session.
  4. Restart i3wm

    Execute the following command to restart i3wm:

    i3-msg restart
    
    

    Then, when you start the Alacritty terminal, a tmux session will automatically start.

Conclusion#

tmux is a very useful terminal multiplexing tool that allows us to use the terminal more efficiently. By mastering commonly used shortcuts and modifying the configuration file, we can personalize our 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.