- ITerm2 version: Build 3.2.6 OS version: 10.14.2 plist: com.googlecode.iterm2.plist Attach a debug log, if possible: debuglog.txt Detailed steps to reproduce the problem: Open iTerm What happened: ScreenShot2018-12-29at4.21.37PM What should have happened: An iTerm.
- ITerm2 is a replacement for Terminal and the successor to iTerm. It works on Macs with macOS 10.14 or newer. ITerm2 brings the terminal into the modern age with features you never knew you always wanted. Why Do I Want It?
- Prior to iTerm2 3.3.0, only the window name was ever used in the UI. In iTerm2 3.3.0 and later with tmux 2.9 or later, you can choose to show tmux window titles in native tab and window titles. Put this in your /.tmux.conf file: set-option -g set-titles on set-option -g set-titles-string '#T'.
Since you're using iTerm2 and looking to change the ssh:// handler, I'll assume you know your way around your Mac and you can figure out how to install that tool yourself. Once you've got it installed, go to your System Preferences, search for Default Apps at the bottom and open it. In there, go to URLS and find the 'ssh' option. ITerm2 is a replacement for Terminal and the successor to iTerm. It works on the recent versions of macOS. Its focus is on performance, internationalization, and supporting innovative features that make your life better, while maintaining a lot of options and settings to customize the terminal.
Writing iTerm2 Python scripts
iTerm2 is a very powerful piece of software and includes a plugin system that allows you to write Python scripts that terminal programs can take advantage of. In this post, we're going to write two Python scripts for iTerm2: one to automatically switch to/from Dark Mode, and the second to play a sound on your local speakers when an escape sequence is printed, even over SSH. I'll also share a few bonus snippets that take advantage of some of iTerm2's lesser known features.
Getting Started
Let's build a simple 'Hello, World' iTerm2 script to get started. First, enable the Python API in 'Preferences ► General ► Magic ► Enable Python API'.
Once enabled, go to 'Scripts ► Manage ► New Python Script'. If you've never done this before, iTerm2 will automatically ask to download the Python runtime. Once that's downloaded, answer 'Basic' and 'Simple' to the two questions, then save the file to the default directory (~/Library/Application Support/iTerm2/Scripts
). Take the template that iTerm2 created for you and add a print
statement to it after the async_create_tab
call.
Now in iTerm, select your script from the Scripts menu. If everything is working, your active window should open up a new tab.
Debugging these scripts is possible with the iTerm2 console. To open it, use 'Scripts ► Manage ► Console'. You'll see all of your scripts, including those which have crashed or finished. Clicking on the latest one, we should see the output from the script, including the 'Created a tab!' message that we added. We can also use the 'Terminate' and 'Restart' buttons to control the script. Note that you'll need to manually restart the script any time you make a change to it.
Once everything is ready, we could move our script to the Scripts/AutoLaunch
directory to have it be started automatically by iTerm2 when iTerm2 launches. That isn't useful for the one-off script from this example, but you will probably want to do this for long-running scripts like the next two.
Automatic Dark Mode
To have iTerm2 automatically switch between a light and a dark theme following your system setting, follow these steps:
Go to Preferences ► Profiles ► Colors ► Color Presets. and set up your preferred light and dark presets.
Malwarebytes premium 3 2 36. Download DarkMode.py into your AutoLaunch directory.
Update the
async_handle_theme
function to use the correct Color Presets from step 1.Manually launch the script for the first time using 'Scripts ► AutoLaunch ► DarkMode.py' (or restart iTerm).
I'll walk you through the important parts of the code. Starting in the main
function, you can see we set up a VariableMonitor on the effectiveTheme variable. This will allow us to detect when the system toggles Dark Mode. Note that the main
function also checks the theme once on startup, so if Dark Mode was toggled while iTerm was not running, the change will be picked up immediately.
The async_handle_theme
function simply looks at the theme, which will be something like 'dark', 'light highContrast', 'dark minimal', etc., and decides which color scheme to use before passing it to async_set_color_preset
.
The main workhorse is the async_set_color_preset
function. The first part loops over all of the saved profiles in iTerm2, which are called PartialProfiles. You can see in my screenshot above that I have just one named 'Default'. By setting the color scheme for each profile, when a new session is opened it will have the correct color scheme applied.
The second part loops over all tabs of all windows and sets their color scheme. This is causes all of the current session to switch to the correct color scheme when the system switches to/from Dark Mode. It does this by creating a LocalWriteOnlyProfile which will just contain the necessary changes to the colors, and then applying those changes with async_set_profile_properties
.
Once you've set up the color schemes and installed this script, it'll work silently. If anything isn't working, check the iTerm2 script console first to find out why.
Playing sounds with iTerm
This script allows you to write a short shell script to play an audio file on your local computer (or anything else, even to hit a physical bell), even over an SSH session. It works by using iTerm2's custom escape sequences to run a Python function. To install this script:
- Download PlaySound.py into your AutoLaunch directory.
- Replace the
LIBRARIES
constant with a list of directories that contain audio files you'd like to play. - Manually launch the script for the first time using 'Scripts ► AutoLaunch ► PlaySound.py' (or restart iTerm).
1password 6 8 8. Our new custom escape sequence can be triggered using this bash snippet:
You can replace the 'Glass' with the name of any sound file in one of the LIBRARIES
directories. If you have a MacOS machine, the above snippet should make this sound:
The source code to this is really simple. Let's first look at the main
function: it uses a CustomControlSequenceMonitor to listen for our sequence, and then it calls our locate_sound
function to resolve the value (e.g. Glass
) to a filename (/System/Library/Sounds/Glass.aiff
), and finally calls a program to play the sound.
Our locate_sound
function simply searches for files with a matching filename in one of the configured directories.
A quick note about security here. The iTerm docs recommend that the ID (COMMAND
in this script) should be a password rather than a well-known string. This is because knowing the ID is the only access control that the file has. For example, if you run curl malicious.site/textfile
and that file contains the escape sequence, it would trigger your script. For something silly like playing sounds, this isn't a big concern, but it's certainly worth keeping in mind when expanding on this idea.
Bonus: Little-known iTerm2 features
This next section doesn't require any Python scripts at all: everything is built into iTerm2. iTerm2 includes many proprietary escape codes that we can take advantage of using some simple shell scripts. You can add these scripts to your .bash_profile
to use them interactively, or include them in scripts you write.
The iterm_notify
function sends a system notification with the provided text.
The iterm_bounce
function causes the Dock icon for iTerm2 to, well, bounce.
iterm_bounce example
Finally, the iterm_badge
function adds a text label to the background of your terminal. This is useful for labeling tabs when you have many of them open.
Wrapping up
With these scripts you can make iTerm2 more useful for you. Next time, we're going to look at some ways to get the most use out of these scripts.
How to configure ranger image preview on OSX with iTerm2? Ranger's image preview in iTerm2 does not work out of the box and you will need some additional scripts and config settings to get it working. Here you can see step by step how it gets done.
TL;DR
Install iTerm >= 2.9 and…
Outline
- Requirements
- Installation
- iTerm
- Ranger
- imgcat
- Dependencies
- Configuration
- Known Problems
- Image display
- tmux
- Ranger in action
- Further readings
1. Requirements
You need to make sure to meet the following requirements.
- iTerm >= 2.9
As of now, the stable release of iTerm2 does not support image preview so you will have to download the test version here.
Also note to get the full range of previews in ranger you will need the following optional tools:
- file for determining file types
- The python module chardet, in case of encoding detection problems
- sudo to use the 'run as root'-feature
- img2txt (from caca-utils) for previewing images in ASCII-art
- highlight for syntax highlighting of code
- atool for previews of archives
- lynx, w3m or elinks for previews of html pages
- pdftotext for pdf previews
- transmission-show for viewing bit-torrent information
- mediainfo or exiftool for viewing information about media files
2. Installation
2.1 iTerm
- Download iTerm2 test version here.
- Install
2.2 Ranger
On OSX most cli applications can be installed using homebrew. So we first have to install it and then simply use it to install ranger itself.
2.3 imgcat
Download the latest copy of imgcat from github and put it into a bin directory which is inside your $PATH
variable. (What is PATH)
If you have a ~/bin
directory, just put it there and it will work for your user only, otherwise put it into /usr/local/bin
for all users.
2.4 Dependencies
For all ranger previews you can optionally install the above listed dependencies via homebrew:
3. Configuration
If you are using ranger for the first time, generate the ranger config files.
Now you can go to ~/.config/ranger
and see what files have been generated:
Let's go over them quickly:
commands.py
: Commands which are launched with:
commands_full.py
: Full set of commandsrc.conf
: Configuration and keybindingsrifle.conf
: File associations (which program to use for opening files)scope.sh
: Responsible for various filet previews
Currently the only important file to us is rc.conf
. Open it in your favorite editor and change the following two lines to look like this:
Now you are all set and can enjoy to have ranger image preview on iTerm2. Google apps for my computer.
On OSX most cli applications can be installed using homebrew. So we first have to install it and then simply use it to install ranger itself.
2.3 imgcat
Download the latest copy of imgcat from github and put it into a bin directory which is inside your $PATH
variable. (What is PATH)
If you have a ~/bin
directory, just put it there and it will work for your user only, otherwise put it into /usr/local/bin
for all users.
2.4 Dependencies
For all ranger previews you can optionally install the above listed dependencies via homebrew:
3. Configuration
If you are using ranger for the first time, generate the ranger config files.
Now you can go to ~/.config/ranger
and see what files have been generated:
Let's go over them quickly:
commands.py
: Commands which are launched with:
commands_full.py
: Full set of commandsrc.conf
: Configuration and keybindingsrifle.conf
: File associations (which program to use for opening files)scope.sh
: Responsible for various filet previews
Currently the only important file to us is rc.conf
. Open it in your favorite editor and change the following two lines to look like this:
Now you are all set and can enjoy to have ranger image preview on iTerm2. Google apps for my computer.
4. Known Problems
4.1 Image display
When the images do not show up correctly you will need to alter rc.conf
and set draw_borders
to true
:
4.2 tmux
The problem I have encountered and haven't solved so far is that this does not work inside a tmux
session. So if anybody knows how to make ranger preview images with iTerm2 inside tmux
, please let me know.
5. Ranger in action
Here are a few slides to see ranger in action:
ranger
Iterm2 For Windows
6. Further readings
If you want to know how to integrate ranger into vim as a file explorer with all its cool features, read: Use ranger as a file explorer in vim.
Iterm2 Setup
_ Empires and puzzles trainer hero.