Skip to content

Instantly share code, notes, and snippets.

@jwintersinger
Created March 21, 2018 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwintersinger/4a1270af4d1c896a3ff23cc000204c31 to your computer and use it in GitHub Desktop.
Save jwintersinger/4a1270af4d1c896a3ff23cc000204c31 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
import subprocess
def move_window(oldpos, newpos):
return subprocess.check_output(['tmux', 'move-window', '-s', str(oldpos), '-t', str(newpos)])
def main():
output = subprocess.check_output(['tmux', 'list-windows'])
lines = [l.strip() for l in output.split('\n') if l.strip() != '']
frags = [line.strip().split(': ', 1) for line in output.split('\n') if line.strip() != '']
shell_command = 'bash'
frags.sort(key = lambda (idx, rest): rest if '* ' not in rest else shell_command)
free_pos = max([int(f[0]) for f in frags]) + 1
for newpos, (oldpos, _) in enumerate(frags):
move_window(oldpos, int(newpos) + free_pos)
active_idx = None
for newpos, (_, rest) in enumerate(frags):
actual_pos = newpos + free_pos
move_window(actual_pos, newpos)
if '* ' in rest:
active_idx = newpos
subprocess.check_output(['tmux', 'select-window', '-t', str(active_idx)])
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment