![]() ![]() nf: bind m command -p "Send window to:" "run -b '~/tmux-windowswap #I %1'" Then, just add this simple key-binding to your. Winlist=($(tmux list-windows | cut -d: -f1 | xargs | # start at highest indexed window # Smaller than everything slide it to the far left, then renumber Winlist=($(tmux list-windows | cut -d: -f1 | xargs)) # Bigger than everything slide it to the far right, then renumber Minwin=$(tmux list-windows | cut -d: -f1 | sort -n | head -n1)Įcho "Error: Bad window number specified." Maxwin=$(tmux list-windows | cut -d: -f1 | sort -nr | head -n1) ![]() # Function that provides improved window-swapping functionality for tmux If the window number is in-between the smallest/biggest numbers (i.e., presumably, matches an existing window number), it just swaps them with the swap-window command. This is done with the move-window command. It detects whether the destination window number is smaller than the smallest window number or bigger than the biggest window number, and if so, nudges all the other windows to the right or left (respectively) and re-numbers them to from 1 - N incrementally. None of the answers here satisfied me, so I wrote a script that (sort-of) mimicks the screen window-moving behavior for tmux. There doesn't seem to be a way around this.ĮDIT in 2020: Recent tmux versions require -d to keep the old behavior of moving the current window and staying with it in its new position. Thus, if you are on window #1, switch to window four and move it back one, you'll find that your last-used window is the new # 4 (formerly #3) instead of #1. The only issue I can find with this approach is that swap-window will implicitly alter the last-used window to the one you swapped with. By forcing a refresh of just the status bar (-S), you avoid this. If you're wondering about refresh-client -S, that's necessary because sometimes, while the reordering from move-window will work properly, the status bar won't update until further changes are made. It works similarly when moving the first window to the end by picking a huge number you'll never use, it ensures that when move-window -r fires again everything will be numbered like you'd expect. This works by temporarily moving the last window to the unused index-0 and then calling move-window -r to renumber them starting from 1 again. If you are using base-index 1 like me and you don't think you'll ever go above 999 windows, you can use a little trick to make it roll properly, though the commands bloat a bit: set -g base-index 1īind-key -n M-Left run-shell 'if tmux list-windows | head -n 1 | grep -q active then tmux move-window -t 999 \ move-window -r \ refresh-client -S else tmux swap-window -d -t -1 fi'īind-key -n M-Right run-shell 'if tmux list-windows | tail -n 1 | grep -q active then tmux move-window -t 0 \ move-window -r \ refresh-client -S else tmux swap-window -d -t +1 fi' This can easily be combined with base-index and renumber-windows to have a list of windows that start at an arbitrary number and never has any gaps. So, the commands I use stop working when the window has reached the edge of the list: bind-key -n M-Left run-shell 'tmux list-windows | head -n 1 | grep -q active || tmux swap-window -d -t -1'īind-key -n M-Right run-shell 'tmux list-windows | tail -n 1 | grep -q active || tmux swap-window -d -t +1' I did this because, when you issue a swap +1 at the last window (or swap -1 at the first window), it will still swap, instead of looping back around again like you might expect: 0:one 1:two 2:three 3:zero* The approach I use combines a bit of Ashish's answer with piec's I have alt-left and right arrow bound to a quick little shell callout that moves the window one to the left or the right, unless it is the first or last window, respectfully.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |