Launching a tmux window adapted for convenient work with Ruby on Rails

Good afternoon. Recently, he began to study Ruby on Rails. Prior to this, I programmed in c # (asp.net). Switching to Ruby also meant switching to Ubuntu and working closely with the terminal. Ruby is learning from this online tutorial: railstutorial.ru . On board I have Ubuntu 13.04.

Working with RoR baud Ubuntu involves many terminal windows. In one rails server, in another Guard with Spork, in the third Ruby console, etc. On the Internet, I came across a program called tmux . Without going into details, I’ll say that it allows you to work with several consoles in one terminal window (for example, Linux forgive me for terminology). In the picture it looks something like this:


After installing tmux ( sudo apt-get install tmux) and learning the basicsI fell in love with this multiplexer. Everything about him is good. There is one “but” - after each reboot, you must reconfigure the tmux window. And this is 5 extra minutes! Is it really impossible to save the state of tmux windows!

On the Internet, several solutions were found that suggested saving the session as a separate script through cron. For me, as a beginner, all these dances with a tambourine were complicated, not understandable and not functional. After thinking, I decided to write a bash script that would launch a new tmux session with the 3 windows I needed and take several parameters.
Parameters wanted these:
  • launching without parameters creates a new tmux session, a new window and splits it into three panels (focus in the first window of the first panel)
  • -s launches a server in the second panel of Rails
  • -t launches Guard + Spork in the third panel (TDD RSpec environment)
  • -o opens my project in my favorite Sublime Text 3 editor
  • -c in the first panel the Rails console starts


After reading the manual on tmux and learning a little about bash scripts, I added the following lines to ~ / .bash_aliases:
alias rapp='cd ~/work/ruby/Apps/sample_app'
alias rappo='cd ~/work/ruby/Apps/sample_app && subl .'
alias rapps='/bin/bash --login'
alias tmux-k='tmux kill-session'
alias tmux-ko='tmux kill-server'
alias tmux-l='tmux ls' 

Everything is simple here. I added a few shortcuts for easier access to my project (sample_app), as well as a few shortcuts for tmux.

Next, I created the tmux-s file in the / bin ( sudo touch tmux-s) folder and made it executable ( sudo chmod +x tmux-s). As a matter of fact, this file will contain the code that will raise the tmux session with the panels and windows I need, as well as accept the necessary parameters. The contents of the file are as follows:

#!/bin/bash
#######################
# Constants
flag='0'
#######################
# Config Variables
session_name="rails"
first_window_name="first"
second_window_name="second"
base_pane_command="rapp && rapps"
server_start_comand="rails s"
tests_start_comand="guard"
project_open_command="rappo"
console_open_command="rails c"
#######################
# Addpanes
add_panes() {
  tmux split-window -h -t "${session_name}"
  tmux split-window -v -t "${session_name}"
  tmux send-keys -t "${session_name}":"${first_window_name}".1 "${base_pane_command}" C-m
  tmux send-keys -t "${session_name}":"${first_window_name}".2 "${base_pane_command}" C-m
  tmux send-keys -t "${session_name}":"${first_window_name}".3 "${base_pane_command}" C-m
  tmux send-keys -t "${session_name}":"${first_window_name}".2 "${server_start_comand}"
  tmux send-keys -t "${session_name}":"${first_window_name}".3 "${tests_start_comand}"
}
#######################
# Tmux server start
tmux_session_start() {
  if [ "${flag}" != '1' ] ;then
    echo "Session ${session_name} start..."
    tmux new-session -s ${session_name} -n ${first_window_name} -d
    add_panes
  fi
}
#######################
# Rails server start
rails_server_start() {
  tmux send-keys -t "${session_name}":"${first_window_name}".2 C-m
}
#######################
# Guard start
guard_tests_start() {
  tmux send-keys -t "${session_name}":"${first_window_name}".3 C-m
}
#######################
# Open project in Sublime Text
open_project() {
  tmux send-keys -t "${session_name}":"${first_window_name}".1 "${project_open_command}" C-m
}
#######################
# Ruby console start
ruby_console_start() {
  tmux send-keys -t "${session_name}":"${first_window_name}".1 "${console_open_command}" C-m
}
#######################
# error_param
error_param() {
  tmux kill-session
  echo "Session ${session_name} killed..."
  echo "Parameter is incorrect! Avaliable parameters: 
      -s for start Rails server
      -t for start Gusrd server
      -o for open project in Sublime Text
      -c for open Ruby concole"
  exit 1
}
if [ ! ${1} ] ;then
  tmux_session_start
fi
while [ ${1} ] ;do
  case ${1} in
    -s)
      tmux_session_start
      rails_server_start
      flag='1'
      shift 1
    ;;
    -t)
      tmux_session_start
      guard_tests_start
      flag='1'
      shift 1
    ;;
    -o)
      tmux_session_start
      open_project
      flag='1'
      shift 1
    ;;
    -c)
      tmux_session_start
      ruby_console_start
      flag='1'
      shift 1
    ;;
    *)
      error_param
    ;;
  esac
done
echo "Success!"
tmux select-pane -L -t "${session_name}"
tmux resize-pane -R -t "${session_name}":"${first_window_name}".1 25
tmux new-window  -n "${second_window_name}" -t "${session_name}"
tmux select-window -t "${session_name}":"${first_window_name}"
tmux attach -t "${session_name}"

The Config Variables block contains user settings

Also, to work correctly, I advise you to create the ~ / .tmux.conf file and enter the settings in it from here: tmux user settings

All. We open a new terminal, type in it tmux-sand get a convenient tmux window, ready to work with Ruby on Rails. You can play with the parameters - they also work. It is only important to properly configure aliases from ~ / .bash_aliases so that the paths are written to your project. And remember, the script is written to help those who study on the book railstutorial.ru . If you do not have a Guard, then the script will not work correctly with the -t option. Also install Sublime Text.

Please do not judge strictly. From beginner to beginner. I killed it for a day and decided to share it (I didn’t find such instructions on Russian-speaking resources). Adequate criticism is welcome (I'm new to linux, so I'm sure the code is far from normal).

Also popular now: