Local Development - By enabling various layouts and communication between multiple processes, tmux can enable enhanced efficiency for local development.
TMUX VS SCREEN
TMUX runs on OpenBSD, FreeBSD, NetBSD, Linux, OS X and Solaris
$ brew install tmux
# apt-get install tmux
Also installs:
$ brew install reattach-to-user-namespace
The solution:
From there you can add the following lines to your `.tmux.conf` file and you will be able to copy and paste:
# 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"
$ brew tap thoughtbot/formulae
$ brew install rcm
Session - A named collection of one or more windows.
Window - A single screen within tmux, similar to tabs in terminal applications.
At any given time, a client will be attached to a single window.
Pane - A portion of a window running a single process, e.g. Vim, zsh, man, etc.
Panes can be oriented either vertically or horizontally and resized as needed.
tmux new-session -s {name}
$ cat $(command -v tat)
#!/bin/sh
#
# Attach or create tmux session named the same as current directory.
path_name="$(basename "$PWD" | tr . -)"
session_name=${1-$path_name}
not_in_tmux() {
[ -z "$TMUX" ]
}
session_exists() {
tmux list-sessions | sed -E 's/:.*$//' | grep -q "^$session_name$"
}
create_detached_session() {
(TMUX='' tmux new-session -Ad -s "$session_name")
}
create_if_needed_and_attach() {
if not_in_tmux; then
tmux new-session -As "$session_name"
else
if ! session_exists; then
create_detached_session
fi
tmux switch-client -t "$session_name"
fi
}
create_if_needed_and_attach
<prefix> d Detach current session
tmux detach-session
tmux list-sessions; tmux ls
bind-key C-h choose-tree
<prefix> <ctrl> h Interface for session selection
tmux list-command | grep session
bind j new-window -c "#{pane_current_path}"
<prefix> {n} go to the window
bind-key b break-pane -d
tmux list-command | grep window
bind-key - split-window -v -c '#{pane_current_path}'
bind-key \ split-window -h -c '#{pane_current_path}'
~/.tmux.conf
# Smart pane switching with awareness of vim splits
is_vim='echo "#{pane_current_command}" | grep -iqE "(^|\/)g?(view|n?vim?)(diff)?$"'
bind -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"
<prefix> o cycle through panes
<prefix> <space> cycle layouts
<prefix> x close pane
<prefix> q show pane numbers
<prefix> z Zoom pane
Explore more:
tmux list-commands | grep pane
# 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"
<prefix> [ to start "copy-mode"
One project per session
One Vim instance per session
Tmux executable
Tmux command prompt
.tmux.conf file
Not persist through restarts
Useful for scripting
Not persist through restarts
Useful for exploring new features
Persisted
Useful
Key bindings
bind-key r source-file ~/.tmux.conf \; display-message "~/.tmux.conf reloaded"
Will bind through `prefix`
-n flag let you setup binding without prefix
-r flag let you input couple commands in chain
tmux ls | grep : | cut -d. -f1 | grep -v attached |
awk '{print substr($1, 0, length($1)-1)}' | xargs -L 1 tmux kill-session -t
Script to kill all unnamed, detached sessions
The end