Skip to content

Instantly share code, notes, and snippets.

@lucaspiller
Created August 17, 2012 10:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lucaspiller/3377737 to your computer and use it in GitHub Desktop.
Save lucaspiller/3377737 to your computer and use it in GitHub Desktop.
Tmux Primer

I use this function to setup / resume my tmux sessions. If a session exists with the given name it resumes it, otherwise it creates it:

tm() {
  [[ -z "$1" ]] && { echo "usage: tm <session>" >&2; return 1; }
  tmux has -t $1 && tmux attach -d -t $1 || tmux new -s $1
}

(N.b. If you use something similar already check you have the -d flag. This will kill old clients (e.g. ssh connections that died, quitting your terminal before disconnecting) and ensure the window resizes correctly.)

Navigation

Tmux uses a leader for functions similar to Vim. By default it is Ctrl b. I have it mapped to Ctrl h so it doesn't conflict with Vim, although I'm still not 100% happy with it. All of these need to be prefixed with the leader.

c - Create a new window
0-9 - Switch to the given window index

" - Split a pane vertically
% - Split a pane horizontally

Up / Down / Left / Right - Move to the pane above / below / to the left / to the right

d - Detatch from session

Tmux.conf tweaks

# enable mouse mode
set -g mode-mouse on

# use mouse to select panes
set-option -g mouse-select-pane on

# use mouse to resize panes
set-option -g mouse-resize-pane on
set-option -g mouse-select-window on

# enable 256 color support
set -g default-terminal "screen-256color"

# clearer status bar
set -g status-bg '#171717'
set -g status-fg '#ffffff'

# remove status text
set -g status-left ''
set -g status-right ''

# highlight active window
set-window-option -g window-status-current-bg '#009AC0'

# start windows from 1
set -g base-index 1

Vim integration

I use terminal Vim within tmux. I always have it running on window 1. I have this function (inspired by tmux-vim) to open a file in the Vim instance of the current tmux session:

v() {
  if [[ -z "$TMUX" ]]; then
    # tmux is not running, so just open it in Vim
    vim $1
  else
    tmux select-window -t 1
    tmux send-keys escape
    tmux send-keys :badd space $1 enter
    tmux send-keys :edit space $1 enter
  fi
}
@pusewicz
Copy link

That's mine tmux.conf:

set-option -g mouse-select-pane on
set -g default-terminal "screen-256color"

# Set status bar
set -g status-bg black
set -g status-fg white
set -g status-left '#[fg=green]#H'

# Highlight active window
set-window-option -g window-status-current-bg red

# Set right status to uptime and load
set -g status-right '#[fg=yellow]#(uptime | cut -d "," -f 4-)'

# Automatically set window title
setw -g automatic-rename off

# Vi style bindings
setw -g mode-keys vi

unbind % # Remove default binding since we’re replacing
bind | split-window -h
bind - split-window -v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment