Showing posts with label tmux. Show all posts
Showing posts with label tmux. Show all posts

Saturday, November 24, 2012

A Tmux Crash Course

SkyHi @ Saturday, November 24, 2012

I’ve been using Tmux for about six months now and it has become just as essential to my workflow as vim. Pane and window management, copy-mode for navigating output, and session management make it a no-brainer for those who live in the terminal (and especially vim). I’ve compiled a list of tmux commands I use daily to help me work more efficiently.
Tmux Panes
If a tmux command I mention is bound to a keyboard shortcut by default, I’ll note that in parenthesis.

SESSION MANAGEMENT

Sessions are useful for completely separating work environments. I have a ‘Work’ session and a ‘Play’ session; in ‘Work’, I keep everything open that I need during my day-to-day development, while in ‘Play’, I keep open current open-source gems or other work I hack on at home.
tmux new -s session_name
creates a new tmux session named session_name
tmux attach -t session_name
attaches to an existing tmux session named session_name
tmux switch -t session_name
switches to an existing session named session_name
tmux list-sessions
lists existing tmux sessions
tmux detach (prefix + d)
detach the currently attached session

WINDOWS

Tmux has a tabbed interface, but it calls its tabs “Windows”. To stay organized, I rename all the windows I use; if I’m hacking on a gem, I’ll name the window that gem’s name. The same thing goes for client applications. That way, I can recognize windows by context and not what application it’s running.
tmux new-window (prefix + c)
create a new window
tmux select-window -t :0-9 (prefix + 0-9)
move to the window based on index
tmux rename-window (prefix + ,)
rename the current window

PANES

Panes take my development time from bland to awesome. They’re the reason I was able to uninstall MacVim and develop solely in iTerm2. I don’t have to switch applications to switch contexts (editing, reading logs, IRB, etc.) - everything I do, I do in a terminal now. People argue that OS X’s Cmd+Tab is just as fast, but I don’t think so.
tmux split-window (prefix + ")
splits the window into two vertical panes
tmux split-window -h (prefix + %)
splits the window into two horizontal panes
tmux swap-pane -[UDLR] (prefix + { or })
swaps pane with another in the specified direction
tmux select-pane -[UDLR]
selects the next pane in the specified direction
tmux select-pane -t :.+
selects the next pane in numerical order

HELPFUL TMUX COMMANDS

tmux list-keys
lists out every bound key and the tmux command it runs
tmux list-commands
lists out every tmux command and its arguments
tmux info
lists out every session, window, pane, its pid, etc.
tmux source-file ~/.tmux.conf
reloads the current tmux configuration (based on a default tmux config)

MUST-HAVES

These are some of my must-haves in my tmux config:
# remap prefix to Control + a
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# force a reload of the config file
unbind r
bind r source-file ~/.tmux.conf

# quick pane cycling
unbind ^A
bind ^A select-pane -t :.+

WORKFLOW

During the day, I’ll work on one or two Rails apps, work on my dotfiles, run irssi, and maybe run vim in another window to take notes for myself. As I mentioned, I run all of this inside one tmux session (named work) and switch between the different windows throughout the day.
When I’m working on any Ruby work specifically, I’ll have a 75%/25% vertical split for vim and a terminal so I can run tests, interact with git, and code. If I run tests or ‘git diff’ and want to see more output than the 25% allots me, I’ll use tmux to swap the panes and then move into copy mode to see whatever I need to see.
Finally, I run iTerm2 in full-screen mode. Switching between OS X apps for an editor and a terminal is for chumps!



REFERENCES
http://robots.thoughtbot.com/post/2641409235/a-tmux-crash-course

Thursday, March 15, 2012

tmux disable confirmation prompt on kill-window

SkyHi @ Thursday, March 15, 2012
By default  & is bound to confirm-before -p "kill-window #W? (y/n)" kill-window. The use of confirm-before causes the prompting. If you do not want the confirmation prompt, then just rebind & directly to kill-window:
bind-key & kill-window
You may also want to rebind x, too. It defaults to a confirming version of kill-pane; though you might want to consider whether this one might be too easy to accidentally type before removing the confirmation.
bind-key x kill-pane
Note: Both examples above are suitable for direct inclusion in .tmux.conf, but you could also type them into a  : prompt, or type them into a shell after tmux (though you would have to quote/escape & since it usually special to the shell).
I think those are the only default bindings that use confirm-before. You can check your particular configuration like this:
tmux list-keys | grep confirm-before

REFERENCES
 http://unix.stackexchange.com/questions/30270/tmux-disable-confirmation-prompt-on-kill-window

Friday, December 16, 2011

Tmux swap panes

SkyHi @ Friday, December 16, 2011

SESSION MANAGEMENT

Sessions are useful for completely separating work environments. I have a ‘Work’ session and a ‘Play’ session; in ‘Work’, I keep everything open that I need during my day-to-day development, while in ‘Play’, I keep open current open-source gems or other work I hack on at home.
tmux new -s session_name
creates a new tmux session named session_name
tmux attach -t session_name
attaches to an existing tmux session named session_name
tmux switch -t session_name
switches to an existing session named session_name
tmux list-sessions
lists existing tmux sessions
tmux detach (prefix + d)
detach the currently attached session

WINDOWS

Tmux has a tabbed interface, but it calls its tabs “Windows”. To stay organized, I rename all the windows I use; if I’m hacking on a gem, I’ll name the window that gem’s name. The same thing goes for client applications. That way, I can recognize windows by context and not what application it’s running.
tmux new-window (prefix + c)
create a new window
tmux select-window -t :0-9 (prefix + 0-9)
move to the window based on index
tmux rename-window (prefix + ,)
rename the current window

PANES

Panes take my development time from bland to awesome. They’re the reason I was able to uninstall MacVim and develop solely in iTerm2. I don’t have to switch applications to switch contexts (editing, reading logs, IRB, etc.) - everything I do, I do in a terminal now. People argue that OS X’s Cmd+Tab is just as fast, but I don’t think so.
tmux split-window (prefix + ")
splits the window into two vertical panes
tmux split-window -h (prefix + %)
splits the window into two horizontal panes
tmux swap-pane -[UDLR] (prefix + { or })
swaps pane with another in the specified direction
tmux select-pane -[UDLR]
selects the next pane in the specified direction
tmux select-pane -t :.+
selects the next pane in numerical order

HELPFUL TMUX COMMANDS

tmux list-keys
lists out every bound key and the tmux command it runs
tmux list-commands
lists out every tmux command and its arguments
tmux info
lists out every session, window, pane, its pid, etc.
tmux source-file ~/.tmux.conf
reloads the current tmux configuration (based on a default tmux config)

MUST-HAVES

These are some of my must-haves in my tmux config:
# remap prefix to Control + a
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# force a reload of the config file
unbind r
bind r source-file ~/.tmux.conf

# quick pane cycling
unbind ^A
bind ^A select-pane -t :.+

WORKFLOW

During the day, I’ll work on one or two Rails apps, work on my dotfiles, run irssi, and maybe run vim in another window to take notes for myself. As I mentioned, I run all of this inside one tmux session (named work) and switch between the different windows throughout the day.
When I’m working on any Ruby work specifically, I’ll have a 75%/25% vertical split for vim and a terminal so I can run tests, interact with git, and code. If I run tests or ‘git diff’ and want to see more output than the 25% allots me, I’ll use tmux to swap the panes and then move into copy mode to see whatever I need to see.
Finally, I run iTerm2 in full-screen mode. Switching between OS X apps for an editor and a terminal is for chumps!
REFERENCES

Tuesday, October 18, 2011

Use terminal scrollbar with tmux and scrollback

SkyHi @ Tuesday, October 18, 2011

This is possible in both Screen and in tmux and the workaround is similar: to fool the multiplexers into thinking that the terminal has no "alternate screen" mode (such as that used by pico, mutt, etc). This is accomplished by setting termcap commands for the session. For GNU screen, put this in your .screenrc:
termcapinfo xterm*|xs|rxvt|terminal ti@:te@
and for tmux, add this to your .tmux.conf:
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
The 'xterm*' part of the command should be set to whatever your terminal-emulator is declared as. Some form of xterm is a good guess, but you can check your on most sane *nix systems with:
termname
and this can usually be set in the preferences of your terminal program (ie: For Apple's Terminal.app, it's in Settings->Advanced->Emulation->"Declare terminal as"
The end result is that the overflow ends up in the terminal's scrollback buffer instead of disappearing. Of course, since this is one static buffer, things will get messy as you switch between screen or tmux windows, but this is handy for quickly flicking up to see the output of an ls command or the such.



Add the following to your ~/.screenrc:
termcapinfo xterm ti@:te@
termcapinfo xterm-color ti@:te@
This will let you use the Terminal.app scrollbar instead of relying on screen's scrollback buffer.


Scrollback for tmux
Ctrl-A + PageUp or PageDown

REFERENCES

Saturday, September 24, 2011

switching from gnu screen to tmux (updated)

SkyHi @ Saturday, September 24, 2011

update #1: upstream accepted my patch, so the next tmux release will provide window-status-alert-{attr,fg,bg} ;)Only difference is the use of alert instead of flagged. It sounds better anyway ;) I’ll adjust my patch on 1.2 as well :)
update #2 [2010/05/17]: uploaded my updated config file, now using ` as my prefix key
I gave tmux a try yesterday.
Clean config file, thorough documentation and a few nice touches here and there (i.e. better, persistent window splitting) make it a nice alternative to screen, but the biggest difference lies in memory usage. Screen can easily eat up to 40-50mb with just a few windows open, but tmux has yet to reach the 10mb mark!
You may argue that ram is cheap these days, but when you’re on a 360mb VPS ram matters ;)
I did find a few bugs/annoyances, for instance if you add set-option -g default-terminal “screen-256color” in your config file, tmux stops evaluating the #T variable.
I hacked my way out of this one by adding the following in my .bash_profile instead:
[[ $TERM == "screen" ]] && export -p TERM="screen-256color"
Turns out that’s not tmux’s fault but bash’s, I’ll have to create a patch for that as well :) If you have the same issue, you can use the above hack as a temporary fix.

Also version 1.2 does not provide a way for you to customize the colors used on window titles with alerts (either monitored or when the bell is active), but I patched that and sent it upstream ;)
You can find an ebuild with the patch in my overlay, wirelay (layman -a wirelay ;) ).
The patch was accepted upstream so it’ll be in the next release.
My tmux looks like this:


and with some split panes:

finally, below is my (updated on 2010/05/17tmux.conf config file, used in the above screenshots.
I decided to use ` as my prefix key, its really better than hitting ctrl-a all the time



# ` is an interesting key for a prefix
set-option -g prefix `
# set-option -g prefix C-a

unbind-key C-b
bind-key C-a last-window
bind-key ` last-window
bind-key a send-prefix

# we might need ` at some point, allow switching
# we can also send the prefix char with `-a
bind-key F11 set-option -g prefix C-a
bind-key F12 set-option -g prefix `

# 0 is too far from ` 
set -g base-index 1

# set-option -g default-terminal "screen-256color"
set-option -g mouse-select-pane on
set-option -g status-keys vi
set-option -g bell-action any
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not)
set-option -g visual-bell on

setw -g mode-keys vi
setw -g mode-mouse on
setw -g monitor-activity on

bind e previous-window
bind f next-window
bind j up-pane
bind k down-pane

set-option -g status-utf8 on
# set-option -g status-justify centre
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40

set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

set-option -g message-fg black
set-option -g message-bg green

#setw -g mode-bg black

setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-alert-attr default
setw -g window-status-alert-fg yellow

set -g status-left '#[fg=red]#h#[fg=green]:#[fg=white]#S #[fg=green]][#[default]'

# set -g status-right '#[fg=green]][#[fg=white] #T #[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default]'
set -g status-right '#[fg=green]][ #[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default]'

set -g history-limit 4096

# `+r reloads the configuration, handy
bind r source-file ~/.tmux.conf

Thursday, September 22, 2011

tmux.conf

SkyHi @ Thursday, September 22, 2011
#http://www.mindfuzz.net/?p=178 
# Make it use C-a, similar to screen..
unbind C-b
unbind l
set -g prefix C-a
bind-key C-a last-window
http://superuser.com/questions/209437/how-do-i-scroll-in-tmux

##Sane scrolling
set -g terminal-overrides 'xterm*:smcup@:rmcup@'

#http://www.msscripting.com/2011/06/14/tmux-config/
#Highlighting the active window in status bar
setw -g window-status-current-bg black
setw -g window-status-current-fg yellow
setw -g window-status-alert-bg black
setw -g window-status-alert-fg cyan  # backwards


#http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2/

#!/bin/sh
tmux new-session -d -s hawkhost

tmux new-window -t hawkhost:1 -n 'Server1' 'ssh root@10.x.x.x'
tmux new-window -t hawkhost:2 -n 'Server2' 'ssh root@10.x.x.x'
tmux new-window -t hawkhost:3 -n 'Server3' 'ssh root@10.x.x.x'
tmux new-window -t hawkhost:4 -n 'Server4' 'ssh root@10.x.x.x'
tmux new-window -t hawkhost:5 -n 'Server5' 'ssh root@10.x.x.x'
tmux select-window -t hawkhost:1
tmux -2 attach-session -t hawkhost




Here is my current .tmux.conf:
#https://bbs.archlinux.org/viewtopic.php?id=84157&p=1
#.tmux.conf:
# vi mode
set-window-option -g mode-keys vi
set-option -g status-keys vi

# misc settings
set-window-option -g utf8 on
set-window-option -g automatic-rename off
set-window-option -g mode-mouse on
set bell-action none

# statusbar
set-option -g status-bg black
set-option -g status-fg yellow
set-option -g status-right '#H %d.%m.%Y %H:%M'
set-window-option -g window-status-current-attr bold

and heres a example project file that i start, when i login to xmonad, that creates all my current project windows and panes:
#!/bin/sh
# creates the environment for the project a

tmux start-server

if ! $(tmux has-session -t ProjectA) 
then

    cd /srv/www/ProjectA
    tmux new-session -d -s ProjectA -n terminal

    tmux new-window -t ProjectA:1 -n editor
    tmux set-window-option -t ProjectA:1 aggressive-resize on

    cd /srv/www/ProjectA/system/usr/share/doc/database
    tmux new-window -t ProjectA:2 -n database
    tmux split-window -t ProjectA:2
    tmux resize-pane -D -t ProjectA:2.0 20 
    tmux select-pane -t ProjectA:2.0

    tmux new-window -t ProjectA:3 -n log 
    tmux split-window -t ProjectA:3

    cd /srv/www/ProjectA/system/usr/share/doc/test
    tmux new-window -t ProjectA:4 -n test

    tmux send-keys -t ProjectA:1 'vim' C-m
    tmux send-keys -t ProjectA:2.0 'mysql -u root -pmypass ProjectA' C-m
    tmux send-keys -t ProjectA:3.0 'tail -f /srv/www/ProjectA/system/var/log/messages.log' C-m
    tmux send-keys -t ProjectA:3.1 'tail -f /var/log/httpd/error_log' C-m

    tmux select-window -t ProjectA:0

fi

tmux -2 attach-session -t ProjectA