23

I find myself often doing the same thing with tmux:

  1. cd to a given directory.
  2. tmux
  3. Rename window to what I'm doing.
  4. Split the window vertically 50%.
  5. Start one process in the left window.
  6. Start another process in the right window.
  7. Profit.

Is there a way for me to automate launching all of this so that I can run a single command and get the window I'm looking for?

1
  • What about a shell alias?
    – Marco
    Jan 15 '13 at 17:14
22

Archwiki saves the day! Session Initialization on the tmux page gives an example.

That said, instead of starting tmux as tmux, tmux new -s name will name the session when it starts instead of giving it a number.

Session initialization


You can have tmux open a session with preloaded windows by including those details in your ~/.tmux.conf:

new  -n WindowName Command
neww -n WindowName Command
neww -n WindowName Command

To start a session with split windows (multiple panes), include the splitw command below the neww you would like to split; thus:

new  -s SessionName -n WindowName Command
neww -n foo/bar foo
splitw -v -p 50 -t 0 bar
selectw -t 1 
selectp -t 0

would open 2 windows, the second of which would be named foo/bar and would be split vertically in half (50%) with foo running above bar. Focus would be in window 2 (foo/bar), top pane (foo).

Note: Numbering for sessions, windows and panes starts at zero, unless you have specified a base-index of 1 in your .conf

To manage multiple sessions, source separate session files from your conf file:

# initialize sessions
bind F source-file ~/.tmux/foo
bind B source-file ~/.tmux/bar
6

Rob's answer is great and explains exactly how to handle sessions from within tmux itself. Now, if you don't mind using a third-party tool, tmuxinator is a great project that helps create and manage sessions easily.

Each session is controlled by a config file that allows you to define all of your windows and panes along with their layouts and default commands to run within them. It also allows you to define a default project directory, specific socket, and daemons that are required to be running (e.g. mysql). To top it off, it provides an easy method to attach to them as well: simply run mux [session-name] and it will either start the session or attach to it if it is already running. I've been using it for a little while now and love it (thanks aziz!).

https://github.com/aziz/tmuxinator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.