Opening files in NeoVim within iTerm2 from the macOS Finder
I wanted a way to automatically open certain file types using the command-line version of NeoVim (nvim
) on macOS, and to be able to open NeoVim within a terminal window using a Dock icon. I use iTerm2 as my terminal application.
Using Automator, it’s possible to create an application with this functionality using an AppleScript. A similar process could be used to create an application launcher for other command-line programs, also allowing one or more file paths to be used as arguments.
- Install NeoVim and iTerm2 if you haven’t already.
- Open Automator and create a new Application.
- Add a
Run AppleScript
action and paste in the code below. If iTerm is already running it will create a new window, and it will optionally open one or more files in different NeoVim tabs. If you run the action within Automator it may give you an error but it should work when you create an Application from it.on run {input, parameters} # seem to need the full path at least in some cases # -p opens files in separate tabs set nvimCommand to "/usr/local/bin/nvim -p " set filepaths to "" if input is not {} then repeat with currentFile in input set filepaths to filepaths & quoted form of POSIX path of currentFile & " " end repeat end if if application "iTerm" is running then tell application "iTerm" create window with default profile command nvimCommand & filepaths end tell else tell application "iTerm" tell current session of current window write text nvimCommand & filepaths end tell end tell end if end run
- Save the Automator document as an Application in
/Applications
. - If you wish, you can change the application’s icon.
- Download
neovim-mark.png
from the NeoVim website repository. - Open this image in Preview and copy it to the clipboard.
- Right-click your NeoVim application file and click
Get Info
. - Click on the icon at the top of the info window and paste the image you copied earlier.
- Download
- Open
/Applications
and drag your new application to the Dock if you wish. - Right-click a file of the type you want to automatically open in NeoVim (e.g. a Markdown (
.md
) file and clickGet Info
. SelectOther…
in theOpen with:
menu, navigate to your new application and clickAdd
. Then clickChange All…
and all files with the same extension should now open in NeoVim.
If you have any questions or suggestions, why not get in touch with me on Twitter?