📜 ⬆️ ⬇️

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

Good day. More recently began to learn Ruby on Rails. Before that, I programmed in c # (asp.net). Switching to Ruby also meant switching to Ubuntu and working closely with the terminal. Ruby is studying this online tutorial: railstutorial.ru . On board, I have Ubuntu 13.04.

Working with Ubuntu's RoR baud 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 (forgive me for Linux terminology). In the picture it looks like this:


After installing tmux ( sudo apt-get install tmux ) and learning the basics, I fell in love with this multiplexer. Everything in it is good. There is one “but” - after each reboot, you need to 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 in a separate script via cron. For me, as a beginner, all these dances with a tambourine were difficult, not understandable and not functional. Thinking, I decided to write a bash-script that would launch a new tmux session with the 3 windows I needed and took several parameters.
Parameters wanted such:


After reading the tmux manual 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' 

It's simple. I added several abbreviations for simplified access to my project (sample_app), as well as several abbreviations for tmux.

Then I created the tmux-s file in the / bin folder ( sudo touch tmux-s ) and made it executable ( sudo chmod +x tmux-s ). As a matter of fact, in this file there will be a code that will raise the tmux session with the panels and windows I need, and also take the necessary parameters. The file contents 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}" Cm tmux send-keys -t "${session_name}":"${first_window_name}".2 "${base_pane_command}" Cm tmux send-keys -t "${session_name}":"${first_window_name}".3 "${base_pane_command}" Cm 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 Cm } ####################### # Guard start guard_tests_start() { tmux send-keys -t "${session_name}":"${first_window_name}".3 Cm } ####################### # Open project in Sublime Text open_project() { tmux send-keys -t "${session_name}":"${first_window_name}".1 "${project_open_command}" Cm } ####################### # Ruby console start ruby_console_start() { tmux send-keys -t "${session_name}":"${first_window_name}".1 "${console_open_command}" Cm } ####################### # 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 for correct work, I advise you to create a file ~ / .tmux.conf and enter the settings in it from here: user tmux settings

Everything. Open a new terminal, type tmux-s and 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 set up aliases correctly from ~ / .bash_aliases so that the paths are assigned to your project. And remember, the script is written to help those who learn from the book railstutorial.ru . If you do not have Guard, the script will not work correctly with the -t parameter. Also install Sublime Text.

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

Source: https://habr.com/ru/post/217339/


All Articles