Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

tmux has the "move-window" command with which you can move a window to a specified empty index like so:

move-window -t 3

However, if index 3 is already taken you will not be able to move your current window to position 3.

I'm looking for an option that will allow me to move (insert) my window at position 3 even if it is already taken and then increase the window numbers for the other subsequent windows (for example the one that was originally at 3 will now be 4, the following will be 5 etc.)

share|improve this question

3 Answers 3

up vote 2 down vote accepted

The script below will allow you to do what you want. You can run it from a shell prompt or you can do:

:run "ins-move 2 5"

That example moves window two to the position before window five.

#!/bin/bash
for ((i=$1; i<$2-1; i++))
do
    tmux swap-window -s :$i -t :$((i+1))
done
share|improve this answer
    
Thanks! Works great - though I will have to adapt it to work even for cases like "5 2". At least now I know how it's done. –  user67834 Feb 18 '11 at 22:02
    
@Andrei: I'm glad I was able to get you started. Please don't forget to mark answers accepted and to upvote when you feel it's appropriate. –  Dennis Williamson Feb 18 '11 at 22:06
    
I upvoted your answer just now. Couldn't do it before because I just now got enough reputation points. –  user67834 Feb 18 '11 at 22:33

Try: new-window -a

using -a option when create new window can insert the new one just at the next index, while all other subsequent windows' index will increase automatically.

share|improve this answer
    
+1. Welcome to Super User! –  unforgettableid Jul 19 '13 at 4:51

I have got a solution without the need of external scripts. Put the following in your .tmux.conf:

bind i command-prompt -p 'Insert window at:' 'run-shell "if tmux select-window -t %1; then tmux new-window -a; tmux swap-window -s %1 -t \$((%1+1)); else tmux new-window; tmux move-window -t %1; fi; tmux select-window -t #I; tmux select-window -t %1;"'

Press [PREFIX]-i and provide wanted insert location. The last 2 select-window commands make sure the 'previous' window is set as expected and you go to the inserted window.

EDIT: ok, I spoke too soon. I had to change #I to #{window_id} because the window index changes for some windows on insertion. The new function (with added newlines for readability):

bind i command-prompt -p 'Insert window at:' '      \
    run-shell "                                     \
        if tmux select-window -t %1; then           \
            tmux new-window -a;                     \
            tmux swap-window -s %1 -t \$((%1+1));   \
        else                                        \
            tmux new-window;                        \
            tmux move-window -t %1;                 \
        fi;                                         \
        tmux select-window -t #{window_id};         \
        tmux select-window -t %1;                   \
    "'                                               

I started using a simplified version, through the following 2 commands in .tmux.conf:

 bind i command-prompt -p 'Insert window at:' 'new-window -a -t %1; swap-window -t -1'
 bind I command-prompt -p 'New window at:'    'new-window -t %1'

Prefix-i inserts a new window at position x when window x exists, and moves windows above that one up. Prefix-I creates a new window at position x when window x doesn't exist.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.