Skip to content

Instantly share code, notes, and snippets.

@indirect
Last active December 23, 2015 15:19
Show Gist options
  • Save indirect/6655250 to your computer and use it in GitHub Desktop.
Save indirect/6655250 to your computer and use it in GitHub Desktop.
tmux configuration
#!/bin/bash
# I alias this script to `mux`:
# $ alias mux=tmux-named
# Then I tell terminal to automatically attach new windows to my session named "main",
# by setting the preference named "Shells open with" to this command:
# /path/to/tmux-named main
#
# When working on a particular project, I can jump a session for that project
# by running "mux project-name".
# Set up paths and whatnot
test -e ~/.bashrc && source ~/.bashrc
# We need tmux. Obvs.
if [[ -z `which tmux` ]]; then echo "You need tmux first!"; exit 1; fi
# Named variables are much more flexible
name="$1"
# Make sure we got a session name to create or join
[[ -n "$name" ]] || { echo "Usage: tmux-named [SESSION_NAME]"; exit; }
# Make sure we can run homebrewed tmux
if [[ $PATH != */usr/local/bin* ]]; then export PATH=$PATH:/usr/local/bin; fi
# Make sure there is a 'name' session
tmux has -t "$name" 2> /dev/null || TMUX= tmux new -d -s "$name"
# Calculate the number of the next session
session_number=$(tmux ls -F "#S" | grep "^$name" | wc -l | sed "s/^[ ]*//")
session_name="${name} $session_number"
#echo $session_name
# If this session already exists, a lower number must be missing
while tmux has -t "$session_name" 2> /dev/null; do
let session_number--
session_name="${name} $session_number"
done
# Create the new session
TMUX= tmux new-session -d -t "$name" -s "$session_name"
# Tell this session to die when the window containing it is closed
die="set-option -q -t $session_name destroy-unattached on"
if [ -z $TMUX ]; then
# Join the new and configured session
tmux $die \; attach-session -t "$session_name"
else
# Switch this tmux client to the new session
tmux $die \; switch-client -t "$session_name"
fi
# change tmux prefix from ⌃b to ⌃t
unbind C-b
set -g prefix C-t
# tell tmux that terminal has 256 colors
set -g default-terminal "screen-256color"
# re-enable pbcopy and pbpaste inside tmux shells
set-option -g default-command "reattach-to-user-namespace -l bash"
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"
# keep 10k lines of scrollback
set -g history-limit 100000
# refresh tmux config with ⌃t R
bind R source-file ~/.tmux.conf \; display-message "Config reloaded..."
# window titles
set -g set-titles on
set-window-option -g automatic-rename on
# disable escape sequence delay
set -sg escape-time 0
# start window numbering at 1
set -g base-index 1
# resize whenever possible
setw -g aggressive-resize on
# act like vim
setw -g mode-keys vi
bind-key | split-window -h
bind-key - split-window
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind-key -r C-h select-window -t :-
bind-key -r C-l select-window -t :+
# allow scrollback in Terminal
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
# enter scrollback mode and scroll up on shift-pageup
bind-key -n M-PPage copy-mode -u
set-window-option -g xterm-keys on # to make ctrl-arrow, etc. work
# status line inspired by wemux from https://gist.github.com/2305333
set -g status-left-length 32
set -g status-right-length 150
set -g status-fg white
set -g status-bg colour234
set -g window-status-activity-attr bold
set -g pane-border-fg colour245
set -g pane-active-border-fg colour39
set -g message-fg colour16
set -g message-bg colour221
set -g message-attr bold
set -g status-left "#[fg=colour235,bg=colour252,bold] ⧉ #S #[fg=colour252,bg=colour235,nobold]⮀"
set -g window-status-format "#[fg=white,bg=colour234] #I #W"
set -g window-status-current-format "#[fg=colour234,bg=colour39]⮀#[fg=colour18,bg=colour39,noreverse,bold] #I ⮁ #W #[fg=colour39,bg=colour234,nobold]⮀"
set -g status-right " #[fg=colour238,bg=colour234]⮂#[fg=colour245,bg=colour238,bold,noreverse] #(hostname -s) "
@h3xx
Copy link

h3xx commented Aug 10, 2015

#(hostname -s) is better-written as #h

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