iTerm2: Showing the PHP, Ruby or Node version in your status bar
Version 3.3.0 of iTerm2 introduces a cool new feature that allows you to customise the status bar. The status bar allows you to modify the top of your prompt to show information about your terminal session, information that’s always present as you’re working.
There are some frequently useful pieces of information that us developers like to know as we’re navigating inside a project, such as what branch we’re on or our present working directory. Typically this is done by modifying the bash prompt to show this information before the caret. This method is challenging to manage and substantially slows down the instantiation of the prompt, sometimes adding seconds on to the start time of a new shell.
I’ve started using this to show me the following useful information:
- The current branch that I’m working on
- The directory I’m in
- The current PHP version
- The current Ruby version
- The current Node version.
Getting this configured wasn’t as straight forward as I imagined to get working, so please find instructions on getting this working below.
Requirements
To use this feature, you’ll need:
- iTerm with a minimum version of 3.3.0
- Have installed the “Shell Integration” (click “Install Shell Integration” in the menu)
Configuration File
For this to work, you need to define a shell function for iTerm2 to call. The example below is for bash, but there are instructions for your shell.
1
2
3
4
5
6
7
# See https://www.iterm2.com/3.3/documentation-scripting-fundamentals.html
function iterm2_print_user_vars() {
iterm2_set_user_var phpVersion $(php -v | awk '/^PHP/ { print $2 }')
iterm2_set_user_var rubyVersion $(ruby -v | awk '{ print $2 }')
iterm2_set_user_var nodeVersion $(node -v)
}
This creates three new variables: user.phpVersion
, user.rubyVersion
and user.nodeVersion
.
Configuring the Status Bar
You can now use these variables with the “string interpolation” component for the status bar.
You can edit the status components by:
- Navigating to the iTerm2 preferences
- Clicking “Profiles”
- “Session”
- Scroll down and click “Configure Status Bar”
You can use the inbuilt components for the branch and directory display. To use your variables, create three “Interpolated String” components and drag them where you want them.
Click “Configure Component” and use the variables in your output, e.g. PHP: \(user.phpVersion)