How to use working directories in tmux?

Quick Answer

Use the -c flag to set working directories: tmux new-session -c ~/project starts a session in that directory. For splits, use "#{pane_current_path}" to inherit the current pane's directory.

tmux new-session -c ~/project -s work

Detailed Explanation

Working directories in tmux determine where new shells start when you create sessions, windows, or panes. By default, tmux uses the directory where you started tmux, but you can control this with the -c flag.

Setting Directories for New Sessions

# Start session in specific directory
tmux new-session -c ~/project -s work

# Start and run command in directory
tmux new-session -c ~/code -s dev 'vim .'

Inheriting Current Directory

The most useful pattern is making new windows and panes inherit the current directory:

# In ~/.tmux.conf - make splits inherit current directory
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"

Manual Directory Setting

# Create window in specific directory
tmux new-window -c ~/projects/frontend -n "frontend"

# Split pane in specific directory
tmux split-window -h -c ~/projects/backend

Pro Tip

Add the key bindings above to your ~/.tmux.conf so that Ctrl+b c creates new windows in the current directory instead of the original tmux start directory.

Project Setup Script

Create a script to set up a project with proper working directories:

#!/bin/bash
# Create project session
tmux new-session -d -s myproject -c ~/projects/myproject
tmux new-window -t myproject:1 -n 'server' -c ~/projects/myproject/server  
tmux new-window -t myproject:2 -n 'client' -c ~/projects/myproject/client
tmux attach-session -t myproject