What does the following section do in the tmux config file?
# remap prefix
unbind C-b
set -g prefix C-a
bind C-a send-prefix
I know functionally it is changing the prefix mapping of C-b to C-a, but why does this require three lines to do that?
It seems to me the unbind removes the default C-b as prefix. But then how do the other two work? (Also, is the last line necessary?)
The first two lines unbind and set do the prefix remapping.
The last line is necessary, even though without it, you can already use c-a as prefix key.
Imagine, now your prefix is c-a, now you open vim in a tmux window, you want to increment a number by c-a, how can you do it? When you pressed c-a tmux will capture it as the tmux prefix, now you need some key to send the original c-a to the process(vim).
Same example when you edit command line with emacs binding. You want your cursor to move to the BOL by c-a.
The last line will allow a process to accept c-a in tmux window, by pressing c-a twice.
As an addition to Kent's aswer, you actually don't need unbind C-b since you can only have one main prefix. So basically you only need two lines to remap a prefix key.
Related
This is my .tmux.conf file, placed in the home directory.
unbind C-b
set -g prefix S-a
bind S-a send-prefix
bind r source-file ~/.tmux.conf
I am properly loading it with tmux source-file ~/.tmux.conf. However, doing shift+a to initiate the prefix doesn't work.
However if I replace S-a with C-a, it'll work (ctrl+a).
What am I doing wrong?
You can't and it's not a good idea, the shift key is by no way meant for that. Take a look in the man tmux, section KEY BINDINGS for the list of available keys. More info are available here https://unix.stackexchange.com/a/140010
Solution 1
Run: (WARN: Save your work in all sessions first)
tmux kill-server
Solution 2
In your .zshrc/.bashrc file, add the following line:
[ -z "${TMUX}" ] || tmux YOUR_COMMAND
e.g. YOUR_COMMAND = set -g prefix S-a.
I want unbind the prefix keys of tmux, use caps lock-a instead of Ctrl-B
So, C means Ctrl, and ??? refer to caps lock?
unbind C-b
set -g prefix ???-a
tmux source-file ~/.tmux.conf
This is kind of a hack but achieves the end goal.
Change the behaviour of Capslock to Ctrl and keep the tmux config as C-a.
In OSX this can done using System Preferences > Keyboard > Modifier Keys.
In Linux this can be done using the command 'setxkbmap'
I am following a tmux tutorial which states (Control + b) + % should open a new pane. When I try to do this from tmux though, the Control + b keypress just gets converted into a character which displays on the command line.
Various tmux tutorials seem to treat Control + b as a special keypress, but it always just appears as a character on my command line. How do I use the tmux prefix correctly?
That tutorial you linked to in your question has you overriding the default Control-b with Control-a
try (Control-a) + %
this is a popular override. I use it. My ~/.tmux.conf has:
set -g prefix C-a
unbind C-b
bind C-a send-prefix
this is nice when you also remap your caps lock key to be control, the caps lock key and the 'a' key are right next to each other
Does Tmux supports key-bindings with key sequences like Vim does (e.g. bind-key ab kill-pane)? Or how can i emulate that?
I'm using tmux 2.3.
You can emulate key sequences by defining your own key tables and chaining them together.
For example, if I want <C-q>x to do something, I put the binding for 'x' into a key table "my-keys", then bind the key that activates that key table with switch-client (C-q):
bind-key -Tmy-keys x send-keys "my binding"
# Multi-key prefix for custom bindings
bind-key -Troot C-q switch-client -Tmy-keys
NOTE: I started with C-q, because it seems to conflict the least with the command line and Vim.
So, now you have every key at your disposal with a C-q prefix.
If you want more keys in your sequence, add another level of indirection:
bind-key -Tmy-keys x send-keys "my binding"
# Pane (i.e. 'W'indow commands like Vim with C-w)
bind-key -Tmy-keys-window-ctl s swap-pane
bind-key -Tmy-keys C-w switch-client -T my-keys-window-ctl
# Multi-key prefix for custom bindings
bind-key -Troot C-q switch-client -Tmy-keys
So, now I have swap-pane bound to <C-q><C-w>s.
This works because
<C-q> activates "my-keys" key table,
which has the binding <C-w>,
which activates "my-keys-window-ctl" key table
which has the binding s to call swap-pane
Tmux supports only single character key bindings (unfortunately).
So, only this:
bind-key a kill-pane
or this:
bind-key b kill-pane
Please note this is different from for example C-a (Ctrl-a) or M-a (Alt-a).
Even though we users write those with multiple characters and even have to press 2 keys to invoke them, both Ctrl-a and Alt-a are actually a single character for tmux (and in general to my knowledge).
Alternative
...might not be what you expect, but here it is:
# in .tmux.conf
bind a command-prompt -p "pressed a" "run '~/my_script %%'"
And the example my_script file:
#!/bin/bash
case "$1" in
b)
tmux kill-pane
;;
c)
tmux kill-window
;;
esac
Now after you reload your tmux.conf and press prefix + a you'll get a tmux prompt saying 'pressed a'.
Go ahead and press b and Enter. tmux kill-pane from the script will execute.
Similarly if you press prefix + a + c and Enter you'll execute another option from the script.
This kind-of mimics what you want with the addition of Enter key at the end.
Also, the provided script is extendable so you can add more "bindings" to get prefix + a + d + Enter etc..
I just move from GNU screen to tmux, in screen when I hit C-a, then type a again can take me to the beginning of the line, I wonder if I stick with C-a in tmux how can I work around this?
Maybe its an issue about the version I am using, but if the above code does not work for you, try this:
set -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
As you know, C-b is the default prefix in tmux. C-b C-b is used to send an actual C-b character to the terminal. If you switch the prefix to C-a, you just need to rebind some keys to update which one sends the send-prefix command.
For your .tmux.conf:
# You probably already put this in
set prefix C-a
unbind-key C-b
bind-key C-a send-prefix