Minimal and Effective Tmux
Improve your experience of tmux with tmux configuration and a simple bash function
Search for a command to run...
Improve your experience of tmux with tmux configuration and a simple bash function
No comments yet. Be the first to comment.
Learn how to create a custom VSCode extension to organize and categorize your settings.json file, making your development environment cleaner and more
Learn effective Python exception handling with practical examples, including custom error classes and try-except blocks for robust error management.
A python tool to generate cheatsheet of command line tools as a PDF
Use rehype-external-links to open links in new tab
.tmux.confTmux is a terminal multiplexer that allows you to manage multiple terminal sessions from a single window. You can create, navigate, split, and close panes and windows easily. Configuring Tmux with a .tmux.conf file enhances its usability by customizing key bindings, appearance, and behavior.
To install Tmux, use the following command:
On Ubuntu/Debian:
sudo apt-get install tmux
On macOS (using Homebrew):
brew install tmux
.tmux.confThe .tmux.conf file is where you configure Tmux. This file should be located in your home directory:
nano ~/.tmux.conf
You can add custom settings, key bindings, and design tweaks to this file. Once you've made changes, reload the configuration using the command:
tmux source-file ~/.tmux.conf
Here’s a minimal .tmux.conf file:
# Set the prefix key to Ctrl + a
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# Enable mouse support
set -g mouse on
# Split panes vertically and horizontally using | and -
bind | split-window -h
bind - split-window -v
# Switch panes with Alt + arrow keys
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
To start Tmux, simply type:
tmux
Once inside a Tmux session, you can split panes, create new windows, and navigate between them using your configured key bindings.
.tmux.confThis configuration file provides several customizations. Let’s break it down:
Remap Prefix Key:
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
Ctrl + b. This remaps it to Ctrl + a, making it easier to access.Split Panes:
unbind '"'
bind | split-window -h -c "#{pane_current_path}"
unbind %
bind - split-window -v -c "#{pane_current_path}"
| splits the pane horizontally and - splits it vertically. The -c "#{pane_current_path}" ensures that the new pane starts in the same directory as the current one.Mouse Control:
set -g mouse on
Reload Configuration:
bind r source-file ~/.tmux.conf
Prefix + r to reload the .tmux.conf file without restarting Tmux.Switch Panes with Alt + Arrow Keys:
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
Move Between Windows with Ctrl + Alt + Arrow Keys:
bind -n C-M-Left previous-window
bind -n C-M-Right next-window
Ctrl + Alt + Left/Right Arrow.Window Management:
set-option -g allow-rename off
bind c new-window -c "#{pane_current_path}"
Design Tweaks:
Bell Configuration:
set -g visual-activity off
set -g bell-action none
Clock Mode:
setw -g clock-mode-colour colour1
colour1).Pane Borders and Status Bar:
set -g pane-border-style 'fg=colour0'
set -g pane-active-border-style 'fg=colour12'
set -g status-position bottom
set -g status-style 'fg=colour12'
setw -g window-status-current-style 'fg=colour12 bold'
colour12.Managing multiple Tmux sessions and windows can be cumbersome, especially if you frequently switch between directories. By adding a custom function to your .zshrc file, you can simplify this process and enhance your workflow. In this post, we'll show you how to create a powerful Tmux session manager using Zsh.
Tmux is a versatile terminal multiplexer that allows you to manage multiple terminal sessions within a single window. However, if you often find yourself creating and switching between Tmux sessions and Windows, a bit of automation can go a long way. This is where customizing your .zshrc file with a function for handling Tmux sessions can be incredibly useful.
Here's a Zsh function that automates the process of creating or attaching to a Tmux session. This script checks if a Tmux session already exists; if it does, it will either switch to an existing window or create a new one based on your current directory. If no session exists, it creates a new one.
# A better way to handle tmux sessions
function tnew() {
local parent_name="$(basename "$(dirname "$(pwd)")" | tr -d "[:space:]-")"
local current_name="$(basename "$(pwd)" | tr -d "[:space:]-")"
local session_name="Hyperspace"
local window_name="${current_name}"
# Check if any tmux session exists
if tmux list-sessions 2>/dev/null | grep -q "^"; then
# Check if a window for the current directory already exists
if tmux list-windows -t "$(tmux display-message -p '#S')" | grep -q "${window_name}"; then
# If the window exists, switch to it
tmux select-window -t "$window_name"
else
# If the window doesn't exist, create a new one
tmux new-window -n "$window_name" -c "$(pwd)"
tmux select-window -t "$window_name"
fi
tmux attach-session -t "$(tmux display-message -p '#S')"
else
# No existing session, create a new one and attach to it
tmux new-session -s "$session_name" -n "$window_name" -c "$(pwd)"
fi
}
# Aliases
alias tm='tnew'
Function Definition:
function tnew() {
# Code here
}
tnew function is defined to manage Tmux sessions.Session and Window Naming:
local parent_name="$(basename "$(dirname "$(pwd)")" | tr -d "[:space:]-")"
local current_name="$(basename "$(pwd)" | tr -d "[:space:]-")"
local session_name="Hyperspace"
local window_name="${current_name}"
parent_name and current_name are derived from the current working directory. The function uses these names to keep your Tmux sessions and windows organized.Session Handling:
if tmux list-sessions 2>/dev/null | grep -q "^"; then
# Existing session handling
else
# No session, create a new one
fi
Alias Definition:
alias tm='tnew'
tm alias is created to call the tnew function easily. Instead of typing out the entire function name, you can simply use tm to manage your Tmux sessions.Add the Function to .zshrc:
Open your .zshrc file:
nano ~/.zshrc
Add the tnew function and alias to the file.
Apply Changes:
After editing .zshrc, reload it to apply the changes:
source ~/.zshrc
Use the Alias:
tm to create or attach to a Tmux session based on your current directory.By integrating this script into your .zshrc file, you can greatly simplify the process of managing Tmux sessions and windows. It provides a seamless way to handle your terminal environments, allowing you to focus more on your work and less on session management. Give it a try and see how it enhances your productivity!
Original post: Minimal and Effective Tmux on hyperoot.dev.
Connect with me on social media:
Got feedback or questions? Feel free to leave a comment below or reach out to me on my personal site. Your thoughts are valuable!