]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - shell/tmux-window.pl
multimedia: yt-dlp: also write video description
[config/dotfiles.git] / shell / 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 prefix ;;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 unbind-key -n 0 \; \
18 #                   ...
19 #                   unbind-key -n 9 \; \
20 #                   unbind-key -n \\; \         # this unmaps ;
21 #                   select-window -t :10 \;
22 #
23 # Due to Tmux's handling of errors, they abort the rest of the mapping, the
24 # select-window command is executed as last part of the mapping.
25 #
26 # If a secondary chaining (e.g. prefix ;;3) is requested, a second file like
27 # the generated one must be loaded with ; to jump to windows 20-29. Therefore
28 # the following line is added in this case.
29 #
30 #     bind-key -n \; source-file "/path/to/second/file"
31 #
32 # To use these mappings first create the files using this script. To switch to
33 # windows 10-19 with ;0-;9 and to 20-29 with ;;0-;;9 run these commands (can
34 # be chained indefinitely):
35 #
36 #     $ perl ./tmux-window.pl 1 "`pwd`/tmux-window2.conf" > tmux-window1.conf
37 #     $ perl ./tmux-window.pl 2                           > tmux-window2.conf
38 #
39 # Then add the following to your tmux.conf:
40 #
41 #     bind-key \; source-file "/path/to/tmux-window1.conf"
42 #
43 # tmux-window1.conf automatically contains the path to tmux-window2.conf,
44 # therefore chaining for ;;3 will work to jump to window 23.
45
46 # Copyright (C) 2012-2013  Simon Ruderich
47 #
48 # This program is free software: you can redistribute it and/or modify
49 # it under the terms of the GNU General Public License as published by
50 # the Free Software Foundation, either version 3 of the License, or
51 # (at your option) any later version.
52 #
53 # This program is distributed in the hope that it will be useful,
54 # but WITHOUT ANY WARRANTY; without even the implied warranty of
55 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
56 # GNU General Public License for more details.
57 #
58 # You should have received a copy of the GNU General Public License
59 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
60
61
62 use strict;
63 use warnings;
64
65
66 if (scalar @ARGV != 1 && scalar @ARGV != 2) {
67     print STDERR "Usage: $0 <level> [</path/to/tmux-window.conf>]\n";
68     exit 1;
69 }
70
71 my $level = $ARGV[0];
72 my $path  = $ARGV[1];
73
74 if (defined $path and $path !~ m{^/}) {
75     print STDERR "<path> must be an absolute path!\n";
76     exit 2;
77 }
78
79
80 for (my $i = 0; $i < 10; $i++) {
81     print "bind-key -n $i ";
82     for (my $j = 0; $j < 10; $j++) {
83         print "unbind-key -n $j \\; ";
84     }
85     print "unbind-key -n \\\\; \\; ";
86     # Do the select-window last. If the window doesn't exist the failing
87     # select-window command prevents unbinding the other keys.
88     print "select-window -t :$level$i\n";
89 }
90
91 if (defined $path) {
92     print "bind-key -n \\; source-file \"$path\"\n";
93 }