LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-22-2014, 08:56 PM   #1
jeffguy77
LQ Newbie
 
Registered: May 2008
Posts: 17

Rep: Reputation: 4
Anyone use tmux? I love it. Here's my profile and scripts. Share yours.


I have been using tmux for a while now so I could have tabs and split windows when I remote SSH into my machines for work... and I love it!

http://tmux.sourceforge.net/

Here's what it look likes:


I thought it would be nice if people wanted to share their setups and scripts.

Here's my ~/.tmux.conf file:
Code:
# ---------------------
# Configuration
# ---------------------

# use UTF8 unicode
set -g utf8
set-window-option -g utf8 on
set -g status-utf8 on

# make tmux display things in 256 colors
# local .bashrc needs to have: export TERM=xterm-256color
set -g default-terminal "xterm-256color"

# set scrollback history to 10000 (10k)
set -g history-limit 10000

# shorten command delay
set -sg escape-time 1

# visual notification of activity in other windows
setw -g monitor-activity on
set -g visual-activity on

# set window and pane index to 1 (0 by default)
set-option -g base-index 1
setw -g pane-base-index 1

# ---------------------
# New Keyboard Bindings
# ---------------------

# set Ctrl-a as the default prefix key combination
# and unbind C-b to free it up
set -g prefix C-a
unbind C-b

# use send-prefix to pass C-a through to application
bind C-a send-prefix

# rebind clear screen with Ctrl-l
bind C-l send-keys 'C-l'

# reload ~/.tmux.conf using PREFIX r
bind r source-file ~/.tmux.conf \; display "Config reloaded"

# use PREFIX | to split window horizontally and PREFIX - to split vertically
bind \ split-window -h
bind | split-window -h
bind - split-window -v
bind _ split-window -v

# make the current window the first window
bind T swap-window -t 1

# setup tab to switch windows
bind tab last-window

# map pane switching with ALT+Arrow keys
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# toggle zoom
bind b break-pane

# join windows
bind-key j command-prompt -p "join pane from:"  "join-pane -s '%%'"
bind-key s command-prompt -p "send pane to:"  "join-pane -t '%%'"

# toggle mouse on
unbind-key -n F8
bind-key -n F8 \
	set -g mode-mouse on \;\
	set -g mouse-resize-pane on \;\
	set -g mouse-select-pane on \;\
	set -g mouse-select-window on \;\
	display 'Mouse: ON'

# 9/16/2014 5:04:08 PMtoggle mouse off
unbind-key -n F9
bind-key -n F9 \
	set -g mode-mouse off \;\
	set -g mouse-resize-pane off \;\
	set -g mouse-select-pane off \;\
	set -g mouse-select-window off \;\
	display 'Mouse: OFF'

# ---------------------
# Mouse
# ---------------------
# switch panes by using the mouse (turns off the ability to select text for local clipboard copy!)
# setw -g mouse-utf8 on
# set -g mode-mouse on
# setw -g mode-mouse on
# set -g terminal-overrides 'xterm*:smcup@:rmcup@'
# set-option -g mouse-select-pane on
# setw -g mouse-select-window on
# setw -g mouse-resize-pane on

# OR explicitly disable mouse control
setw -g mode-mouse off
set -g mouse-select-pane off
set -g mouse-resize-pane off
set -g mouse-select-window off

# ---------------------
# Copy & Paste
# ---------------------
# setup reattach-to-user-namespace for copy and paste
# set-option -g default-command "reattach-to-user-namespace -l $SHELL"
# set-window-option -g automatic-rename on

# ---------------------
# VIM
# ---------------------
# 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"

# bind y run 'tmux save-buffer - | reattach-to-user-namespace pbcopy '
# bind C-y run 'tmux save-buffer - | reattach-to-user-namespace pbcopy '

# set copy-mode settings
# bind [ copy-mode
# bind -t vi-copy v begin-selection
# bind -t vi-copy y copy-selection
# bind -t vi-copy V rectangle-toggle
# bind ] paste-buffer

# map Vi movement keys as pane movement keys
# bind h select-pane -L
# bind j select-pane -D
# bind k select-pane -U
# bind l select-pane -R

# and use C-h and C-l to cycle thru panes
# bind -r C-h select-window -t :-
# bind -r C-l select-window -t :+

# resize panes using PREFIX H, J, K, L
# bind H resize-pane -L 5
# bind J resize-pane -D 5
# bind K resize-pane -U 5
# bind L resize-pane -R 5

# ----------------------
# Pane Highlighting
# ----------------------

# Options: black red yellow green blue magenta cyan white
# or write "colour\d+", e.g. "colour5" or "colour170"

# set pane colors - hilight the active pane

# inactive border
set-option -g pane-border-fg colour235        # dark gray
set-option -g pane-border-bg colour233        # super dark gray

# active border
set-option -g pane-active-border-fg white     # white
set-option -g pane-active-border-bg colour233 # super dark gray

# ----------------------
# Status Bar
# -----------------------
set-option -g status on                # turn the status bar on
set -g status-interval 1               # set update frequencey (default 15 seconds)
set -g status-justify centre           # center window list for clarity
# set-option -g status-position top    # position the status bar at top of screen

# set color for status bar
# set-option -g status-attr dim
set-option -g status-bg colour234
set-option -g status-fg white

# status left options
set-option -g status-left-length 80
set-option -g status-left "#[fg=colour240](#[bg=default,fg=colour255]#H#[fg=colour240])-(#[bg=default,fg=colour39]#S#[fg=colour240])"

# window list options
setw -g automatic-rename on
set-window-option -g window-status-format '#[fg=colour51,dim]#I#[fg=colour32]:#[fg=colour253]#W#[fg=colour253,dim]#F'
set-window-option -g window-status-current-format '#[bg=colour25,fg=colour51,bold]#I#[bg=colour25,fg=colour32]:#[fg=colour230]#W#[fg=dim]#F'
set -g base-index 1

# status right options
set-option -g status-right-length 80
set -g status-right "#[fg=colour240](#[fg=colour39]%a %b %-m %Y#[fg=colour240])-(#[fg=colour255]%-I:%M:%S%P#[fg=colour240])"

# ----------------------
# Scripts
# -----------------------
# bind h source-file ~/.tmux/yourfile
I have most of the VIM lines commented out at the moment, but if you use VIM, just put them back.

This makes CTRL-A the modifier and makes F8 and F9 turn the mouse on and off (the only two function keys that work in all terminals in all operating systems).

Also, you'll want to add this to your ~/.bashrc file:

Code:
# Make sure 256 color terminals are enabled
export TERM=xterm-256color
And, optionally, you can add this code into your prompt (export PROMPT_COMMAND) to change the tab names automatically to the folder you are in:
Code:
# If we are in tmux, change the window name to our current path
# If tmux is installed
if [ "$(type -t tmux)" = "file" ]; then
	# If we are in tmux
	if [ ! -z "$TMUX" ]; then
		tmux rename-window ${PWD//*\//};
	fi
fi
I also have some nifty scripts in my ~/.tmux directory that I'm not using at the moment but here they are:

battery.sh
Code:
if [ -d /sys/class/power_supply/BAT0 ];
then
    now=`cat /sys/class/power_supply/BAT0/energy_now`
    full=`cat /sys/class/power_supply/BAT0/energy_full`
    out=`echo $now/$full*100 | bc -l | cut -c 1-5`
    printf "%.f%% | " $out
else
    echo ""
fi
cpu.sh
Code:
#!/bin/bash

case "$OSTYPE" in
    linux-gnu)
        CPU=`eval $(awk '/^cpu /{print "previdle=" $5 "; prevtotal=" $2+$3+$4+$5 }' /proc/stat); sleep 0.4; eval $(awk '/^cpu /{print "idle=" $5 "; total=" $2+$3+$4+$5 }' /proc/stat); intervaltotal=$((total-${prevtotal:-0})); echo "$((100*( (intervaltotal) - ($idle-${previdle:-0}) ) / (intervaltotal) ))"`

        FREE_MEM=`free | awk '/buffers\/cache/{print (100 - ($4/($3+$4) * 100.0));}'`

        printf "CPU:%.f%% Mem:%.f%%" $CPU $FREE_MEM
        ;;
esac
cpu_avg_temp.sh
Code:
case "$OSTYPE" in
    linux-gnu)
            if which sensors > /dev/null; then
                sensors | grep Core | awk '{print $3;}' | grep -oEi '[0-9]+.[0-9]+' | awk '{total+=$1; count+=1} END {print total/count,"C"}'
            else
                ""
            fi
        ;;
esac
disk.sh
Code:
#!/bin/bash

case "$OSTYPE" in
    linux-gnu)
        io_line_count=`iostat -d -x -m | wc -l` ;
        iostat -d -x -m 1 2 -z | tail -n +$io_line_count | grep -e "^sd[a-z].*" | awk 'BEGIN{rsum=0; wsum=0}{ rsum+=$6; wsum+=$7} END {print "IO:" rsum " " wsum ""}'
        ;;
esac
net_connections.sh
Code:
#!/bin/bash

awk 'END {print NR}' /proc/net/tcp
net_speed.sh
Code:
#!/bin/bash

case "$OSTYPE" in
    linux-gnu)
        if [ -z "$1" ]; then
            echo
            echo usage: $0 network-interface
            echo
            echo e.g. $0 eth0
            echo
            exit
        fi

        IF=$1

        R1=`cat /sys/class/net/$1/statistics/rx_bytes`
        T1=`cat /sys/class/net/$1/statistics/tx_bytes`
        sleep 1
        R2=`cat /sys/class/net/$1/statistics/rx_bytes`
        T2=`cat /sys/class/net/$1/statistics/tx_bytes`
        TBPS=`expr $T2 - $T1`
        RBPS=`expr $R2 - $R1`
        TKBPS=`expr $TBPS / 1024`
        RKBPS=`expr $RBPS / 1024`
        printf "%s:%d %d " $1 $RKBPS $TKBPS
        ;;
esac
Lastly, when you want to load tmux as your "shell" or want to make an alias to load it, use this:

Code:
tmux a -t Session || tmux new -s Session
That will connect to the session if it's created or create it if it doesn't exist yet.

For more information on tmux, this is a great tutorial:
http://lukaszwrobel.pl/blog/tmux-tut...windows-easily
 
Old 09-22-2014, 11:15 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
I am a Tmux user myself - totally could not live without it!

My ~/.tmux.conf

Code:
#~/.tmux.conf skel
#Enable next lines to use ^a instead of ^b as the control sequence start char
set-option -g prefix C-a
unbind C-b

#Bind C-a C-a to open last window
bind-key C-a last-window

#hilite current window
set-window-option -g window-status-current-bg red
set-window-option -g window-status-current-fg yellow

#Keep windows open after opening script is finished
setw -g remain-on-exit on

#Status message persistence in ms
set-option -g display-time 2000

#Start numbering of windows from 1
set -g base-index 1

#If not set, all windows are constrained to size of smallest client window
setw -g aggressive-resize on

#Allow nested sessions, sends C-a prefix to remote
bind-key a send-prefix

#Set vi selection mode
setw -g mode-keys vi
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection


# Copy into clipboard
bind C-c choose-buffer "run \"tmux save-buffer -b '%%' - | xsel -i -b\""
# Paste from clipboard
bind C-v run "xsel -o -b | tmux load-buffer - && tmux paste-buffer"

# Copy into primary
bind C-y choose-buffer "run \"tmux save-buffer -b '%%' - | xsel -i\""
# Paste from primary
bind C-p run "xsel -o | tmux load-buffer - && tmux paste-buffer"

#Cause tmux to re-read the config file after it is started
bind r source-file ~/.tmux.conf
Pretty basic, but I add Vi select key-bindings, xsel for clipboard actions and of course change the bind-key to Ctl-a.

For my own use I have a script that runs at login which works down a list of tmux session startup scripts in various project directories. When one is executable it starts that session which allows me to control my environment based on active projects. Each session start script will set paths, open shells, database clients, etc. for that project. It then starts and attaches a top level "Admin" session.

Code:
*** ~/stmux.sh (example stanza) ***
#start active project sessions...
...
#Configure ~/russian-text sessions
#Русски
if [ -x ~/russian-text/DEVEL/tmux/tmux_session.sh ]; then
. ~/russian-text/DEVEL/tmux/tmux_session.sh
fi
...
#Start and attach Admin session...
tmux has-session -t Admin
if [ $? != 0 ]; then
tmux new-session -d -s Admin -n '~/' 'cd ~/; bash -i; crontab -l'
#Windows will be added in the reverse order listed here (push)
... more windows ...
fi

tmux select-window -t Admin:1
tmux -2 attach-session -t Admin
I then Ctl-a s to select a project, Ctl-a # for window, and get to work...

It is my IDE of choice! Initial startup state screenshot below...
Attached Thumbnails
Click image for larger version

Name:	tmux_dm.png
Views:	240
Size:	136.2 KB
ID:	16491  
 
Old 09-23-2014, 12:18 AM   #3
jeffguy77
LQ Newbie
 
Registered: May 2008
Posts: 17

Original Poster
Rep: Reputation: 4
Astrogeek, nice. I'll be using some of the things from your configuration. I especially like the idea of the double control-A for the last window.

---
I know some people swear off a mouse when using tmux, but one thing I didn't figure out for a long time was getting the mouse to click through into the programs while using tmux. For example, if you use midnight commander or links you can click on the interface. To make mouse clicks go to the programs you have to tell tmux to act like xterm-256color in the .tmux.conf file:

set -g default-terminal "xterm-256color"

And I'm not sure if it's necessary, but my ~/.bashrc file also has:

export TERM=xterm-256color

Just figured that out recently. Thought it might help somebody else.

Last edited by jeffguy77; 09-23-2014 at 04:33 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Scripts in profile.d automatically executing jamesdunn72 Linux - Newbie 3 07-16-2014 03:33 PM
rc scripts, profile.d, and startup mh983 Slackware 2 08-04-2006 06:20 PM
How i can share my profile in DC++ & linuxdcpp? tsialiam Linux - Software 1 07-23-2006 06:19 AM
Thunderbird profile folder in SMB Share equisol Linux - Software 3 04-24-2006 03:32 PM
Startup (rc, profile, etc) scripts NewRedHatter Linux - Newbie 1 08-31-2002 12:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration