Files
2024-05-01 12:28:44 -06:00

12 KiB

TMUX: Organized Beginner's Guide

TMUX enhances your terminal experience by enabling multiple sessions, windows, and panes management. This guide demystifies TMUX, focusing on foundational commands, customization, and visual setups.

Getting Started with TMUX

Launching Sessions

  • New Session: tmux new -s session_name starts a new session named session_name.
  • Attach to Session: tmux attach -t session_name attaches to an existing session.
  • List Sessions: tmux ls shows all active sessions.
  • Detach: Ctrl+a d detaches you from the current session.

Windows and Panes

  • New Window: Ctrl+a c creates a new window within the session.
  • Navigate Windows: Ctrl+a n (next) and Ctrl+a p (previous).
  • Rename Window: Ctrl+a , prompts for a new name for the current window.
  • Split Panes: Horizontal Ctrl+a |, Vertical Ctrl+a -.
  • Navigate Panes: Ctrl+a [arrow keys] or Ctrl+a [h/j/k/l] for Vi-like navigation.
  • Resize Panes: Ctrl+a [Shift+Arrow keys] resizes panes.
  • Close Pane/Window: Exiting all panes in a window or exit in the active pane closes it.

Visual Customization and Workflow Efficiency

Edit ~/.tmux.conf for custom settings. Below are essentials for an improved look and feel:

Prefix Key

  • Prefer Ctrl+a for easier accessibility.
    unbind C-b
    set -g prefix C-a
    bind C-a send-prefix
    

Appearance

  • Status Bar: Customize its look.
    set -g status-bg black
    set -g status-fg white
    set -g status-left '#[fg=green](#S) #(whoami)'
    set -g status-right '#[fg=yellow]#(date)'
    
  • Enable 256 Color Support: Enhances visual themes.
    set -g default-terminal "screen-256color"
    

Mouse Support

  • Allows pane and window interaction using the mouse.
    set -g mouse on
    

Key Bindings

  • Customize for efficiency, e.g., pane resizing, and navigation.
    bind | split-window -h
    bind - split-window -v
    bind h select-pane -L
    bind j select-pane -D
    bind k select-pane -U
    bind l select-pane -R
    

Reloading Config

  • Apply changes without restarting TMUX.
    bind r source-file ~/.tmux.conf \; display-message "Config reloaded..."
    

Practical Workflow

  1. Initiate a Session: Start your work session.
  2. Organize Work: Use windows for major tasks and panes for detailed work.
  3. Seamless Navigation: Switch between tasks efficiently with custom bindings.
  4. Maintain Session Integrity: Detach and reattach as needed.

Advanced Tips

  • Scrollback: Enter copy mode with Ctrl+a [ for scrolling.
  • Script Automation: Pre-configure sessions, windows, and panes for projects.

Refreshing .tmux.conf:

tmux source-file ~/.tmux.conf

Or within TMUX: Ctrl+a : then source-file ~/.tmux.conf.

TMUX is a versatile tool, bridging the gap between simplicity and complexity. Its true power unfolds as you customize it to match your workflow, making terminal management intuitive and efficient.


TMUX: A Complete Guide for Beginners

TMUX is a terminal multiplexer that allows you to manage multiple terminal sessions within a single window. It's invaluable for managing complex workflows, maintaining persistent sessions, and organizing workspaces. This guide covers the basics to get you started with TMUX, focusing on typical workflows and setting up visuals.

Starting with TMUX

Launching TMUX

To start a new TMUX session, simply type tmux in your terminal. To create or attach to a named session (for easier management), use:

tmux new -s my_session

Detaching and Attaching Sessions

Detach from a session with Ctrl+a d (assuming you've set Ctrl+a as the prefix, as recommended).

To list all TMUX sessions:

tmux ls

Attach to an existing session by name:

tmux attach -t my_session

Managing Windows and Panes

Windows

  • Create a new window: Ctrl+a c
  • Switch between windows: Ctrl+a n (next), Ctrl+a p (previous)
  • Rename the current window: Ctrl+a ,
  • Close the current window: Exit all panes in the window.

Panes

  • Split pane horizontally: Ctrl+a | (custom binding)
  • Split pane vertically: Ctrl+a - (custom binding)
  • Navigate between panes: Ctrl+a [arrow key] or Ctrl+a [h/j/k/l] (Vi-like bindings)
  • Resize panes: Ctrl+a [Shift+arrow key] (custom binding for resizing)
  • Close a pane: exit or Ctrl+d in the active pane

Customizing Visuals

Edit your ~/.tmux.conf file to customize TMUX's appearance and behavior. A simple visual setup could include:

# Set the prefix to Ctrl+a for convenience
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# Improve the status bar appearance
set -g status-bg black
set -g status-fg white
set -g status-left '#[fg=green]Session: #S #[fg=white]|'
set -g status-right '#[fg=yellow]#(date)'

# Enable mouse support
set -g mouse on

Reload the TMUX configuration by detaching from your session and running:

tmux source-file ~/.tmux.conf

Or, within a TMUX session, press Ctrl+a and then :, type source-file ~/.tmux.conf, and hit Enter.

Sessions, Windows, and Panes Workflow

  1. Start a TMUX session for a new project or task (tmux new -s project_name).
  2. Create windows within the session for separate parts of your task (e.g., editing, running commands).
  3. Split windows into panes for side-by-side work on related activities.
  4. Detach and reattach to your session as needed, keeping your workspace intact between logins or reboots.

Additional Tips

  • Scrolling: Enter copy mode with Ctrl+a [ to scroll using arrow keys or PageUp/PageDown. Press q to exit copy mode.
  • Customize key bindings: To streamline your workflow, customize key bindings in ~/.tmux.conf.
  • Scripting TMUX: You can script TMUX commands to automate your environment setup.

TMUX is a powerful tool that, once mastered, will significantly enhance your command-line efficiency. Experiment with different configurations and workflows to fully harness TMUX's capabilities.


# Set UTF-8 support for TMUX, enhancing compatibility with wide character sets.
set -g utf8

# Set terminal to use 256 colors, enabling richer color schemes.
set -g default-terminal "screen-256color"

# Change the prefix from 'Ctrl+b' to 'Ctrl+a' for easier reach.
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# Vi-like keys for pane selection. Allows moving between panes using 'h', 'j', 'k', 'l'.
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Resize panes using Shift+Arrow keys. Adjusts pane size in small steps for precise layout control.
bind -n S-Left resize-pane -L 2
bind -n S-Right resize-pane -R 2
bind -n S-Up resize-pane -U 2
bind -n S-Down resize-pane -D 2

# Reload TMUX config without restarting TMUX using Prefix+r.
bind r source-file ~/.tmux.conf \; display-message "Config reloaded..."

# Split panes using '|' for vertical and '-' for horizontal splits. More intuitive symbols.
bind | split-window -h
bind - split-window -v

# Use Vim keys in copy mode to navigate text. Ensures consistency for Vim users.
setw -g mode-keys vi

# Configure the status bar's appearance and information displayed.
set -g status-interval 60                    # Update interval.
set -g status-justify centre                 # Center-align window list.
set -g status-left-length 20                 # Length of left segment.
set -g status-right-length 150               # Length of right segment.
set -g status-left '#[fg=green](#S) #(whoami)'   # Show session number and user name on left.
set -g status-right '#[fg=yellow]#(uptime | cut -d "," -f 2-4)' # Show system uptime on right.

# Simplify window list appearance in the status bar.
set -g window-status-format "#I #W"              # Default window format.
set -g window-status-current-format "#[bold]#I #W" # Highlight active window.

# Enable mouse support for interacting with panes, windows, and the status line.
set -g mouse on

# Set a generous history limit, allowing more scroll-back.
set -g history-limit 10000

# Ensure window indexing is continuous by renumbering after a window is closed.
set -g renumber-windows on

# Monitor for activity in windows, highlighting any changes.
setw -g monitor-activity on
set -g visual-activity on

TMUX Configuration File

Use Vim-style key bindings for pane selection and copy-mode

setw -g mode-keys vi set -g status-keys vi

Set prefix to Ctrl-A (alternative to the default Ctrl-B)

set-option -g prefix C-a bind-key C-a send-prefix

Reload tmux config with prefix + r

bind r source-file ~/.tmux.conf

Improve pane navigation using Vim keys (h, j, k, l) with 'Alt' as modifier

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

Resize panes with 'Alt' + Arrow keys

bind-key -n M-Left resize-pane -L 2 bind-key -n M-Down resize-pane -D 2 bind-key -n M-Up resize-pane -U 2 bind-key -n M-Right resize-pane -R 2

Enable mouse support for pane selection, resizing, and scrolling

set -g mouse on

Open new tmux window with NeoVim using prefix + e

bind-key e new-window "nvim"

Status Line Customization

set -g status-style bg=colour235,fg=colour136 set -g status-left "#[fg=green]Session: #S" set -g status-right "#[fg=yellow]#H | %Y-%m-%d %H:%M"

Customize window list appearance

setw -g window-status-format "#[fg=blue]#I:#W" setw -g window-status-current-format "#[fg=yellow]#I:#W"

Pane Border Colors

set -g pane-border-style fg=colour235 set -g pane-active-border-style fg=colour136

Synchronize panes: typing in one pane types in all panes

bind-key C-s setw synchronize-panes

Quick pane cycling with Ctrl-o

bind-key C-o select-pane -t :.+


### Changes and Rationale:

1. **Vim-style Key Bindings**: Retained for copy-mode and status-line interactions.

2. **Pane Navigation**: Changed to use `Alt` (`M-`) combined with `h/j/k/l`. This reduces potential conflicts with NeoVim's keybindings.

3. **Pane Resizing**: Also uses the `Alt` modifier to align with the pane navigation scheme.

4. **Mouse Support**: No change, as it's essential for a seamless experience.

5. **NeoVim Integration**: The shortcut for opening NeoVim in a new window (`prefix + e`) remains the same for convenience.

6. **Status Line and Pane Border Colors**: Unchanged, as they are primarily aesthetic.

After updating your `~/.tmux.conf`, reload the configuration in an active tmux session with `tmux source-file ~/.tmux.conf`. 

Next, you can share your current NeoVim configuration, and we can align it to complement your tmux setup, ensuring that the two tools work together harmoniously.
User
now provide me a cheatsheet for tmux for all my key-bindings and how to use tmux like starting sessions, getting layouts configured etc.
ChatGPT
Certainly! Below is a concise cheat sheet for tmux, focusing on your customized key-bindings and essential commands for managing sessions, windows, panes, and layouts.

### Starting and Managing tmux Sessions

- **Start a New Session**: `tmux` or `tmux new -s [session-name]`
- **List Sessions**: `tmux ls`
- **Attach to a Session**: `tmux attach -t [session-name]`
- **Detach from a Session**: `Ctrl-A` then `D` (while in tmux)
- **Kill a Session**: `tmux kill-session -t [session-name]`

### Working with Windows

- **Create a New Window**: `Ctrl-A` then `C` (capital C)
- **Open NeoVim in New Window**: `Ctrl-A` then `e`
- **Switch to Window by Number**: `Ctrl-A` then `[window-number]`
- **Rename Current Window**: `Ctrl-A` then `,`
- **Close Current Window**: Close all panes or `exit` from shell

### Pane Management

- **Split Pane Horizontally**: `Ctrl-A` then `"`
- **Split Pane Vertically**: `Ctrl-A` then `%`
- **Navigate Between Panes**: `Alt` + `h/j/k/l`
- **Resize Panes**: `Alt` + `Arrow keys`
- **Toggle Pane Synchronization**: `Ctrl-A` then `C-s`
- **Close Current Pane**: `exit` from shell in that pane

### Copy Mode and Scrolling

- **Enter Copy Mode**: `Ctrl-A` then `[`
- **Navigate in Copy Mode**: Vim-style navigation (`h/j/k/l`)
- **Scroll Up/Down**: `Ctrl-A` then `[`, then `PageUp/PageDown` or `Ctrl-B/Ctrl-F`

### Miscellaneous

- **Reload tmux Configuration**: `Ctrl-A` then `r`
- **Cycle Through Panes**: `Ctrl-A` then `C-o`
- **Show Shortcuts and Key Bindings**: `Ctrl-A` then `?`

### Customizing Layouts

- **Switching Layouts**: `Ctrl-A` then `Space` cycles through pre-set pane layouts.
- **Saving Custom Layouts**: tmux does not have a native layout saving feature, but you can script layouts or use plugins like `tmux-resurrect` or `tmuxinator` for more advanced layout management.