There is currently no direct way to reset a key’s binding to its default; the initialization of the default bindings (in
key_bindings_init()) is done once when the tmux server first starts (in
server_start()), and there is no mechanism to reset a single key.
For your desired scenario where you want the act of sourcing your configuration file to reestablish a default binding that was previously overridden by a custom binding that has since been deleted from your configuration file, the method you devised is reasonable (though unfortunately verbose):
unbind-key -a, then reestablish all the “default” bindings, then establish your custom bindings (some of which might override a “default” binding).
A server’s current bindings can be extracted with the
list-keys command*; this can help generate/maintain your proposed
.tmux.reset.conf file, but you need a way to extract the default bindings, not the current bindings.
There are some situations where the output of
list-keys is not currently directly usable: the binding for semicolon needs its semicolon escaped with a backslash to prevent it from being interpreted as a tmux command separator, and any bindings that had arguments that used double quotes inside single quotes (none of the default bindings are like this) will come out as double quotes inside double qoutes.
To get the default bindings you need a temporary server with a minimal configuration (i.e. no custom bindings) so that you can capture its
list-keys output. There is no limit to the number of tmux servers you can run, but each one must use a different socket pathname; the
-S tmux options can be used to specify a socket name (in
$TMPDIR/tmux-$UID or full socket pathname. So, to talk to (or start) a new server on a socket named
temp, you would use this:
tmux -L temp …
To make sure it does not use your
.tmux.conf, you use
-f to tell it to read
/dev/null (a special file that is always empty):
tmux -f /dev/null -L temp …
Note: this does not prevent the processing of
/etc/tmux.conf, if such a file exists; the path to this “system configuration file” is hard-coded and there is no option to bypass it (short of patching the code).
Normally, you need a
new-session command to actually start the server, but we do not want any sessions, just an initialized server to query. The
start-server command does just that: starts a server without creating any sessions.
tmux -f /dev/null -L temp start-server …
Now, we just need to append our “query” command (
list-keys in this case):
tmux -f /dev/null -L temp start-server \; list-keys
Note: the semicolon needs to be escaped or quoted to prevent the shell from treating it as a shell command separator since we want it to be a tmux command separator.
Since there are no sessions to maintain, the server will exit automatically after it finishes running the
So, you can use a command like this to generate the bulk of your
.tmux.reset.conf without having to worry about temporarily removing your
.tmux.conf file (to let you see just the default bindings) and without having to shut down any existing servers.
run-shell command was synchronous you could embed a call like this in your configuration file (capturing to a temporary file that you would then process with
source-file) instead of having a static file (your
.tmux.reset.conf). That would let you always use the default bindings from your current version of tmux (the default bindings change occasionally). Alas, the completion of the
run-shell command is currently asynchronous with respect to subsequent commands (commands that come after a
run-shell command will usually run before the process spawned by
run-shell has had a chance to finish).