From: Simon Ruderich Date: Sat, 20 Oct 2012 10:24:00 +0000 (+0200) Subject: tmux.conf: Add mappings to switch to windows 10-29 quickly. X-Git-Url: https://ruderich.org/simon/gitweb/?p=config%2Fdotfiles.git;a=commitdiff_plain;h=3f5d80c3a9142c424ca2c482b6ad28c475349d42 tmux.conf: Add mappings to switch to windows 10-29 quickly. Also add helper script tmux-window.pl and update setup.sh. --- diff --git a/.gitignore b/.gitignore index 13634ac..caf6922 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ /screenrc /shell/aliases /tmux.conf +/tmux-window1.conf +/tmux-window2.conf # Ignore temporary zsh files. /zsh/cache /zsh/history* diff --git a/setup.sh b/setup.sh index 5ade6bb..a5af86b 100755 --- a/setup.sh +++ b/setup.sh @@ -152,6 +152,19 @@ fi if installed tmux; then generate perl tmux.conf ./bin/remove-continuation.pl + # Add mappings to switch to windows 10-29 quickly. See tmux-window.pl for + # details. + perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" > tmux-window1.conf + perl ./tmux-window.pl 2 > tmux-window2.conf + # Set absolute path to tmux-window1.conf in tmux.conf. + perl < tmux.conf > tmux.conf.tmp \ + -e 'while () { + s/\bTMUX_WINDOW_PATH\b/$ARGV[0]/; + print; + }' \ + "`pwd`/tmux-window1.conf" + mv tmux.conf.tmp tmux.conf + # 256 colors not available. if test -z "$use_256colors"; then echo tmux.conf: removing 256 colors diff --git a/tmux-window.pl b/tmux-window.pl new file mode 100644 index 0000000..b16529b --- /dev/null +++ b/tmux-window.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl + +# Helper script to support quick window jumping with a prefix key for 0-9 in +# Tmux. + +# At the moment Tmux doesn't support mappings with multiple keys, e.g. prefix +# ;0, prefix ;1 etc. (where ; is the common secondary prefix key). But this +# works in GNU screen and is very useful to switch to windows quickly, e.g. +# prefix ;3 to switch to window 13 or ;;5 to switch to window 25. +# +# To simulate this missing feature ; and 0-9 must be rebound when prefix ; is +# used, and unbound when ; or 0-9 was pressed and the window selected. This +# script generates these tedious mappings. +# +# For each number (0-9) the following mapping is generated (example with 0): +# +# bind-key -n 0 select-window -t :10 \; \ +# unbind-key -n 0 \; \ +# ... +# unbind-key -n 9 \; \ +# unbind-key -n \\; # this unmaps ; +# +# If a secondary chaining (e.g. prefix ;;3) is requested, a second file like +# the generated one must be loaded with ; to jump to windows 20-29. Therefore +# the following line is added in this case. +# +# bind-key -n \; source-file "/path/to/second/file" +# +# To use these mappings first create the files using this script. To switch to +# windows 10-19 with ;0-;9 and to 20-29 with ;;0-;;9 run these commands (can +# be chained indefinitely): +# +# $ perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" > tmux-window1.conf +# $ perl ./tmux-window.pl 2 > tmux-window2.conf +# +# Then add the following to your tmux.conf: +# +# bind-key \; source-file "/path/to/tmux-window1.conf" +# +# tmux-window1.conf automatically contains the path to tmux-window2.conf, +# therefore chaining for ;;3 will work to jump to window 33. + +# Copyright (C) 2012 Simon Ruderich +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +use strict; +use warnings; + + +if (scalar @ARGV != 1 && scalar @ARGV != 2) { + print STDERR "Usage: $0 []\n"; + exit 1; +} + +my $level = $ARGV[0]; +my $path = $ARGV[1]; + +if (defined $path and $path !~ m{^/}) { + print STDERR " must be an absolute path!\n"; + exit 2; +} + + +for (my $i = 0; $i < 10; $i++) { + print "bind-key -n $i select-window -t :$level$i \\; "; + for (my $j = 0; $j < 10; $j++) { + print "unbind-key -n $j \\; "; + } + print "unbind-key -n \\\\;\n"; +} + +if (defined $path) { + print "bind-key -n \\; source-file \"$path\"\n"; +} diff --git a/tmux.conf.in b/tmux.conf.in index 097ebc3..c748eea 100644 --- a/tmux.conf.in +++ b/tmux.conf.in @@ -113,4 +113,9 @@ bind-key C-b capture-pane \; \ save-buffer ~/.tmp/tmux-urlview \; \ new-window "$SHELL -c 'urlview < ~/.tmp/tmux-urlview'" +# Binding for fast switching to windows 10 to 29 (and more if necessary). +# Thanks to Kays in #irssi on Freenode (2012-10-18 16:25 CEST) for reminding +# me of this issue in Tmux. See tmux-windows.pl for details how it works. +bind-key \; source-file "TMUX_WINDOW_PATH" + # vim: ft=tmux