Skip to content

Instantly share code, notes, and snippets.

@poloxue
Last active September 15, 2023 10:39
Show Gist options
  • Save poloxue/37d3d79b35964ab8d885296b84ab4b5a to your computer and use it in GitHub Desktop.
Save poloxue/37d3d79b35964ab8d885296b84ab4b5a to your computer and use it in GitHub Desktop.
A small python script for enter a tmux session effectively.
#!/usr/bin/env python
import os
output = os.popen("tmux ls -F \\#{session_name}").read()
sessions = output.strip().split("\n")
print("Sessions:")
for index in range(len(sessions)):
print(f"{index} - {sessions[index]}")
input_value = input("Please select a session <Index or Name>(default):")
if input_value.isdigit():
sess_name = sessions[int(input_value)]
elif not input_value:
sess_name = "default"
else:
sess_name = input_value
if sess_name not in sessions:
answer = input(f"New a session `{sess_name}`?(Y/N)")
answer == "Y" and os.system(f"tmux new -s {sess_name}") # pyright: ignore
else:
os.system(f"tmux attach -t {sess_name}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment