Sunday, February 5, 2017

Windows terminal automated tasks with conemu

I'm a Ubuntu fan and I recently shift into Windows. For me the main issue was the terminal in Windows, in Ubuntu there were some great tools to handle multiple tasks at once such as tmux, vim etc .. . I mostly used to tmux to handle multiple windows and split terminal horizontally and vertically to run multiple tasks.


Figure 1: ConEmu running multiple tasks on different windows powershells

Finally my dream came true for windows. This great ConEmu tool helped me to handle the same thing that tmux does and much more.

The best thing is in ConEmu you can write task to automate multiple tasks like in Figure 1 that you can run it any time within a click. The screenshot shows 4 different tasks running in 4 splited tabs.
  • Vim editing a file
  • Running a node server
  • Running a test suite
  • Running a git status
Writing a automated task like above is super easy. I'll explain how to write the Figure 1 task inside ConEmu.
Prerequisites: 
  • Download ConEmu :)
  • Hope you have installed vim for windows. (If not you can replace notepad command for vim)
  • I have used react-boilerplate project for this scenario. 
  • After you cloned the above project you may need to execute npm run setup command.
Now lets dig in to task creation.

This is the complete task for Figure 1 setup

powershell -new_console:d:C:\Users\noely\Documents\development\learn\react\react-boilerplate -noexit vim .\app\index.html

powershell -cur_console:s50H:d:C:\Users\noely\Documents\development\learn\react\react-boilerplate -noexit npm start

powershell -cur_console:s50V:d:C:\Users\noely\Documents\development\learn\react\react-boilerplate -noexit npm run test:watch

powershell -cur_console:s50V:d:C:\Users\noely\Documents\development\learn\react\react-boilerplate -noexit git status

If you want to run this task add this task to ConEmu under Settings > Startup > Tasks. To open settings use Win+Alt+P. I have created a task called react-dev.


Figure 2: Adding above automated task to ConEmu

Detailed explanation can be found here.

  • According to the Figure 1 screenshot you can see left terminal windows is running a vim editor.
  • Note ConEmu can run any installed terminal such as cmd, powershell, gitbash (if you have already installed in the system)
  • In this case I will use powershell console to run the vim editor.
  • If you can see in first command I have used -new_console command it's to create new powershell window, so then we can use that same window to split and run multiple tasks.
  • After the -new_console command I have specified another command called :d  which is used to set a initial directory path to start the new poewershell console.
  • What about the -noexit flag, It's for telling that do not exit the console after the task is done. (ex: if you kill the server it wont remove the terminal from the window). Even though you are done with that terminal you can do any other task on that, 
  • Then I have add a open a file command in .\app\index.html using vim editor (vim file_path)
  • Now lets dig in to 2nd command, there I have used -cur_console which represents newly created console to run new commands.
  • Then I have split current terminal window to 50% horizontally using :s50H. V  - vertical split 
  • Same as previous I have used d: to navigate to the working directory to start commands such as
    • npm start (start server)
    • npm run test:watch (run test task)
    • git status
Now It's time to run the automated task
                                               Figure 3: Running the automated task


I hope you understood creating a simple automated task in ConEmu tool. You can refer their doc and automate any task using current terminal.