Automate your iTerm

Dien Bui
2 min readMar 28, 2022
iTerm

If you use macOS and you are a software engineer, you may know about iTerm already. Well, I won’t mention about how to use iTerm and basic features 😁 . What I want to share is how to automate your iTerm using code.

Let’s imagine you’re developing microservices on your local machine and you want to open iTerm, run all the necessary commands to start services and tail logs. It would be fine for few services, but if you have like more than 5 and you manually click on many keys on keyboard to start them all, you will soon be tired and find it time-consuming.

Let’s dive into how to automate those steps or similar ones on iTerm step-by-step using Python!!!

Install runtime

  • On the top bar, click Scripts > Manage > New Python Script
  • You will be prompted to install Python runtime environment and you should follow to install

Create & run your first script

Once finished, try the 1st step above to create your first Python script

  • iTerm will ask you types of script you want to create a template for you
  • Open your script in any text editor
  • For my example, I will show you how to create scripts that will create new tab and within that tab I split it into 4 panes and echo some texts
  • Once done, save it and head back to iTerm
  • Click on Scripts and you’ll see your script, then click on it
new script listed in Scripts section
  • You will notice something new on iTerm like the image below. Voila, now you finally have it, new tab with panes without so much effort in typing 😁

Discover more

  • For more available APIs and details, head to official documentation at https://iterm2.com/python-api/
  • The location of scripts can be revealed by heading to Scripts > Manage > Reveal Scripts In Finder

I hope that this post can give you a glimpse of the potential of iTerm and how it can boost your daily work and productivity. Cheers and happy coding!!!

Note: there’s no copyright for any piece of code above and they’re totally open source. Please feel free to copy as your own and modify if needed.

--

--

Dien Bui

Backend, data and cloud computing enthusiast | Power of open-source and sharing believer | Senior Platform Engineer @ NAB | Co-founder of Project Cocoon