Setting up Tmux environment


Tmux is a fast and easy to use terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more.

It's an extremely useful piece of software, especially when working with remote server through SSH or just during some development work where you need to have a quick access to code, database, debugger and more in the same terminal window.

Tmux installation

  • Unix:

            
    $ sudo apt-get install tmux
            
        
  • Mac:

            
    $ brew install tmux
            
        

Tmux options

There is a lot of them. I will describe only the one we need to create Bash script for this post. For the full reference check the official documentation.

Tmux run internally a server that drives all the created sessions. To run the server you need to simply execute

	
$ tmux
    

By default, to enter command mode, type inside the Tmux session

	
Ctrl + b
    

There is also a useful command to show all available key binds and list all Tmux features

	
Ctrl + b ?
    

To exit the help mode press esc.

Start session with custom settings

That was the basics, but the post is about starting session with custom settings. Tmux accepts command line arguments so you can drive session from outside. This allows to create simple bash script and specify how you want your session to look like after it's started.

To create session

	
$ tmux new-session -s development -n server -d
    

Where

  • -s specifies session name
  • -n specifies window name
  • -d prevents session being attached automatically to the current terminal window

To list running sessions use

	
$ tmux ls
development: 1 windows (created Sat Mar  5 09:23:18 2016) [151x40]
    

To check if the given session exists

	
$ tmux has-session -t session_name
    

To attach session

	
$ tmux a -t session_name
    

Now we start setting up first Tmux pane

	
$ tmux send-keys -t development 'cd /home' C-m
    

Where

  • send-keys send specified key or keys to the pane
  • -t target session
  • C-m run passed comand

You can send many commands at once or send them this way one after another.

Last useful tip explains how to split window into multiple panes

	
$ tmux split-window -h -t development
    

Where

  • -h split horizontally (use -v to split vertically)
  • -t target session

Summary

That's all the basic commands you need to know. By combining them all into one script you can start and set up your dev/server environment in one go. Let's see a full example

	
#!/bin/sh

session_name=dev-session

tmux has-session -t $session_name
if [ $? != 0 ]; then
    tmux new-session -s $session_name -n server -d
    tmux send-keys -t $session_name 'cd /home' C-m
    tmux send-keys -t $session_name 'vim' C-m

    tmux split-window -h -t $session_name
    tmux send-keys -t $session_name 'cd /home' C-m
    tmux send-keys -t $session_name 'ls -la' C-m

    tmux split-window -v -t $session_name
    tmux send-keys -t $session_name 'cd /home' C-m
    tmux send-keys -t $session_name 'python' C-m
fi

tmux a -t $session_name
    

Now run the script and check the results (To run from console ./run_tmux.sh, add executable permission chmod +x run_tmux.sh).