0

I'm trying to make a script that will open up tmux and then split the screens and open up a few python instances but it will ony run the first command.

k   = PyKeyboard()

def cntrl_b(key):
    k.press_key('Control_L')
    k.tap_key('b')
    k.release_key('Control_L')
    k.tap_key(key)

def make_win():
    subprocess.call('tmux')
    subprocess.call(cntrl_b('%'))
    subprocess.call('clear')
    subprocess.call('"')
    subprocess.call('clear')
    subprocess.call('ipython')
    subprocess.call(cntrl_b('o'))
    subprocess.call('bpython')

if __name__=='__main__':
    make_win()

I'm calling 'clear' because I'm using powerline and I get error messages when I open tmux.

I've tested each command individually and they each work.

This code will just open tmux and not do any of the other commands.

2
  • 1
    You might want to check out a tool called tmuxinator that can be used to start multiple tmux sessions and send commands to them. I belive tmuxinator works by sending keystrokes to the parent terminal, not to individual shell sessions. Commented Mar 11, 2016 at 16:52
  • @HåkenLid Thanks for the to tip, I've been having a play around with it, it seems fun. Commented Mar 11, 2016 at 17:26

1 Answer 1

1

Your code runs 'tmux' and waits for it to exit. After tmux has exited, your program calls your function cntrl_b which will return None. That return value is used as the path of the next program to run when you pass it to subprocess.call(). This is clearly not what you want.

What you want is to run the tmux process, then send input to it. Something like this:

def make_win():
    proc = subprocess.Popen('tmux', stdin=subprocess.PIPE)
    # TODO: look up the bytes for Ctrl+B and put that in the string
    proc.stdin.write('Bo'); proc.stdin.flush()
    proc.stdin.write('clear\n'); proc.stdin.flush()
    proc.stdin.write('"'); proc.stdin.flush()
    proc.stdin.write('clear\n'); proc.stdin.flush()
    proc.stdin.write('ipython\n'); proc.stdin.flush()
    # TODO: look up the bytes for Ctrl+B and put that in the string
    proc.stdin.write('Bo'); proc.stdin.flush()
    proc.stdin.write('bpython\n'); proc.stdin.flush()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.