├── .gitignore ├── Makefile ├── LICENSE ├── keybindings.conf ├── commands.conf ├── tmux-modal.tmux └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .kbd.conf 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /usr/bin/env bash -o pipefail 2 | 3 | CONF_FILE := keybindings.conf 4 | SCRIPT := tmux-modal.tmux 5 | 6 | .PHONY: all 7 | all: 8 | 9 | .PHONY: test 10 | test: all 11 | ./$(SCRIPT) 12 | 13 | .PHONY: clean 14 | clean: 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Waqar Hameed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /keybindings.conf: -------------------------------------------------------------------------------- 1 | # These are the default keybindings for tmux-modal. This file will be used 2 | # (sourced) by the main script `tmux-modal.tmux` when tmux loads. Users can 3 | # however load their own custom keybindings file by copying this file, make any 4 | # changes and then tell tmux-modal to load the custom file instead with tmux 5 | # option `modal-keybindings-conf`. For example, put this is in your 6 | # `.tmux.conf`: 7 | # 8 | # set -g @modal-keybindings-conf /path/to/my-tmux-modal-keybindings.conf 9 | # 10 | # The syntax is the same as for tmux keybindings: 11 | # 12 | # KBD_CMD_EXIT=Escape 13 | # KBD_WIN_RESIZE_UP=S-Up 14 | # KBD_GOTO_WIN_NEXT=C-n 15 | # KBD_GOTO_SESS_PREV=M-p 16 | # 17 | # This binds `Esc`, `Shift-Up`, `Ctrl-n` and `Alt-p`, to `KBD_CMD_EXIT` (exit 18 | # modal command mode), `KBD_WIN_RESIZE_UP` (window resize up), 19 | # `KBD_GOTO_WIN_NEXT` (go to next window) and `KBD_GOTO_SESS_PREV` (go to 20 | # previous session), respectively. 21 | # 22 | # Each keybinding variable below has an accompanying comment line. Comment lines 23 | # that starts with a single '#' are "root" commands. Two '#'s means that this is 24 | # a "sub-command" to the previous root command. Three '#'s means that it is a 25 | # sub-command to the previous sub-command, and so on. If a 'q' is followed by a 26 | # '#' in the comment line, it indicates that this command is "sticky", i.e. it 27 | # needs to be exited explicitly (with `KBD_QUIT`). 28 | # 29 | # For example, `KBD_WIN_GOTO_1` is a sub-command to `KBD_WIN` and is used with 30 | # `w 1`. `KBD_PASTE` can be used directly after entering the modal mode, i.e. 31 | # `M-m y`. While `KBD_WIN_RESIZE_RIGHT` is used with `w r l` and is a sticky 32 | # command (since `KBD_WIN_RESIZE`'s comment line is `##q`) and must be exited 33 | # with 'q' (see the `README` for more details). 34 | 35 | # Enter modal command mode. 36 | KBD_CMD=M-m 37 | 38 | # Exit modal command mode. 39 | KBD_CMD_EXIT=M-m 40 | 41 | # Quit command. This is used to exit command modes that don't require prefix 42 | # (e.g. "w r" used for resizing panes). 43 | KBD_QUIT=q 44 | 45 | # Enter copy mode. 46 | KBD_COPY_MODE=c 47 | 48 | # Paste buffer (e.g. from copy mode). 49 | KBD_PASTE=y 50 | 51 | # Open the tmux command prompt. 52 | KBD_CMD_PROMPT=: 53 | 54 | # Window command prefix. 55 | KBD_WIN=w 56 | 57 | ## Select window 0 (window command alias for KBD_GOTO_WIN_0). 58 | KBD_WIN_GOTO_0=0 59 | 60 | ## Select window 1 (window command alias for KBD_GOTO_WIN_1). 61 | KBD_WIN_GOTO_1=1 62 | 63 | ## Select window 2 (window command alias for KBD_GOTO_WIN_2). 64 | KBD_WIN_GOTO_2=2 65 | 66 | ## Select window 3 (window command alias for KBD_GOTO_WIN_3). 67 | KBD_WIN_GOTO_3=3 68 | 69 | ## Select window 4 (window command alias for KBD_GOTO_WIN_4). 70 | KBD_WIN_GOTO_4=4 71 | 72 | ## Select window 5 (window command alias for KBD_GOTO_WIN_5). 73 | KBD_WIN_GOTO_5=5 74 | 75 | ## Select window 6 (window command alias for KBD_GOTO_WIN_6). 76 | KBD_WIN_GOTO_6=6 77 | 78 | ## Select window 7 (window command alias for KBD_GOTO_WIN_7). 79 | KBD_WIN_GOTO_7=7 80 | 81 | ## Select window 8 (window command alias for KBD_GOTO_WIN_8). 82 | KBD_WIN_GOTO_8=8 83 | 84 | ## Select window 9 (window command alias for KBD_GOTO_WIN_9). 85 | KBD_WIN_GOTO_9=9 86 | 87 | ## Select window with tree view (window command alias for KBD_GOTO_WIN_TREE). 88 | KBD_WIN_GOTO_TREE=t 89 | 90 | ## Select window with index (window command alias for KBD_GOTO_WIN_INDEX). 91 | KBD_WIN_GOTO_INDEX=i 92 | 93 | ## Select left pane. 94 | KBD_WIN_PANE_LEFT=h 95 | 96 | ## Select right pane. 97 | KBD_WIN_PANE_RIGHT=l 98 | 99 | ## Select above pane. 100 | KBD_WIN_PANE_UP=k 101 | 102 | ## Select below pane. 103 | KBD_WIN_PANE_DOWN=j 104 | 105 | ## Delete window pane. 106 | KBD_WIN_PANE_DEL=d 107 | 108 | ## Select previous window (window command alias for KBD_GOTO_WIN_PREV). 109 | KBD_WIN_PREV=H 110 | 111 | ## Select next window (window command alias for KBD_GOTO_WIN_NEXT). 112 | KBD_WIN_NEXT=L 113 | 114 | ## Delete window. 115 | KBD_WIN_DEL=D 116 | 117 | ## Create new window. 118 | KBD_WIN_CREATE=c 119 | 120 | ## Select last window (window command alias for KBD_GOTO_WIN_LAST). 121 | KBD_WIN_LAST=o 122 | 123 | ## Zoom pane. 124 | KBD_WIN_ZOOM=z 125 | 126 | ## Break pane. 127 | KBD_WIN_BREAK=b 128 | 129 | ## Display pane numbers. 130 | KBD_WIN_NR=n 131 | 132 | ## Rename window. 133 | KBD_WIN_RENAME=, 134 | 135 | ##q Pane command prefix (same bindings as KBD_WIN but without the prefix). 136 | KBD_WIN_PANE=w 137 | 138 | ## Split command prefix. 139 | KBD_WIN_SPLIT=s 140 | 141 | ### Split window pane right. 142 | KBD_WIN_SPLIT_RIGHT=l 143 | 144 | ### Split window pane down. 145 | KBD_WIN_SPLIT_DOWN=j 146 | 147 | ## Move command prefix. 148 | KBD_WIN_MOVE=m 149 | 150 | ### Move window pane up. 151 | KBD_WIN_MOVE_UP=k 152 | 153 | ### Move window pane down. 154 | KBD_WIN_MOVE_DOWN=j 155 | 156 | ## Arrange command prefix. 157 | KBD_WIN_ARRANGE=a 158 | 159 | ### Arrange window layout 1 ("even-horizontal"). 160 | KBD_WIN_ARRANGE_1=1 161 | 162 | ### Arrange window layout 2 ("even-vertical"). 163 | KBD_WIN_ARRANGE_2=2 164 | 165 | ### Arrange window layout 3 ("main-horizontal"). 166 | KBD_WIN_ARRANGE_3=3 167 | 168 | ### Arrange window layout 4 ("main-vertical"). 169 | KBD_WIN_ARRANGE_4=4 170 | 171 | ##q Resize command prefix. 172 | KBD_WIN_RESIZE=r 173 | 174 | ### Resize pane left one step. 175 | KBD_WIN_RESIZE_LEFT=h 176 | 177 | ### Resize pane right one step. 178 | KBD_WIN_RESIZE_RIGHT=l 179 | 180 | ### Resize pane down one step. 181 | KBD_WIN_RESIZE_DOWN=j 182 | 183 | ### Resize pane up one step. 184 | KBD_WIN_RESIZE_UP=k 185 | 186 | ### Resize pane left multiple steps. 187 | KBD_WIN_RESIZE_MULTI_LEFT=H 188 | 189 | ### Resize pane right multiple steps. 190 | KBD_WIN_RESIZE_MULTI_RIGHT=L 191 | 192 | ### Resize pane down multiple steps. 193 | KBD_WIN_RESIZE_MULTI_DOWN=J 194 | 195 | ### Resize pane up multiple steps. 196 | KBD_WIN_RESIZE_MULTI_UP=K 197 | 198 | # Session command prefix. 199 | KBD_SESS=s 200 | 201 | ## Detach session. 202 | KBD_SESS_DETACH=d 203 | 204 | ## Select previous session (session command alias for KBD_GOTO_SESS_PREV). 205 | KBD_SESS_PREV=h 206 | 207 | ## Select next session (session command alias for KBD_GOTO_SESS_NEXT). 208 | KBD_SESS_NEXT=l 209 | 210 | ## Select session with a tree view (session command alias for 211 | ## KBD_GOTO_SESS_TREE). 212 | KBD_SESS_TREE=t 213 | 214 | ## Delete session. 215 | KBD_SESS_DEL=D 216 | 217 | ## Rename session. 218 | KBD_SESS_RENAME=, 219 | 220 | # "Go to" command prefix. 221 | KBD_GOTO=g 222 | 223 | ## Go to window command prefix. 224 | KBD_GOTO_WIN=w 225 | 226 | ### Go to window 0. 227 | KBD_GOTO_WIN_0=0 228 | 229 | ### Go to window 1. 230 | KBD_GOTO_WIN_1=1 231 | 232 | ### Go to window 2. 233 | KBD_GOTO_WIN_2=2 234 | 235 | ### Go to window 3. 236 | KBD_GOTO_WIN_3=3 237 | 238 | ### Go to window 4. 239 | KBD_GOTO_WIN_4=4 240 | 241 | ### Go to window 5. 242 | KBD_GOTO_WIN_5=5 243 | 244 | ### Go to window 6. 245 | KBD_GOTO_WIN_6=6 246 | 247 | ### Go to window 7. 248 | KBD_GOTO_WIN_7=7 249 | 250 | ### Go to window 8. 251 | KBD_GOTO_WIN_8=8 252 | 253 | ### Go to window 9. 254 | KBD_GOTO_WIN_9=9 255 | 256 | ### Go to window with tree view. 257 | KBD_GOTO_WIN_TREE=t 258 | 259 | ### Go to window with index. 260 | KBD_GOTO_WIN_INDEX=i 261 | 262 | ### Go to previous window. 263 | KBD_GOTO_WIN_PREV=h 264 | 265 | ### Go to next window. 266 | KBD_GOTO_WIN_NEXT=l 267 | 268 | ### Go to last window. 269 | KBD_GOTO_WIN_LAST=o 270 | 271 | ## Go to session command prefix. 272 | KBD_GOTO_SESS=s 273 | 274 | ### Go to previous session. 275 | KBD_GOTO_SESS_PREV=h 276 | 277 | ### Go to next session. 278 | KBD_GOTO_SESS_NEXT=l 279 | 280 | ### Go to session with tree view. 281 | KBD_GOTO_SESS_TREE=t 282 | -------------------------------------------------------------------------------- /commands.conf: -------------------------------------------------------------------------------- 1 | # These are the default commands executed for the keybindings in tmux-modal. 2 | # This file will be used (sourced) by the main script `tmux-modal.tmux` when 3 | # tmux loads. Users can however load their own custom commands file by copying 4 | # this file, make any changes and then tell tmux-modal to load the custom file 5 | # instead with tmux option `modal-commands-conf`. For example, put this is in 6 | # your `.tmux.conf`: 7 | # 8 | # set -g @modal-commands-conf /path/to/my-tmux-modal-commands.conf 9 | # 10 | # The syntax is the same as for any other Bash file: 11 | # 12 | # CMD_COPY_MODE='copy-mode' 13 | # CMD_WIN_RENAME='command-prompt -I "#W" "rename-window -- \"%%\""' 14 | # CMD_GOTO_WIN_NEXT='select-window -t :+' 15 | # CMD_GOTO_SESS_PREV='switch-client -p' 16 | # 17 | # This redefines the variables for the commands for the keybindings 18 | # `KBD_COPY_MODE`, `KBD_WIN_RENAME`, `KBD_GOTO_WIN_NEXT` and 19 | # `KBD_GOTO_SESS_PREV`, respectively. The variable value is thus the command as 20 | # a Bash string (make sure to escape necessary characters). 21 | # 22 | # Note that the variable names follow the same pattern as the keybindings in 23 | # `keybindings.conf`. See the file `keybindings.conf` for more information. 24 | 25 | # Enter copy mode. 26 | CMD_COPY_MODE='copy-mode' 27 | 28 | # Paste buffer (e.g. from copy mode). 29 | CMD_PASTE='paste-buffer' 30 | 31 | # Open the tmux command prompt. 32 | CMD_CMD_PROMPT='command-prompt' 33 | 34 | # Window command prefix. 35 | 36 | ## Select window 0 (window command alias for CMD_GOTO_WIN_0). 37 | CMD_WIN_GOTO_0='select-window -t :0' 38 | 39 | ## Select window 1 (window command alias for CMD_GOTO_WIN_1). 40 | CMD_WIN_GOTO_1='select-window -t :1' 41 | 42 | ## Select window 2 (window command alias for CMD_GOTO_WIN_2). 43 | CMD_WIN_GOTO_2='select-window -t :2' 44 | 45 | ## Select window 3 (window command alias for CMD_GOTO_WIN_3). 46 | CMD_WIN_GOTO_3='select-window -t :3' 47 | 48 | ## Select window 4 (window command alias for CMD_GOTO_WIN_4). 49 | CMD_WIN_GOTO_4='select-window -t :4' 50 | 51 | ## Select window 5 (window command alias for CMD_GOTO_WIN_5). 52 | CMD_WIN_GOTO_5='select-window -t :5' 53 | 54 | ## Select window 6 (window command alias for CMD_GOTO_WIN_6). 55 | CMD_WIN_GOTO_6='select-window -t :6' 56 | 57 | ## Select window 7 (window command alias for CMD_GOTO_WIN_7). 58 | CMD_WIN_GOTO_7='select-window -t :7' 59 | 60 | ## Select window 8 (window command alias for CMD_GOTO_WIN_8). 61 | CMD_WIN_GOTO_8='select-window -t :8' 62 | 63 | ## Select window 9 (window command alias for CMD_GOTO_WIN_9). 64 | CMD_WIN_GOTO_9='select-window -t :9' 65 | 66 | ## Select window with tree view (window command alias for CMD_GOTO_WIN_TREE). 67 | CMD_WIN_GOTO_TREE='choose-tree -Zw' 68 | 69 | ## Select window with index (window command alias for CMD_GOTO_WIN_INDEX). 70 | CMD_WIN_GOTO_INDEX='command-prompt -p index "select-window -t \":%%\""' 71 | 72 | ## Select left pane. 73 | CMD_WIN_PANE_LEFT='select-pane -L' 74 | 75 | ## Select right pane. 76 | CMD_WIN_PANE_RIGHT='select-pane -R' 77 | 78 | ## Select above pane. 79 | CMD_WIN_PANE_UP='select-pane -U' 80 | 81 | ## Select below pane. 82 | CMD_WIN_PANE_DOWN='select-pane -D' 83 | 84 | ## Delete window pane. 85 | CMD_WIN_PANE_DEL='kill-pane' 86 | 87 | ## Select previous window (window command alias for CMD_GOTO_WIN_PREV). 88 | CMD_WIN_PREV='select-window -t :-' 89 | 90 | ## Select next window (window command alias for CMD_GOTO_WIN_NEXT). 91 | CMD_WIN_NEXT='select-window -t :+' 92 | 93 | ## Delete window. 94 | CMD_WIN_DEL='kill-window' 95 | 96 | ## Create new window. 97 | CMD_WIN_CREATE='new-window' 98 | 99 | ## Select last window (window command alias for CMD_GOTO_WIN_LAST). 100 | CMD_WIN_LAST='last-window' 101 | 102 | ## Zoom pane. 103 | CMD_WIN_ZOOM='resize-pane -Z' 104 | 105 | ## Break pane. 106 | CMD_WIN_BREAK='break-pane' 107 | 108 | ## Display pane numbers. 109 | CMD_WIN_NR='display-panes' 110 | 111 | ## Rename window. 112 | CMD_WIN_RENAME='command-prompt -I "#W" "rename-window -- \"%%\""' 113 | 114 | ## Split command prefix. 115 | 116 | ### Split window pane right. 117 | CMD_WIN_SPLIT_RIGHT='split-window -h' 118 | 119 | ### Split window pane down. 120 | CMD_WIN_SPLIT_DOWN='split-window' 121 | 122 | ## Move command prefix. 123 | 124 | ### Move window pane up. 125 | CMD_WIN_MOVE_UP='swap-pane -U' 126 | 127 | ### Move window pane down. 128 | CMD_WIN_MOVE_DOWN='swap-pane -D' 129 | 130 | ## Arrange command prefix. 131 | 132 | ### Arrange window layout 1 ("even-horizontal"). 133 | CMD_WIN_ARRANGE_1='select-layout even-horizontal' 134 | 135 | ### Arrange window layout 2 ("even-vertical"). 136 | CMD_WIN_ARRANGE_2='select-layout even-vertical' 137 | 138 | ### Arrange window layout 3 ("main-horizontal"). 139 | CMD_WIN_ARRANGE_3='select-layout main-horizontal' 140 | 141 | ### Arrange window layout 4 ("main-vertical"). 142 | CMD_WIN_ARRANGE_4='select-layout main-vertical' 143 | 144 | ##q Resize command prefix. 145 | 146 | ### Resize pane left one step. 147 | CMD_WIN_RESIZE_LEFT='resize-pane -L' 148 | 149 | ### Resize pane right one step. 150 | CMD_WIN_RESIZE_RIGHT='resize-pane -R' 151 | 152 | ### Resize pane down one step. 153 | CMD_WIN_RESIZE_DOWN='resize-pane -D' 154 | 155 | ### Resize pane up one step. 156 | CMD_WIN_RESIZE_UP='resize-pane -U' 157 | 158 | ### Resize pane left multiple steps. 159 | CMD_WIN_RESIZE_MULTI_LEFT='resize-pane -L 5' 160 | 161 | ### Resize pane right multiple steps. 162 | CMD_WIN_RESIZE_MULTI_RIGHT='resize-pane -R 5' 163 | 164 | ### Resize pane down multiple steps. 165 | CMD_WIN_RESIZE_MULTI_DOWN='resize-pane -D 5' 166 | 167 | ### Resize pane up multiple steps. 168 | CMD_WIN_RESIZE_MULTI_UP='resize-pane -U 5' 169 | 170 | # Session command prefix. 171 | 172 | ## Detach session. 173 | CMD_SESS_DETACH='detach-client' 174 | 175 | ## Select previous session (session command alias for CMD_GOTO_SESS_PREV). 176 | CMD_SESS_PREV='switch-client -p' 177 | 178 | ## Select next session (session command alias for CMD_GOTO_SESS_NEXT). 179 | CMD_SESS_NEXT='switch-client -n' 180 | 181 | ## Select session with a tree view (session command alias for 182 | ## CMD_GOTO_SESS_TREE). 183 | CMD_SESS_TREE='choose-tree -Zs' 184 | 185 | ## Delete session. 186 | CMD_SESS_DEL='kill-session' 187 | 188 | ## Rename session. 189 | CMD_SESS_RENAME='command-prompt -I "#S" "rename-session \"%%\""' 190 | 191 | # "Go to" command prefix. 192 | 193 | ## Go to window command prefix. 194 | CMD_GOTO_WIN= 195 | 196 | ### Go to window 0. 197 | CMD_GOTO_WIN_0='select-window -t :0' 198 | 199 | ### Go to window 1. 200 | CMD_GOTO_WIN_1='select-window -t :1' 201 | 202 | ### Go to window 2. 203 | CMD_GOTO_WIN_2='select-window -t :2' 204 | 205 | ### Go to window 3. 206 | CMD_GOTO_WIN_3='select-window -t :3' 207 | 208 | ### Go to window 4. 209 | CMD_GOTO_WIN_4='select-window -t :4' 210 | 211 | ### Go to window 5. 212 | CMD_GOTO_WIN_5='select-window -t :5' 213 | 214 | ### Go to window 6. 215 | CMD_GOTO_WIN_6='select-window -t :6' 216 | 217 | ### Go to window 7. 218 | CMD_GOTO_WIN_7='select-window -t :7' 219 | 220 | ### Go to window 8. 221 | CMD_GOTO_WIN_8='select-window -t :8' 222 | 223 | ### Go to window 9. 224 | CMD_GOTO_WIN_9='select-window -t :9' 225 | 226 | ### Go to window with tree view. 227 | CMD_GOTO_WIN_TREE='choose-tree -Zw' 228 | 229 | ### Go to window with index. 230 | CMD_GOTO_WIN_INDEX='command-prompt -p index "select-window -t \":%%\""' 231 | 232 | ### Go to previous window. 233 | CMD_GOTO_WIN_PREV='select-window -t :-' 234 | 235 | ### Go to next window. 236 | CMD_GOTO_WIN_NEXT='select-window -t :+' 237 | 238 | ### Go to last window. 239 | CMD_GOTO_WIN_LAST='last-window' 240 | 241 | ## Go to session command prefix. 242 | 243 | ### Go to previous session. 244 | CMD_GOTO_SESS_PREV='switch-client -p' 245 | 246 | ### Go to next session. 247 | CMD_GOTO_SESS_NEXT='switch-client -n' 248 | 249 | ### Go to session with tree view. 250 | CMD_GOTO_SESS_TREE='choose-tree -Zs' 251 | -------------------------------------------------------------------------------- /tmux-modal.tmux: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # SPDX-License-Identifier: MIT 4 | 5 | set -euo pipefail 6 | 7 | # Check Bash version. We use features introduced in Bash 4.0. 8 | if [ ${BASH_VERSINFO[0]} -lt 4 ]; then 9 | echo "ERROR: Bash version is too old." \ 10 | "Please upgrade to version 4.0 or newer" 11 | exit 95 12 | fi 13 | 14 | unbind() { 15 | readarray -t BINDINGS <<< $(tmux list-keys | grep -E "$1" || true) 16 | for line in "${BINDINGS[@]}"; do 17 | if [ -z "$line" ]; then 18 | continue 19 | fi 20 | 21 | local lineArr=($line) 22 | local kt=${lineArr[2]} 23 | local kbd=${lineArr[3]} 24 | 25 | # Check if keybinding contains any "special" characters. 26 | case "$kbd" in 27 | '"'*'"') 28 | # Keybindings that contain characters that need to be escaped, 29 | # are quoted in the output of `tmux list-keys`. For example, 30 | # `M-#` and `M-$` are listed as `"M-#"` and `"M-$"`. Before 31 | # giving it to `tmux unbind-key`, we therefore need to strip the 32 | # surrounding quotes. 33 | kbd=$(sed -e 's/^"\(.\+\)"$/\1/' <<< "$kbd") 34 | ;;& 35 | *"\\"*) 36 | # Some characters are escaped as well, e.g. `M-"`, `#` and `$` 37 | # are listed as `"M-\""`, `\#` and `\$`, respectively. 38 | # Backslashes needs to be removed before running `tmux 39 | # unbind-key`. 40 | kbd=$(sed -e "s/\\\\\(.\)/\1/g" <<< "$kbd") 41 | ;;& 42 | *";"*) 43 | # Semicolons are escaped and they need to be that for `tmux 44 | # unbind-key`. Above we removed all backslashes, therefore we 45 | # add it here for this special character. 46 | # TODO: Are there any other special characters that need to be 47 | # escaped? 48 | kbd=$(sed -e "s/;/\\\\;/g" <<< "$kbd") 49 | ;; 50 | esac 51 | 52 | tmux unbind-key -T $kt $kbd 53 | done 54 | } 55 | 56 | parse_yn_opt() { 57 | if [ "$1" != on ] && [ "$1" != off ]; then 58 | echo Option "\"$2\"": Invalid value \""$1"\" 59 | echo Valid values are \"on\" or \"off\" 60 | return 22 61 | fi 62 | } 63 | 64 | KT_PREFIX="ktm" 65 | KT_CMD=$KT_PREFIX-cmd 66 | 67 | # Reset all our key tables before binding. There might be remnant from previous 68 | # bindings, e.g. from KBD_CONF_FILE (if for instance reloading tmux 69 | # configuration file or running this explicitly). 70 | unbind "^bind-key +-T +$KT_PREFIX-" 71 | unbind "^bind-key +-T +root +.+ +set-option key-table $KT_CMD" 72 | 73 | # Parse options. 74 | 75 | # Source default and custom (overriding) keybinding file. 76 | CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 77 | KBD_CONF_FILE="$CURRENT_DIR/keybindings.conf" 78 | 79 | source "$KBD_CONF_FILE" 80 | 81 | KBD_CONF_OPT=@modal-keybindings-conf 82 | KBD_CONF_VAL=$(tmux show-options -g -v -q $KBD_CONF_OPT) 83 | if [ -n "$KBD_CONF_VAL" ]; then 84 | if [ ! -f "$KBD_CONF_VAL" ]; then 85 | echo Option $KBD_CONF_OPT: File \""$KBD_CONF_VAL"\" not found 86 | exit 2 87 | fi 88 | 89 | source "$KBD_CONF_VAL" 90 | fi 91 | 92 | # Source default and custom (overriding) command file. 93 | CMD_CONF_FILE="$CURRENT_DIR/commands.conf" 94 | 95 | source "$CMD_CONF_FILE" 96 | 97 | CMD_CONF_OPT=@modal-commands-conf 98 | CMD_CONF_VAL=$(tmux show-options -g -v -q $CMD_CONF_OPT) 99 | if [ -n "$CMD_CONF_VAL" ]; then 100 | if [ ! -f "$CMD_CONF_VAL" ]; then 101 | echo Option $CMD_CONF_OPT: File \""$CMD_CONF_VAL"\" not found 102 | exit 2 103 | fi 104 | 105 | source "$CMD_CONF_VAL" 106 | fi 107 | 108 | # Check usage of y/n-commands. 109 | YESNO_OPT=@modal-yesno-cmd 110 | YESNO_OPT_VAL=$(tmux show-options -g -v -q $YESNO_OPT) 111 | if [ -n "$YESNO_OPT_VAL" ]; then 112 | echo "WARNING: Option $YESNO_OPT has been deprecated." \ 113 | "Please use option $CMD_CONF_OPT instead" 114 | fi 115 | 116 | if [ -z "$YESNO_OPT_VAL" ]; then 117 | YESNO_OPT_VAL=off 118 | elif ! parse_yn_opt "$YESNO_OPT_VAL" $YESNO_OPT ; then 119 | exit 22 120 | fi 121 | 122 | if [ $YESNO_OPT_VAL == on ]; then 123 | CMD_WIN_PANE_DEL='confirm-before -p "kill-pane #P? (y/n)" kill-pane' 124 | CMD_WIN_DEL='confirm-before -p "kill-window #W? (y/n)" kill-window' 125 | CMD_SESS_DEL='confirm-before -p "kill-session #S? (y/n)" kill-session' 126 | fi 127 | 128 | # Start with modal command keytable. 129 | START_OPT=@modal-on-start 130 | START_OPT_VAL=$(tmux show-options -g -v -q $START_OPT) 131 | if [ -z "$START_OPT_VAL" ]; then 132 | START_OPT_VAL=off 133 | elif ! parse_yn_opt "$START_OPT_VAL" $START_OPT; then 134 | exit 22 135 | fi 136 | 137 | # Always sticky commands. 138 | ALWAYS_STICKY_OPT=@modal-always-sticky 139 | ALWAYS_STICKY_VAL=$(tmux show-options -g -v -q $ALWAYS_STICKY_OPT) 140 | if [ -z "$ALWAYS_STICKY_VAL" ]; then 141 | ALWAYS_STICKY_VAL=off 142 | elif ! parse_yn_opt "$ALWAYS_STICKY_VAL" $ALWAYS_STICKY_OPT; then 143 | exit 22 144 | fi 145 | 146 | # Show command keys (key tables) in status bar. 147 | SHOW_CMD_KEYS_OPT=@modal-show-cmd-keys 148 | SHOW_CMD_KEYS_VAL=$(tmux show-options -g -v -q $SHOW_CMD_KEYS_OPT) 149 | if [ -z "$SHOW_CMD_KEYS_VAL" ]; then 150 | SHOW_CMD_KEYS_VAL=off 151 | elif ! parse_yn_opt "$SHOW_CMD_KEYS_VAL" $SHOW_CMD_KEYS_OPT; then 152 | exit 22 153 | fi 154 | 155 | # Modal mode status bar icon. 156 | KT_CMD_ICON_OPT=@modal-cmd-icon 157 | KT_CMD_ICON_VAL=$(tmux show-options -g -v -q $KT_CMD_ICON_OPT) 158 | if [ -z "$KT_CMD_ICON_VAL" ]; then 159 | KT_CMD_ICON_VAL="[=]" 160 | fi 161 | 162 | # Create keybinding file to be sourced by tmux. 163 | 164 | KBD_FILE="$CURRENT_DIR/.kbd.conf" 165 | 166 | # Copy user's current root keytable to KT_CMD. We filter out any bindings that 167 | # conflict with KBD_CMD. 168 | tmux list-keys -T root | \ 169 | grep -E -v " +root +$KBD_CMD +" | \ 170 | grep -E -v " +set-option key-table $KT_CMD" | \ 171 | sed -e "s/\(^bind-key -T\) root/\1 $KT_CMD/g" > $KBD_FILE 172 | 173 | cat << EOF >> "$KBD_FILE" 174 | 175 | bind-key -T root $KBD_CMD set-option key-table $KT_CMD 176 | bind-key -T $KT_CMD $KBD_CMD_EXIT set-option key-table root 177 | 178 | bind-key -T $KT_CMD $KBD_COPY_MODE $CMD_COPY_MODE 179 | 180 | bind-key -T $KT_CMD $KBD_PASTE $CMD_PASTE 181 | 182 | bind-key -T $KT_CMD $KBD_CMD_PROMPT $CMD_CMD_PROMPT 183 | EOF 184 | 185 | # window. 186 | KT_WIN=$KT_PREFIX-window 187 | KT_WIN_PANE=$KT_PREFIX-window-pane 188 | KT_WIN_SPLIT=$KT_PREFIX-window-split 189 | KT_WIN_MOVE=$KT_PREFIX-window-move 190 | KT_WIN_ARRANGE=$KT_PREFIX-window-arrange 191 | KT_WIN_RESIZE=$KT_PREFIX-window-resize 192 | 193 | BIND_KEY_KBD_WIN="bind-key -T $KT_CMD $KBD_WIN switch-client -T $KT_WIN" 194 | if [ "$ALWAYS_STICKY_VAL" == on ]; then 195 | # Enter sticky window-pane directly. 196 | BIND_KEY_KBD_WIN="bind-key -T $KT_CMD $KBD_WIN" 197 | BIND_KEY_KBD_WIN+=" set-option key-table $KT_WIN_PANE" 198 | fi 199 | 200 | cat << EOF >> "$KBD_FILE" 201 | 202 | $BIND_KEY_KBD_WIN 203 | 204 | bind-key -T $KT_WIN $KBD_WIN_GOTO_0 $CMD_WIN_GOTO_0 205 | bind-key -T $KT_WIN $KBD_WIN_GOTO_1 $CMD_WIN_GOTO_1 206 | bind-key -T $KT_WIN $KBD_WIN_GOTO_2 $CMD_WIN_GOTO_2 207 | bind-key -T $KT_WIN $KBD_WIN_GOTO_3 $CMD_WIN_GOTO_3 208 | bind-key -T $KT_WIN $KBD_WIN_GOTO_4 $CMD_WIN_GOTO_4 209 | bind-key -T $KT_WIN $KBD_WIN_GOTO_5 $CMD_WIN_GOTO_5 210 | bind-key -T $KT_WIN $KBD_WIN_GOTO_6 $CMD_WIN_GOTO_6 211 | bind-key -T $KT_WIN $KBD_WIN_GOTO_7 $CMD_WIN_GOTO_7 212 | bind-key -T $KT_WIN $KBD_WIN_GOTO_8 $CMD_WIN_GOTO_8 213 | bind-key -T $KT_WIN $KBD_WIN_GOTO_9 $CMD_WIN_GOTO_9 214 | bind-key -T $KT_WIN $KBD_WIN_GOTO_TREE $CMD_WIN_GOTO_TREE 215 | bind-key -T $KT_WIN $KBD_WIN_GOTO_INDEX $CMD_WIN_GOTO_INDEX 216 | 217 | bind-key -T $KT_WIN $KBD_WIN_PANE_LEFT $CMD_WIN_PANE_LEFT 218 | bind-key -T $KT_WIN $KBD_WIN_PANE_RIGHT $CMD_WIN_PANE_RIGHT 219 | bind-key -T $KT_WIN $KBD_WIN_PANE_UP $CMD_WIN_PANE_UP 220 | bind-key -T $KT_WIN $KBD_WIN_PANE_DOWN $CMD_WIN_PANE_DOWN 221 | bind-key -T $KT_WIN $KBD_WIN_PANE_DEL $CMD_WIN_PANE_DEL 222 | bind-key -T $KT_WIN $KBD_WIN_PREV $CMD_WIN_PREV 223 | bind-key -T $KT_WIN $KBD_WIN_NEXT $CMD_WIN_NEXT 224 | bind-key -T $KT_WIN $KBD_WIN_DEL $CMD_WIN_DEL 225 | bind-key -T $KT_WIN $KBD_WIN_CREATE $CMD_WIN_CREATE 226 | bind-key -T $KT_WIN $KBD_WIN_LAST $CMD_WIN_LAST 227 | bind-key -T $KT_WIN $KBD_WIN_ZOOM $CMD_WIN_ZOOM 228 | bind-key -T $KT_WIN $KBD_WIN_BREAK $CMD_WIN_BREAK 229 | bind-key -T $KT_WIN $KBD_WIN_NR $CMD_WIN_NR 230 | bind-key -T $KT_WIN $KBD_WIN_RENAME $CMD_WIN_RENAME 231 | 232 | bind-key -T $KT_WIN $KBD_WIN_PANE set-option key-table $KT_WIN_PANE 233 | bind-key -T $KT_WIN $KBD_WIN_SPLIT switch-client -T $KT_WIN_SPLIT 234 | bind-key -T $KT_WIN $KBD_WIN_MOVE switch-client -T $KT_WIN_MOVE 235 | bind-key -T $KT_WIN $KBD_WIN_ARRANGE switch-client -T $KT_WIN_ARRANGE 236 | bind-key -T $KT_WIN $KBD_WIN_RESIZE set-option key-table $KT_WIN_RESIZE 237 | EOF 238 | 239 | # window-pane. 240 | cat << EOF >> "$KBD_FILE" 241 | 242 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_0 $CMD_WIN_GOTO_0 243 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_1 $CMD_WIN_GOTO_1 244 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_2 $CMD_WIN_GOTO_2 245 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_3 $CMD_WIN_GOTO_3 246 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_4 $CMD_WIN_GOTO_4 247 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_5 $CMD_WIN_GOTO_5 248 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_6 $CMD_WIN_GOTO_6 249 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_7 $CMD_WIN_GOTO_7 250 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_8 $CMD_WIN_GOTO_8 251 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_9 $CMD_WIN_GOTO_9 252 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_TREE $CMD_WIN_GOTO_TREE 253 | bind-key -T $KT_WIN_PANE $KBD_WIN_GOTO_INDEX $CMD_WIN_GOTO_INDEX 254 | 255 | bind-key -T $KT_WIN_PANE $KBD_WIN_PANE_LEFT $CMD_WIN_PANE_LEFT 256 | bind-key -T $KT_WIN_PANE $KBD_WIN_PANE_RIGHT $CMD_WIN_PANE_RIGHT 257 | bind-key -T $KT_WIN_PANE $KBD_WIN_PANE_UP $CMD_WIN_PANE_UP 258 | bind-key -T $KT_WIN_PANE $KBD_WIN_PANE_DOWN $CMD_WIN_PANE_DOWN 259 | bind-key -T $KT_WIN_PANE $KBD_WIN_PANE_DEL $CMD_WIN_PANE_DEL 260 | bind-key -T $KT_WIN_PANE $KBD_WIN_PREV $CMD_WIN_PREV 261 | bind-key -T $KT_WIN_PANE $KBD_WIN_NEXT $CMD_WIN_NEXT 262 | bind-key -T $KT_WIN_PANE $KBD_WIN_DEL $CMD_WIN_DEL 263 | bind-key -T $KT_WIN_PANE $KBD_WIN_CREATE $CMD_WIN_CREATE 264 | bind-key -T $KT_WIN_PANE $KBD_WIN_LAST $CMD_WIN_LAST 265 | bind-key -T $KT_WIN_PANE $KBD_WIN_ZOOM $CMD_WIN_ZOOM 266 | bind-key -T $KT_WIN_PANE $KBD_WIN_BREAK $CMD_WIN_BREAK 267 | bind-key -T $KT_WIN_PANE $KBD_WIN_NR $CMD_WIN_NR 268 | bind-key -T $KT_WIN_PANE $KBD_WIN_RENAME $CMD_WIN_RENAME 269 | 270 | bind-key -T $KT_WIN_PANE $KBD_WIN_SPLIT switch-client -T $KT_WIN_SPLIT 271 | bind-key -T $KT_WIN_PANE $KBD_WIN_MOVE switch-client -T $KT_WIN_MOVE 272 | bind-key -T $KT_WIN_PANE $KBD_WIN_ARRANGE switch-client -T $KT_WIN_ARRANGE 273 | bind-key -T $KT_WIN_PANE $KBD_WIN_RESIZE set-option key-table $KT_WIN_RESIZE 274 | 275 | bind-key -T $KT_WIN_PANE $KBD_QUIT set-option key-table $KT_CMD 276 | bind-key -T $KT_WIN_PANE $KBD_CMD_EXIT set-option key-table root 277 | EOF 278 | 279 | # window-split. 280 | cat << EOF >> "$KBD_FILE" 281 | 282 | bind-key -T $KT_WIN_SPLIT $KBD_WIN_SPLIT_RIGHT $CMD_WIN_SPLIT_RIGHT 283 | bind-key -T $KT_WIN_SPLIT $KBD_WIN_SPLIT_DOWN $CMD_WIN_SPLIT_DOWN 284 | EOF 285 | 286 | # window-move. 287 | cat << EOF >> "$KBD_FILE" 288 | 289 | bind-key -T $KT_WIN_MOVE $KBD_WIN_MOVE_UP $CMD_WIN_MOVE_UP 290 | bind-key -T $KT_WIN_MOVE $KBD_WIN_MOVE_DOWN $CMD_WIN_MOVE_DOWN 291 | EOF 292 | 293 | # window-arrange. 294 | cat << EOF >> "$KBD_FILE" 295 | 296 | bind-key -T $KT_WIN_ARRANGE $KBD_WIN_ARRANGE_1 $CMD_WIN_ARRANGE_1 297 | bind-key -T $KT_WIN_ARRANGE $KBD_WIN_ARRANGE_2 $CMD_WIN_ARRANGE_2 298 | bind-key -T $KT_WIN_ARRANGE $KBD_WIN_ARRANGE_3 $CMD_WIN_ARRANGE_3 299 | bind-key -T $KT_WIN_ARRANGE $KBD_WIN_ARRANGE_4 $CMD_WIN_ARRANGE_4 300 | EOF 301 | 302 | # window-resize. 303 | cat << EOF >> "$KBD_FILE" 304 | 305 | bind-key -T $KT_WIN_RESIZE $KBD_WIN_RESIZE_LEFT $CMD_WIN_RESIZE_LEFT 306 | bind-key -T $KT_WIN_RESIZE $KBD_WIN_RESIZE_RIGHT $CMD_WIN_RESIZE_RIGHT 307 | bind-key -T $KT_WIN_RESIZE $KBD_WIN_RESIZE_DOWN $CMD_WIN_RESIZE_DOWN 308 | bind-key -T $KT_WIN_RESIZE $KBD_WIN_RESIZE_UP $CMD_WIN_RESIZE_UP 309 | bind-key -T $KT_WIN_RESIZE $KBD_WIN_RESIZE_MULTI_LEFT $CMD_WIN_RESIZE_MULTI_LEFT 310 | bind-key -T $KT_WIN_RESIZE $KBD_WIN_RESIZE_MULTI_RIGHT \ 311 | $CMD_WIN_RESIZE_MULTI_RIGHT 312 | bind-key -T $KT_WIN_RESIZE $KBD_WIN_RESIZE_MULTI_DOWN $CMD_WIN_RESIZE_MULTI_DOWN 313 | bind-key -T $KT_WIN_RESIZE $KBD_WIN_RESIZE_MULTI_UP $CMD_WIN_RESIZE_MULTI_UP 314 | 315 | bind-key -T $KT_WIN_RESIZE $KBD_QUIT set-option key-table $KT_CMD 316 | bind-key -T $KT_WIN_RESIZE $KBD_CMD_EXIT set-option key-table root 317 | EOF 318 | 319 | # session. 320 | KT_SESS=$KT_PREFIX-session 321 | cat << EOF >> "$KBD_FILE" 322 | 323 | bind-key -T $KT_CMD $KBD_SESS switch-client -T $KT_SESS 324 | 325 | bind-key -T $KT_SESS $KBD_SESS_DETACH $CMD_SESS_DETACH 326 | bind-key -T $KT_SESS $KBD_SESS_PREV $CMD_SESS_PREV 327 | bind-key -T $KT_SESS $KBD_SESS_NEXT $CMD_SESS_NEXT 328 | bind-key -T $KT_SESS $KBD_SESS_TREE $CMD_SESS_TREE 329 | bind-key -T $KT_SESS $KBD_SESS_DEL $CMD_SESS_DEL 330 | bind-key -T $KT_SESS $KBD_SESS_RENAME $CMD_SESS_RENAME 331 | EOF 332 | 333 | # goto. 334 | KT_GOTO=$KT_PREFIX-goto 335 | cat << EOF >> "$KBD_FILE" 336 | 337 | bind-key -T $KT_CMD $KBD_GOTO switch-client -T $KT_GOTO 338 | EOF 339 | 340 | # goto-window. 341 | KT_GOTO_WIN=$KT_PREFIX-goto-window 342 | cat << EOF >> "$KBD_FILE" 343 | 344 | bind-key -T $KT_GOTO $KBD_GOTO_WIN switch-client -T $KT_GOTO_WIN 345 | 346 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_0 $CMD_GOTO_WIN_0 347 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_1 $CMD_GOTO_WIN_1 348 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_2 $CMD_GOTO_WIN_2 349 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_3 $CMD_GOTO_WIN_3 350 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_4 $CMD_GOTO_WIN_4 351 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_5 $CMD_GOTO_WIN_5 352 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_6 $CMD_GOTO_WIN_6 353 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_7 $CMD_GOTO_WIN_7 354 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_8 $CMD_GOTO_WIN_8 355 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_9 $CMD_GOTO_WIN_9 356 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_TREE $CMD_GOTO_WIN_TREE 357 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_INDEX $CMD_GOTO_WIN_INDEX 358 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_PREV $CMD_GOTO_WIN_PREV 359 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_NEXT $CMD_GOTO_WIN_NEXT 360 | bind-key -T $KT_GOTO_WIN $KBD_GOTO_WIN_LAST $CMD_GOTO_WIN_LAST 361 | EOF 362 | 363 | # goto-session. 364 | KT_GOTO_SESS=$KT_PREFIX-goto-session 365 | cat << EOF >> "$KBD_FILE" 366 | 367 | bind-key -T $KT_GOTO $KBD_GOTO_SESS switch-client -T $KT_GOTO_SESS 368 | 369 | bind-key -T $KT_GOTO_SESS $KBD_GOTO_SESS_PREV $CMD_GOTO_SESS_PREV 370 | bind-key -T $KT_GOTO_SESS $KBD_GOTO_SESS_NEXT $CMD_GOTO_SESS_NEXT 371 | bind-key -T $KT_GOTO_SESS $KBD_GOTO_SESS_TREE $CMD_GOTO_SESS_TREE 372 | EOF 373 | 374 | # Load the keybindings. 375 | tmux source-file "$KBD_FILE" 376 | 377 | if [ "$START_OPT_VAL" == on ]; then 378 | # Start with modal command keytable. 379 | tmux set-option -g key-table $KT_CMD 380 | fi 381 | 382 | # Prepend left status bar with KT_CMD_ICON if our key tables are in use. 383 | # Determine this by checking if current key table starts with our prefix. 384 | KT_CMD_ICON=$KT_CMD_ICON_VAL 385 | STATUS_LEFT=` 386 | `'#{'` 387 | `'?#{==:'$KT_PREFIX'-,'` 388 | `'#{='$((${#KT_PREFIX} + 1))':client_key_table}'` 389 | `'},'` 390 | `$KT_CMD_ICON' ,'` 391 | `'}' 392 | 393 | if [ "$SHOW_CMD_KEYS_VAL" == on ]; then 394 | # Check which key table is in use and use corresponding "icon" in the left 395 | # status bar. The icons are derived from the keybindings. 396 | KT_WIN_ICON="[$KBD_WIN]" 397 | KT_WIN_PANE_ICON="[$KBD_WIN$KBD_WIN_PANE]" 398 | KT_WIN_SPLIT_ICON="[$KBD_WIN$KBD_WIN_SPLIT]" 399 | KT_WIN_MOVE_ICON="[$KBD_WIN$KBD_WIN_MOVE]" 400 | KT_WIN_ARRANGE_ICON="[$KBD_WIN$KBD_WIN_ARRANGE]" 401 | KT_WIN_RESIZE_ICON="[$KBD_WIN$KBD_WIN_RESIZE]" 402 | 403 | KT_SESS_ICON="[$KBD_SESS]" 404 | 405 | KT_GOTO_ICON="[$KBD_GOTO]" 406 | KT_GOTO_WIN_ICON="[$KBD_GOTO$KBD_GOTO_WIN]" 407 | KT_GOTO_SESS_ICON="[$KBD_GOTO$KBD_GOTO_SESS]" 408 | 409 | # Seems to be the only way to to do if-elseif-...-else in tmux format 410 | # syntax... 411 | STATUS_LEFT=` 412 | `'#{'` 413 | `'?#{==:'$KT_CMD','` 414 | `'#{client_key_table}'` 415 | `'},'` 416 | `$KT_CMD_ICON' ,'` 417 | `'#{'` 418 | `'?#{==:'$KT_WIN','` 419 | `'#{client_key_table}'` 420 | `'},'` 421 | `$KT_WIN_ICON' ,'` 422 | `'#{'` 423 | `'?#{==:'$KT_WIN_PANE','` 424 | `'#{client_key_table}'` 425 | `'},'` 426 | `$KT_WIN_PANE_ICON' ,'` 427 | `'#{'` 428 | `'?#{==:'$KT_WIN_SPLIT','` 429 | `'#{client_key_table}'` 430 | `'},'` 431 | `$KT_WIN_SPLIT_ICON' ,'` 432 | `'#{'` 433 | `'?#{==:'$KT_WIN_MOVE','` 434 | `'#{client_key_table}'` 435 | `'},'` 436 | `$KT_WIN_MOVE_ICON' ,'` 437 | `'#{'` 438 | `'?#{==:'$KT_WIN_ARRANGE','` 439 | `'#{client_key_table}'` 440 | `'},'` 441 | `$KT_WIN_ARRANGE_ICON' ,'` 442 | `'#{'` 443 | `'?#{==:'$KT_WIN_RESIZE','` 444 | `'#{client_key_table}'` 445 | `'},'` 446 | `$KT_WIN_RESIZE_ICON' ,'` 447 | `'#{'` 448 | `'?#{==:'$KT_SESS','` 449 | `'#{client_key_table}'` 450 | `'},'` 451 | `$KT_SESS_ICON' ,'` 452 | `'#{'` 453 | `'?#{==:'$KT_GOTO','` 454 | `'#{client_key_table}'` 455 | `'},'` 456 | `$KT_GOTO_ICON' ,'` 457 | `'#{'` 458 | `'?#{==:'$KT_GOTO_WIN','` 459 | `'#{client_key_table}'` 460 | `'},'` 461 | `$KT_GOTO_WIN_ICON' ,'` 462 | `'#{'` 463 | `'?#{==:'$KT_GOTO_SESS','` 464 | `'#{client_key_table}'` 465 | `'},'` 466 | `$KT_GOTO_SESS_ICON' ,'` 467 | `'}}}}}}}}}}}' 468 | fi 469 | 470 | # We want to set the left status bar once; do it only if we can't find our 471 | # status string in current status (otherwise we would create several icons next 472 | # to each other, which could happen if this is run back to back multiple times 473 | # somehow). 474 | CURRENT_STATUS_LEFT=$(tmux show-options -g -v status-left) 475 | if ! grep -q -F "$STATUS_LEFT" <<< "$CURRENT_STATUS_LEFT"; then 476 | tmux set-option -g status-left "$STATUS_LEFT""$CURRENT_STATUS_LEFT" 477 | fi 478 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | tmux-modal 2 | ========== 3 | 4 | Execute complex tmux commands in just a few keystrokes. tmux-modal introduces a 5 | modal mode (e.g. like in Vim text editor) in tmux that is designed to be 6 | efficient, easy to remember and comfortable. 7 | 8 | ## Examples 9 | 10 | Hit `M-m` (`Alt-m` in tmux terminology) first to enter the modal command mode. 11 | The status bar will show `[=]` to indicate that we are now in the modal mode 12 | (hit `M-m` again to exit back to "normal" mode). Here are some quick examples 13 | what one can do with tmux-modal (see the [keybindings](#keybindings) for a 14 | complete list). 15 | 16 | **Navigation** 17 | 18 | Easily move between panes and windows: 19 | 20 | ![navigation-demo](https://user-images.githubusercontent.com/9569246/152884650-0daa8e87-4eff-4f36-b4b4-b693c3bdef28.gif) 21 | 22 | - `w l` to select the right pane 23 | - `w j` to select the pane below 24 | - `w 1` to select window 1 25 | - `w o` to go back to other/last windows 26 | 27 | **Management** 28 | 29 | Create and delete windows with a breeze. Split, delete, zoom, move and break 30 | panes and much more in just a few keystrokes: 31 | 32 | ![management-gif](https://user-images.githubusercontent.com/9569246/152884665-781279cd-c615-44f7-aaa0-8826e72c558c.gif) 33 | 34 | - `w c` to create a new window 35 | - `w D` to delete window 36 | - `w s k` to split window pane right 37 | - `w s j` to split window pane below 38 | - `w d` to delete a pane 39 | - `w z` to zoom in/out of a pane 40 | 41 | **Sticky commands** 42 | 43 | Use sticky commands to do more, repeatedly. Sticky commands enters a _specific_ 44 | modal mode with specialized keybindings (see [keybindings](#keybindings) for a 45 | more detailed explanation and a full list of available commands). 46 | 47 | ![sticky-window-gif](https://user-images.githubusercontent.com/9569246/152884680-79e0dcd1-7d72-4120-9fab-44f9e5e211ae.gif) 48 | 49 | Hit `w w` to enter sticky window command: 50 | 51 | - `s j` to split window pane below 52 | - `k` to select the above pane 53 | - `d` to delete a pane 54 | - `c` to create window 55 | - `0` to select window 0 56 | 57 | Hit `w r` to enter sticky resize window command: 58 | 59 | ![sticky-resize-gif](https://user-images.githubusercontent.com/9569246/152884687-29d6fff8-122e-4fa7-aeab-cc4d5ef1b198.gif) 60 | 61 | - `k` to resize the window pane one step upwards 62 | - `l` to resize the window pane one step to the right 63 | - `J` to resize the window pane multiple steps downwards 64 | - `H` to resize the window pane multiple steps to the left 65 | 66 | ## Install 67 | 68 | ### Tmux Plugin Manager 69 | 70 | Using [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) is the 71 | recommended way to install tmux-modal. Just add it as a plugin in your 72 | `~/.tmux.conf`: 73 | 74 | ``` 75 | set -g @plugin 'whame/tmux-modal' 76 | ``` 77 | 78 | Then hit `prefix` + `I` to install it (see the documentaion for Tmux Plugin 79 | Manager for more details). 80 | 81 | ### Manually 82 | 83 | You can also clone and manually install tmux-modal. Put this at the end of your 84 | `~/.tmux.conf`: 85 | 86 | ``` 87 | run /path/to/cloned/tmux-modal/tmux-modal.tmux 88 | ``` 89 | 90 | Note however that in this case you have to manually sync the cloned repository 91 | in order to get bug fixes, new features etc. 92 | 93 | ## Keybindings 94 | 95 | The following are the default keybindings of tmux-modal. (Note that these are 96 | customizable, as well as the command executed for each! See option 97 | [`@modal-keybindings-conf`](#custom-keybindings) and 98 | [`@modal-commands-conf`](#custom-commands), respectively.) 99 | 100 | ### Main 101 | 102 | | Keybinding | Description | tmux Command | 103 | |------------|-------------------------------------|------------------| 104 | | `M-m` | Enter modal command mode. | - | 105 | | `M-m` | Exit modal command mode. | - | 106 | | `y` | Paste buffer (e.g. from copy mode). | `paste-buffer` | 107 | | `c` | Enter copy-mode. | `copy-mode` | 108 | | `q` | Quit sticky command. | - | 109 | | `:` | Open tmux command mode. | `command-prompt` | 110 | 111 | ### Window 112 | 113 | | Keybinding | Description | tmux Command | 114 | |------------|-------------------------------------------------------------|----------------------------------------------------| 115 | | `w w` | Enter sticky window command mode. | - | 116 | | `w 0` | Go to window 0. This is an alias for `g w 0`. | `select-window -t :0` | 117 | | `w 1` | Go to window 1. This is an alias for `g w 1`. | `select-window -t :1` | 118 | | `w 2` | Go to window 2. This is an alias for `g w 2`. | `select-window -t :2` | 119 | | `w 3` | Go to window 3. This is an alias for `g w 3`. | `select-window -t :3` | 120 | | `w 4` | Go to window 4. This is an alias for `g w 4`. | `select-window -t :4` | 121 | | `w 5` | Go to window 5. This is an alias for `g w 5`. | `select-window -t :5` | 122 | | `w 6` | Go to window 6. This is an alias for `g w 6`. | `select-window -t :6` | 123 | | `w 7` | Go to window 7. This is an alias for `g w 7`. | `select-window -t :7` | 124 | | `w 8` | Go to window 8. This is an alias for `g w 8`. | `select-window -t :8` | 125 | | `w 9` | Go to window 9. This is an alias for `g w 9`. | `select-window -t :9` | 126 | | `w t` | Select window with tree view. This is an alias for `g w t`. | `choose-tree -Zw` | 127 | | `w i` | Select window with index. This is an alias for `g w i`. | `command-prompt -p index "select-window -t ':%%'"` | 128 | | `w h` | Select left pane. | `select-pane -L` | 129 | | `w l` | Select right pane. | `select-pane -R` | 130 | | `w k` | Select above pane. | `select-pane -U` | 131 | | `w j` | Select below pane. | `select-pane -D` | 132 | | `w d` | Delete window pane. | `kill-pane` | 133 | | `w H` | Select previous window. This is an alias for `g w h`. | `select-window -t :-` | 134 | | `w L` | Select next window. This is an alias for `g w l`. | `select-window -t :+` | 135 | | `w D` | Delete window. | `kill-window` | 136 | | `w c` | Create new window. | `new-window` | 137 | | `w o` | Select other/last window. | `last-window` | 138 | | `w z` | Zoom pane. | `resize-pane -Z` | 139 | | `w b` | Break pane into a new window. | `break-pane` | 140 | | `w n` | Display pane numbers. | `display-panes` | 141 | | `w ,` | Rename window. | `command-prompt -I "#W" "rename-window -- '%%'"` | 142 | | `w s l` | Split window pane right. | `split-window -h` | 143 | | `w s j` | Split window pane down. | `split-window` | 144 | | `w m k` | Move window pane up. | `swap-pane -U` | 145 | | `w m j` | Move window pane down. | `swap-pane -D` | 146 | | `w a 1` | Arrange window to layout 1. | `select-layout even-horizontal` | 147 | | `w a 2` | Arrange window to layout 2. | `select-layout even-vertical` | 148 | | `w a 3` | Arrange window to layout 3. | `select-layout main-horizontal` | 149 | | `w a 4` | Arrange window to layout 4. | `select-layout main-vertical` | 150 | 151 | Note that the sticky window command (`w w`) allows one to execute all of the 152 | above commands in the table but without the initial `w`. For example, after 153 | hitting `w w`, `s l` splits the window pane to the right, `d` deletes the window 154 | pane, `h` selects the left pane and so on (`q` exits the sticky command). Also 155 | see the option [`@modal-always-sticky`](#always-sticky-command) if you instead 156 | always want to use the sticky command version (with only hitting `w` once). 157 | 158 | #### Resize 159 | 160 | | Keybinding | Description | tmux Command | 161 | |------------|------------------------------------|--------------| 162 | | `w r` | Enter sticky resize window command | - | 163 | 164 | When in sticky resize window command, the following resizes the window (as 165 | usual, `q` exits the sticky command). 166 | 167 | | Keybinding | Description | tmux Command | 168 | |------------|------------------------------------------------------|--------------------| 169 | | `h` | Resizes the window pane one step to the left. | `resize-pane -L` | 170 | | `l` | Resizes the window pane one step to the right. | `resize-pane -R` | 171 | | `j` | Resizes the window pane one step downwards. | `resize-pane -D` | 172 | | `k` | Resizes the window pane one step upwards. | `resize-pane -U` | 173 | | `H` | Resizes the window pane multiple steps to the left. | `resize-pane -L 5` | 174 | | `L` | Resizes the window pane multiple steps to the right. | `resize-pane -R 5` | 175 | | `J` | Resizes the window pane multiple steps downwards. | `resize-pane -D 5` | 176 | | `K` | Resizes the window pane multiple steps upwards. | `resize-pane -U 5` | 177 | 178 | ### Session 179 | 180 | | Keybinding | Description | tmux Command | 181 | |------------|--------------------------------------------------------------|------------------------------------------------| 182 | | `s d` | Detach session. | `detach-client` | 183 | | `s h` | Select previous session. This is an alias for `g s h`. | `switch-client -p` | 184 | | `s l` | Select next session. This is an alias for `g s l`. | `switch-client -n` | 185 | | `s t` | Select session with tree view. This is an alias for `g s t`. | `choose-tree -Zs` | 186 | | `s D` | Delete session. | `kill-session` | 187 | | `s ,` | Rename session. | `command-prompt -I "#S" "rename-session '%%'"` | 188 | 189 | 190 | ### Go to 191 | 192 | #### Window 193 | 194 | | Keybinding | Description | tmux Command | 195 | |------------|------------------------------|----------------------------------------------------| 196 | | `g w 0` | Go to window 0. | `select-window -t :0` | 197 | | `g w 1` | Go to window 1. | `select-window -t :1` | 198 | | `g w 2` | Go to window 2. | `select-window -t :2` | 199 | | `g w 3` | Go to window 3. | `select-window -t :3` | 200 | | `g w 4` | Go to window 4. | `select-window -t :4` | 201 | | `g w 5` | Go to window 5. | `select-window -t :5` | 202 | | `g w 6` | Go to window 6. | `select-window -t :6` | 203 | | `g w 7` | Go to window 7. | `select-window -t :7` | 204 | | `g w 8` | Go to window 8. | `select-window -t :8` | 205 | | `g w 9` | Go to window 9. | `select-window -t :9` | 206 | | `g w t` | Go to window with tree view. | `choose-tree -Zw` | 207 | | `g w i` | Go to window with index. | `command-prompt -p index "select-window -t ':%%'"` | 208 | | `g w h` | Go to previous window. | `select-window -t :-` | 209 | | `g w l` | Go to next window. | `select-window -t :+` | 210 | | `g w o` | Go to other/last window. | `last-window` | 211 | 212 | #### Session 213 | 214 | | Keybinding | Description | tmux Command | 215 | |------------|-------------------------------|--------------------| 216 | | `g s h` | Go to previous session. | `switch-client -p` | 217 | | `g s l` | Go to next session. | `switch-client -n` | 218 | | `g s t` | Go to session with tree view. | `choose-tree -Zs` | 219 | 220 | ## Customization 221 | 222 | ### Custom keybindings 223 | 224 | The option `@modal-keybindings-conf` can be set to load custom keybindings. The 225 | file [`keybindings.conf`](keybindings.conf) shows the default keybindings and 226 | can be used as a template. Thus, copy the file and modify it to your liking, and 227 | finally set this in your `.tmux.conf` to load them: 228 | 229 | ``` 230 | set -g @modal-keybindings-conf /path/to/my-tmux-modal-keybindings.conf 231 | ``` 232 | 233 | ### Custom commands 234 | 235 | The option `@modal-commands-conf` can be set to load custom commands that will 236 | be executed for the keybindings. The file [`commands.conf`](commands.conf) shows 237 | the default commands and can be used as a template. Thus, copy the file and 238 | modify it to your liking, and finally set this in your `.tmux.conf` to load 239 | them: 240 | 241 | ``` 242 | set -g @modal-commands-conf /path/to/my-tmux-modal-commands.conf 243 | ``` 244 | 245 | ### Start with modal command mode 246 | 247 | The option `@modal-on-start` can be used to automatically enter the modal 248 | command mode on a new tmux session. If you always want to start a new session 249 | with the modal command mode, add the following to `.tmux.conf`: 250 | 251 | ``` 252 | set -g @modal-on-start on 253 | ``` 254 | 255 | ### Always sticky command 256 | 257 | The option `@modal-always-sticky` can be specified to always use the sticky 258 | version instead of manually entering the sticky command first (e.g. `w w` for 259 | sticky window command): 260 | 261 | ``` 262 | set -g @modal-always-sticky on 263 | ``` 264 | 265 | For example, with this in `.tmux.conf`, one only has to press `w` once and the 266 | sticky window command mode will be entered directly. That is, after hitting `w` 267 | once, you can now directly use `h`, `j`, `k` and `l` to select the window panes 268 | (or any other window commands). Don't forget to exit the sticky command with 269 | `q`. 270 | 271 | ### Show command keys in status bar 272 | 273 | The option `@modal-show-cmd-keys` can be set in `.tmux.conf` to give immediate 274 | feedback in the status bar during tmux-modal command sequences: 275 | 276 | ``` 277 | set -g @modal-show-cmd-keys on 278 | ``` 279 | 280 | The left status bar will now update to match the tmux-modal command currently in 281 | use. For example, if you press `w` the status bar will change from the modal 282 | command icon `[=]` to `[w]` (the window command). If you now further press `s`, 283 | it will update to `[ws]` to signify the split window command sequence and so on. 284 | 285 | ### Set custom status bar modal mode icon 286 | 287 | The option `@modal-cmd-icon` can be used to specify any string, which will be 288 | the left status bar icon to indicate that modal mode is in use. For example, to 289 | use a circle as icon (note the padding at the beginning for a more aesthetically 290 | centering in the status bar): 291 | 292 | ``` 293 | set -g @modal-cmd-icon " 🟢" 294 | ``` 295 | 296 | ### Yes/no prompt 297 | 298 | **DEPRECATED** 299 | 300 | This option is deprecated due to option 301 | [`@modal-commands-conf`](#custom-commands) and will be removed soon. To keep the 302 | old behavior, use option [`@modal-commands-conf`](#custom-commands) instead. For 303 | example: 304 | 305 | ``` 306 | diff --git a/commands.conf b/commands.conf 307 | index 1859fcb..ec4f65e 100644 308 | --- a/commands.conf 309 | +++ b/commands.conf 310 | @@ -82,7 +82,7 @@ CMD_WIN_PANE_UP='select-pane -U' 311 | CMD_WIN_PANE_DOWN='select-pane -D' 312 | 313 | ## Delete window pane. 314 | -CMD_WIN_PANE_DEL='kill-pane' 315 | +CMD_WIN_PANE_DEL='confirm-before -p "kill-pane #P? (y/n)" kill-pane' 316 | 317 | ## Select previous window (window command alias for CMD_GOTO_WIN_PREV). 318 | CMD_WIN_PREV='select-window -t :-' 319 | @@ -91,7 +91,7 @@ CMD_WIN_PREV='select-window -t :-' 320 | CMD_WIN_NEXT='select-window -t :+' 321 | 322 | ## Delete window. 323 | -CMD_WIN_DEL='kill-window' 324 | +CMD_WIN_DEL='confirm-before -p "kill-window #W? (y/n)" kill-window' 325 | 326 | ## Create new window. 327 | CMD_WIN_CREATE='new-window' 328 | @@ -183,7 +183,7 @@ CMD_SESS_NEXT='switch-client -n' 329 | CMD_SESS_TREE='choose-tree -Zs' 330 | 331 | ## Delete session. 332 | -CMD_SESS_DEL='kill-session' 333 | +CMD_SESS_DEL='confirm-before -p "kill-session #S? (y/n)" kill-session' 334 | 335 | # "Go to" command prefix. 336 | ``` 337 | 338 | --- 339 | 340 | _Some commands might be too "dangerous" to execute directly, for example `w d` 341 | (`kill-pane`) or `w D` (`kill-window`). The option `@modal-yesno-cmd` can 342 | therefore be used to ask for confirmation before executing the commands (to 343 | mimic the default tmux behavior). If you want a yes/no prompt before executing 344 | these commands, put this in `.tmux.conf`:_ 345 | 346 | ``` 347 | set -g @modal-yesno-cmd on 348 | ``` 349 | --------------------------------------------------------------------------------