Skip to content

Improve tmux with a simple bash function


Are you tired of juggling multiple terminal windows and tabs just to keep your work organized? Enter tmux, a powerful terminal multiplexer that can revolutionize the way you work in the command line.

To make the most of tmux, consider integrating it into your workflow with a simple yet effective script like the one below:

# file -> .zshrc
function tnew() {
    local parent_name="$(basename "$(dirname "$(pwd)")"| tr -d "[:space:]-")"
    local current_name="$(basename "$(pwd)" | tr -d "[:space:]-")"
    local session_name="${parent_name}_${current_name}"

    if tmux has-session -t "$session_name" 2>/dev/null; then
        tmux attach-session -t "$session_name"
        tmux new-session -s "$session_name"

alias tm='tnew'

Let’s break down what this script does:

To use this script, simply add it to your shell configuration file (e.g., .bashrc, .zshrc) and reload your shell. Now, instead of manually managing tmux sessions, you can simply type tm in your terminal to create or attach to a session based on your current directory.

With this streamlined workflow, you can say goodbye to terminal clutter and focus on what really matters – getting things done. Give it a try and experience the power of tmux sessions firsthand!