guest@dotshare [~/groups/terms/tmux] $ ls MONO-TMUX-scripts/ | cat

MONO TMUX +scripts (scrot)

Gargantua Sep 05, 2018 (terms/tmux)

tmux.conf(raw, dl)

SCROT

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
set-option -g history-limit 50000
set-option -g default-shell /usr/bin/zsh

# bind prefix + b to list tmux windows 

bind b split-window "tmux lsw | percol --initial-index $(tmux lsw | awk '/active.$/ {print NR-1}') | cut -d':' -f 1 | tr -d '\n' | xargs -0 tmux select-window -t"
bind B split-window "tmux ls | percol --initial-index $(tmux ls | awk \"/^$(tmux display-message -p '#{session_name}'):/ {print NR-1}\") | cut -d':' -f 1 | tr -d '\n' | xargs -0 tmux switch-client -t"


bind | split-window -h
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %
bind | split-window -h

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

bind r source-file ~/.tmux.conf

# If current tmux pane is emacs, navigate emacs windows, other wise just use 
# M-directions for moving 

bind -n M-Left run "(tmux display-message -p '#{pane_current_command}' | grep -iq emacs && tmux send-keys M-Left) || tmux select-pane -L"
bind -n M-Right run "(tmux display-message -p '#{pane_current_command}' | grep -iq emacs && tmux send-keys M-Right) || tmux select-pane -R"
bind -n M-Up run "(tmux display-message -p '#{pane_current_command}' | grep -iq emacs && tmux send-keys M-Up) || tmux select-pane -U"
bind -n M-Down run "(tmux display-message -p '#{pane_current_command}' | grep -iq emacs && tmux send-keys M-Down) || tmux select-pane -D"

bind -n M-S-Left run "(tmux swap-pane -U)"
bind -n M-S-Right run "(tmux swap-pane -D)"

unbind-key C-b
set -g prefix 'C-\'

bind-key 'C-\' send-prefix

set -g mouse on
set-option -g allow-rename off
set-window-option -g automatic-rename off


######################
### DESIGN CHANGES ###
######################

# panes
set -g pane-border-fg black
set -g pane-active-border-fg brightred

## Status bar design
# status line
#set -g status-utf8 on
set -g status-justify left
set -g status-bg default
set -g status-fg colour12
set -g status-interval 2

# messaging
set -g message-fg black
set -g message-bg yellow
set -g message-command-fg blue
set -g message-command-bg black

#window mode
setw -g mode-bg colour5
setw -g mode-fg colour0

# window status
setw -g window-status-format " #F#I:#W#F "
setw -g window-status-current-format " #F#I:#W#F "
setw -g window-status-format "#[fg=magenta]#[bg=black] #I #[bg=black]#[fg=colour8] #W "
setw -g window-status-current-format "#[bg=brightmagenta]#[fg=colour8] #I #[fg=colour8]#[bg=colour14] #W "
setw -g window-status-current-bg colour0
setw -g window-status-current-fg colour11
setw -g window-status-current-attr dim
setw -g window-status-bg green
setw -g window-status-fg black
setw -g window-status-attr reverse

# Info on left (I don't have a session display for now)
set -g status-left ''

# loud or quiet?
set-option -g visual-activity off
set-option -g visual-bell off
set-option -g visual-silence off
set-window-option -g monitor-activity off
set-option -g bell-action none

set -g default-terminal "xterm-256color"
set-option -g terminal-overrides "xterm-256color:Tc"

#set -g default-terminal "tmux-256color"

# The modes {
setw -g clock-mode-colour colour135
setw -g mode-attr bold
setw -g mode-fg colour196
setw -g mode-bg colour238

# }
# The panes {

set -g pane-border-bg "#111111"
set -g pane-border-fg colour235
    			
set -g pane-active-border-bg "#111111"
set -g pane-active-border-fg "#777788"

# }
# The statusbar {

set -g status-position bottom
set -g status-bg colour234
set -g status-fg colour137
set -g status-attr dim
set -g status-left ''
set -g status-right '#[fg=colour233,bg=colour241,bold] %d/%m #[fg=colour233,bg=colour245,bold] %H:%M:%S '
set -g status-right-length 50
set -g status-left-length 20

setw -g window-status-current-fg colour81
setw -g window-status-current-bg colour238
setw -g window-status-current-attr bold
setw -g window-status-current-format ' #I#[fg=colour250]:#[fg=colour255]#W#[fg=colour50]#F '

setw -g window-status-fg colour138
setw -g window-status-bg colour235
setw -g window-status-attr none
setw -g window-status-format ' #I#[fg=colour237]:#[fg=colour250]#W#[fg=colour244]#F '

setw -g window-status-bell-attr bold
setw -g window-status-bell-fg colour255
setw -g window-status-bell-bg colour1




# The messages {

set -g message-attr bold
set -g message-fg colour232
set -g message-bg colour166

# }

CLICK TO VIEW

x

tmux_term_or_pane.sh(raw, dl)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/bash
# THIS SCRIPT ASSUMES YOU HAVE 'tmux_update_focus.sh' in your $PATH! 
#
# * Options:
# Max number of panes in each tmux window, minus one:
MAXNUM=3

# Your prefered term, window title must be the same for wmctrl to be
# able to # select the window:
TERM_CLIENT="xfce4-terminal" 

# The option that tells your term you want to execute a command within
# it:
TERM_LAUNCH_OPTION="-e"

# * Code: 
if test $(wmctrl -l | grep $TERM_CLIENT 2>&1 | wc -l) -eq 1; then
    a=$(tmux list-panes | wc -l)

    if [ "$a" -gt $MAXNUM ]; then # Too many windows, do nothing.
	tmux_update_focus.sh
	wmctrl -vR $TERM_CLIENT;
	exit
    fi

    # otherwise, create a window and alternate between vertical/horz:

    if [ $((a%2)) -eq 0 ]; then
	tmux split-window -v;
    else
	tmux split-window -h;
    fi

    tmux_update_focus.sh
    wmctrl -vR $TERM_CLIENT; 

else
    tmux new-session -d -s 'manage'

    workspaces=$(wmctrl -d | cut -d ' ' -f 13-)
    idex=0

    for i in $workspaces; do
	tmux new-window -t manage:$idex -n $i
	let idex=${idex}+1
    done

    tmux_update_focus.sh
    
    $TERM_CLIENT $TERM_LAUNCH_OPTION 'tmux attach -t manage';

fi
 

x

tmux_update_focus.sh(raw, dl)

1
2
3
4
5
6
#!/usr/bin/bash

workspaces=($(wmctrl -d | cut -d ' ' -f 13-)) # get desktop names 
selected=$(xdotool get_desktop) # currently selected desktop id

tmux select-window -t manage:${workspaces[$selected]} & 
 

x

Notes

What is this?

This is not your typical tmux configuration, it contains two simple
scripts which will make your life a lot easier if you like to keep one
term with tmux open. If you have a problem with starting too many
terms, this is for you.

Here’s what the scripts do:

tmux_term_or_pane.sh

Basically, this script will give you a term. You want to bind it to
Super-Return or however you usually launch your terminal.

Does a terminal window exist in the current X session? If so, give
focus to it and split a new pane (unless we’re at MAXNUM panes). This
limits you to a single term window which you can minimize or close and
always get back with your Super-Return keybind.

If a term does not exist, then *start a new session called “manage”
and create tmux windows corresponding to our virtual desktops
*. This
is the important thing.

For example, in my screenshots, I have 4 Openbox desktops called zsh,
web1, web2, and code. The script will get those names using wmctrl -l
and create tmux windows with the corresponding names.

If a tmux session exists but a term does not, just create a term and
attach it to the ‘manage’ session.

The default term client is xfce4-terminal, but you can change it by
changing the TERM_CLIENT variable

tmux_update_focus.sh

Get the virtual desktop names from the window manager then update our
tmux session to select the corresponding tmux window with the same
name.

You want to call this script every time you change virtual desktops,
so tmux will update its focussed window. For example, in my
openbox rc.xml, I have the following:

<keybind key="C-W-Left">
  <action name="DesktopLeft"/>
  <action name="Execute">
    <command>tmux_update_focus.sh</command>
  </action>
</keybind>

Why do things this way?

Organization and minimalism. In the past, I used to simply bind
Super-Return to xfce4-terminal or urxvt, over time, I realized having
10 terms on each desktop is the wrong strategy.

This way, you can have tmux windows corresponding to the same things
you’re doing on your window manager. For example, in my “web1”
desktop, I can have Firefox and a chat client, and weechat running in
my “manage:web1” tmux window. I then set the terminal window to “show
on all desktops”, when I switch desktops, tmux will automatically go
to the web1 window and show me weechat with one keystroke, without me
having to inform tmux of the change. When I switch back to “code”,
it will switch back to the compilation results or whatever I’m doing
on the code tab.

You can close the terminal and get back to exactly what you were doing
on the corresponding desktop with Super-Return.

If your term window is on another desktop or hidden behind a bunch of
other windows, Super-Return will give it focus.

Installation

  1. Put tmux_term_or_pane.sh and tmux_update_focus.sh somewhere in your $PATH, chmod +x them.
  2. Edit tmux_term_or_pane.sh to match your prefered terminal client, the default is xfce4-terminal.
  3. Bind tmux_term_or_pane.sh to your prefered “launch terminal” keybind in your window manager.
  4. Put tmux_update_focus.sh in your window manager’s “change desktop” hook, or bind it to the same keys you use to switch virtual desktops, ie. ALT-CTRL-Left or ALT-CTRL-Right.

Dependencies

  • wmctrl
  • xdotool