Skip to main content
vim

iTerm Semantic History with Neovim

Typecraft

Article by: .Nutch

PSA for those who use iTerm and Neovim

iTerm has a profile setting called "semantic history" that allows you to define the behavior of cmd-clicking on a file name in the command line. By default, it will open the default app for the file. But instead, I found myself wanting to open things in Neovim like 95% of the time. Not only is this possible, but you can even set it to automatically jump to the correct line number if one exists! Here is the solution:

Navigate to Settings > Profiles (pick your profile) > Advanced and scroll down to Semantic History. In the dropdown, select Run coprocess... and in the "Enter command" textbox, enter the following command:

[ -z "\2" ] && echo nvim \1 || echo nvim +\2 \1
Setting up Configuration

Demo

Here is a quick demo of the cmd-click functionality this enables...

Optional Filesystem Navigation

I am a chaotic individual and decided to make another version of this command with the added functionality of navigating my filesystem with my mouse using chained ls outputs. This enables navigating directories and opening files simply by cmd-clicking output from the ls command. It does this by calling cd if the cmd-clicked path is a directory, and then it calls ls again. Also, with this version, Neovim can be opened in the current working directory if the PS1 prompt is cmd-clicked.

Here is the altered coprocess command for the Semantic History setting, use this at your own risk (becoming overly reliant on your mouse may be considered a felony by some).

[ -d "\1" ] && ([ "\5" = "\1" ] && echo nvim \1 || echo "cd \1 && ls") || ([ -z "\2" ] && echo nvim \1 || echo nvim +\2 \1)