0

My current pane resize mapping are

bind Left resize-pane -L 5 bind Down resize-pane -D 5 bind Up resize-pane -U 5 bind Right resize-pane -R 6 

I have to type the key binding strokes and the arrow keys to resize the pane each time. That's stupid. How to into some sort of the resizing pane mode and only tapping the key arrows till the size is satisfied and then exit the resize pane mode.

What will be the command/key mapping for your to "Enter the pan and can change size by only the arrow key mode" and "Exit this mode"

Thanks

0

2 Answers 2

4

Using existing keys

You might not need to add anything to your configuration at all. Here are the default bindings for resizing the panes (from :list-keys):

bind-key -r -T prefix M-Up resize-pane -U 5 bind-key -r -T prefix M-Down resize-pane -D 5 bind-key -r -T prefix M-Left resize-pane -L 5 bind-key -r -T prefix M-Right resize-pane -R 5 bind-key -r -T prefix C-Up resize-pane -U bind-key -r -T prefix C-Down resize-pane -D bind-key -r -T prefix C-Left resize-pane -L bind-key -r -T prefix C-Right resize-pane -R 

That -r option to bind-key means they support repeat -- you can keep hitting C-Arrow or M-Arrow until you've finished resizing, without entering the prefix sequence again. The amount of time before this repeat mode times out is controlled by the repeat_time option (default 500ms).

Binding with no prefix

If you want to avoid the prefix key entirely, you can use a different bind table. From the bind-key section of the manpage:

By default (without -T), the key is bound in the prefix key table. This table is used for keys pressed after the prefix key (for example, by default 'c' is bound to new-window in the prefix table, so 'C-b c' creates a new window). The root table is used for keys pressed without the prefix key: binding 'c' to new-window in the root table (not recommended) means a plain 'c' will create a new window. -n is an alias for -T root.

To bind directly into the root table:

bind-key -n C-Up resize-pane -U 5 bind-key -n C-Down resize-pane -D 5 bind-key -n C-Left resize-pane -L 5 bind-key -n C-Right resize-pane -R 5 

Binding in copy mode

You suggested a dedicated mode for resizing. tmux has no support for such custom modes, but it already has copy mode (by default, entered with prefix-[ and left with q) in which keypresses can have different bindings, typically without the prefix. Unfortunately, binding multiword commands (including commands with arguments) in this mode isn't supported (see tmux issue 215). However, there is a workaround of testing #{pane_in_mode} before doing anything. Based on the comments on that issue, and the above "bind with no prefix" solution:

bind-key -n C-Up if-shell -F "#{pane_in_mode}" "resize-pane -U 5" "send-keys C-Up" bind-key -n C-Down if-shell -F "#{pane_in_mode}" "resize-pane -D 5" "send-keys C-Down" bind-key -n C-Left if-shell -F "#{pane_in_mode}" "resize-pane -L 5" "send-keys C-Left" bind-key -n C-Right if-shell -F "#{pane_in_mode}" "resize-pane -R 5" "send-keys C-Right" 
1

Expanding on the answer by @JigglyNaga, you can define a custom key table and switch to it and back from it on demand. Within the key table you can use any key without prefixes. Here is an example config I use:

bind -T root F4 \ set prefix None \;\ set key-table resize \;\ set status-left '#[bg=#C678DD,fg=#2C323C](resize-#S)' \;\ set window-status-current-style bg=magenta,fg=black \;\ set status-style bg=#E06C75 \;\ refresh-client -S\;\ bind -T resize F4 \ set -u prefix \;\ set -u key-table \;\ set -u status-left \;\ set -u status-style \;\ set -u window-status-current-style \;\ refresh-client -S; bind-key -r -T resize j resize-pane -D 5 bind-key -r -T resize k resize-pane -U 5 bind-key -r -T resize h resize-pane -L 5 bind-key -r -T resize l resize-pane -R 5 bind-key -r -T resize J select-pane -D bind-key -r -T resize K select-pane -U bind-key -r -T resize H select-pane -L bind-key -r -T resize L select-pane -R 

This allows me to press F4 without any prefix to switch to "resize" key table. I also adjust the color and the name within the status so that I can easily see when I am in this mode. While in "resize" table, I can use just hjkl to adjust the sizes and Shift+HJKL to move between panes. Once I am happy with the resized windows, I can press F4 again to go back to regular tmux mode.

2
  • Thanks for sharing this great solution. It reminds me of how tiling window managers resize panes. If someone prefers using Prefix+r instead of F4, simply replace bind -T root F4 \ ... with bind r \ .... Commented Feb 11 at 7:48
  • 1
    @qadzek, yes, that was my inspiration for this approach :) Commented Feb 28 at 12:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.