]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - tmux-window.pl
b16529b379fa52e1085a3692f205a0e58cae25c5
[config/dotfiles.git] / tmux-window.pl
1 #!/usr/bin/perl
2
3 # Helper script to support quick window jumping with a prefix key for 0-9 in
4 # Tmux.
5
6 # At the moment Tmux doesn't support mappings with multiple keys, e.g. prefix
7 # ;0, prefix ;1 etc. (where ; is the common secondary prefix key). But this
8 # works in GNU screen and is very useful to switch to windows quickly, e.g.
9 # prefix ;3 to switch to window 13 or ;;5 to switch to window 25.
10 #
11 # To simulate this missing feature ; and 0-9 must be rebound when prefix ; is
12 # used, and unbound when ; or 0-9 was pressed and the window selected. This
13 # script generates these tedious mappings.
14 #
15 # For each number (0-9) the following mapping is generated (example with 0):
16 #
17 #     bind-key -n 0 select-window -t :10 \; \
18 #                   unbind-key -n 0 \; \
19 #                   ...
20 #                   unbind-key -n 9 \; \
21 #                   unbind-key -n \\; # this unmaps ;
22 #
23 # If a secondary chaining (e.g. prefix ;;3) is requested, a second file like
24 # the generated one must be loaded with ; to jump to windows 20-29. Therefore
25 # the following line is added in this case.
26 #
27 #     bind-key -n \; source-file "/path/to/second/file"
28 #
29 # To use these mappings first create the files using this script. To switch to
30 # windows 10-19 with ;0-;9 and to 20-29 with ;;0-;;9 run these commands (can
31 # be chained indefinitely):
32 #
33 #     $ perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" > tmux-window1.conf
34 #     $ perl ./tmux-window.pl 2                           > tmux-window2.conf
35 #
36 # Then add the following to your tmux.conf:
37 #
38 #     bind-key \; source-file "/path/to/tmux-window1.conf"
39 #
40 # tmux-window1.conf automatically contains the path to tmux-window2.conf,
41 # therefore chaining for ;;3 will work to jump to window 33.
42
43 # Copyright (C) 2012  Simon Ruderich
44 #
45 # This program is free software: you can redistribute it and/or modify
46 # it under the terms of the GNU General Public License as published by
47 # the Free Software Foundation, either version 3 of the License, or
48 # (at your option) any later version.
49 #
50 # This program is distributed in the hope that it will be useful,
51 # but WITHOUT ANY WARRANTY; without even the implied warranty of
52 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
53 # GNU General Public License for more details.
54 #
55 # You should have received a copy of the GNU General Public License
56 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
57
58
59 use strict;
60 use warnings;
61
62
63 if (scalar @ARGV != 1 && scalar @ARGV != 2) {
64     print STDERR "Usage: $0 <level> [</path/to/tmux-window.conf>]\n";
65     exit 1;
66 }
67
68 my $level = $ARGV[0];
69 my $path  = $ARGV[1];
70
71 if (defined $path and $path !~ m{^/}) {
72     print STDERR "<path> must be an absolute path!\n";
73     exit 2;
74 }
75
76
77 for (my $i = 0; $i < 10; $i++) {
78     print "bind-key -n $i select-window -t :$level$i \\; ";
79     for (my $j = 0; $j < 10; $j++) {
80         print "unbind-key -n $j \\; ";
81     }
82     print "unbind-key -n \\\\;\n";
83 }
84
85 if (defined $path) {
86     print "bind-key -n \\; source-file \"$path\"\n";
87 }