Skip to content

Instantly share code, notes, and snippets.

@rxa254
Forked from lucaspiller/gist:3377737
Created November 30, 2015 05:21
Show Gist options
  • Save rxa254/65e365ecc58d2d9c8887 to your computer and use it in GitHub Desktop.
Save rxa254/65e365ecc58d2d9c8887 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment