Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tmux resize on focus

I'd like to be able to specify a secondary size parameter for a particular pane so that it assigns the new size upon focusing the pane, and returns it upon exiting. So e.g.

(Note [] represents focused terminal cursor)

________________
|$ ls  |       |
|a's   |       |
|dir   |       |
|$     |$ []   |
|______|_______|

Swap pane focus

_______________
|        |    |
|$ ls    |    |
|a's dir |    |
|$ []    |$   |
|________|____|

And so on. Especially gonna be cool since resizing panes in recent tmux versions has it do a great job re-flowing the content rather than slicing it off.

In the example, the left pane has its width set to automatically switch to being 8 columns when it is focused, and it got squished thinner when it lost focus. Notice how the content is still visible (this is afterall why we love tmux) but we can still eat our cake too by letting the currently focused pane expand itself automatically so it's always big enough to do work in!

I can probably come up with some elaborate binds to automate the application of resize-pane commands to do this, and make it just the right amount of elaborate to suit my wishes. But I was hoping there was some kind of built-in feature for this.

It would be practical to track and allow the modification of an auxiliary 4-tuple of integers for each pane. These specify the amount of resize-pane -L/D/U/R operations to do upon that pane's focusing, and the reverse direction upon that pane's defocusing.

There may be other, more reasonable formulations of this.

like image 337
Steven Lu Avatar asked Jun 02 '13 03:06

Steven Lu


People also ask

How do you zoom in on tmux?

Wrangle your terminal with tmux We can do this with control-B and then Z for zoom.

How do I fullscreen tmux?

It's a shortcut that toggles between maximizing the current pane (e.g. filling the entire terminal window) and restoring the previous layout. By default that shortcut is <prefix> z . In vanilla tmux <prefix> is Ctrl-b , therefore the shortcut is Ctrl-b z .

How do you split horizontal tmux?

ctrl + b + % to make a vertical split. ctrl + b + " to make a Horizontal split. ctrl + b + left arrow to move to the left pane. ctrl + b + " to make a Horizontal split.


2 Answers

Found a partial solution... Ive got my pane switching synced with vim, so whenever I switch switch panes, I also resize tmux.

This is not optimal because tmux is not verifying the current pane size and resizing it accordingly. Anyway, it works pretty well since you can do ctr-h or ctrl-k twice to resize the panes size.

The relevant config is the following:

# Sync panes with vi
bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys C-h) || tmux select-pane -L && tmux resize-pane -R 30"
bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys C-l) || tmux select-pane -R && tmux resize-pane -L 30"
bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys C-j) || tmux select-pane -D"
# Move panes up and down, wont resize
bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys C-k) || tmux select-pane -U"
like image 115
golfadas Avatar answered Oct 01 '22 12:10

golfadas


Sorry, this only amounts to speculation so far, but I thought about it some, and I think there is functionality that is built in to tmux that can support this but it'll require some/a lot of scripting.

See this question I posted.

http://sourceforge.net/p/tmux/mailman/message/31221459/

Essentially it looks like the select-layout command can (possibly!??!) be used to apply an arbitrary mutation to the layout. I've not tested if it works, though.

Thomas Adam suggested to look in the layout-custom.c source to find out more about what's going on. That's about as far as I got. But if indeed it is possible to programmatically mutate the layout and generate working strings to pass to select-layout, then this would be a very good approach.

Update: Now I have asked this directly (whether just generating a suitable string can indeed achieve resizing to arbitrary layouts), but not yet received a response, from Mr. Adam himself, but the reason why I suspect that this can work if we can generate a string that passes the checksum is that I am able to resize my panes any which way, and change them away once I record the string (yielded by the list-windows command), and then afterwards restore to the recorded layout with the string. This means that there is not some sort of explicit action that must be taken to like "save" the layout or anything, it seems like the checksum is just some kind of clever way to help prevent garbage/pathological strings from wreaking havoc with the string interpreter that does the pane layout rearrangement.

You definitely need to pass a string that is nontrivial to construct in order for this to work. However, the code for computing the checksum and the rest is plain to see in layout-custom.c. One day I'll probably be back with a shell script (or just a C program, if it's possible to lift out the code) that implements suitable transformations. Should be fun.

Yes it would be awesome for me to crank this out and pick up that nice little bounty, but I have Real WorkTM waiting for me tonight unfortunately.

like image 45
Steven Lu Avatar answered Oct 01 '22 14:10

Steven Lu