Double Click File in macOS to Open in Neovim

Boon aka Hwee-Boon Yar February 17, 2024
Source

I have been using Neovim in a dedicated Alacritty for exactly a year.

Took me a long time to come to it, but I recently sought to fix one issue — I can't double click on a file to open it in Neovim.

There are 3 components to getting this to work:

  • Neovim can start a server that listens on a named pipe
  • Automator can be used to make an app, which can then be configured as the file handler.
  • I use Alacritty as a dedicated app for using Neovim

There are a few steps to do it:

  1. Always start Neovim with server listening on a named pipe:

nvim --listen ~/nvim-server.pipe

  1. Write a script (I use ruby, and call it open-in-nvim) that opens a given filename:

#!/usr/bin/env ruby

arg = ARGV[0] if arg.nil? arg = STDIN.read.lines[0]&.chomp end

/opt/homebrew/bin/nvim --server ~/nvim-server.pipe --remote-send "<ESC>:tabe #{arg}<CR>" open -a Alacritty

  1. In Automator, create a new Application, add Run Shell Script, and make Pass input as to stdin and paste the absolute path to the open-in-nvim script. Save the application as open-in-nvim.app

  2. Click on a file with the file types you want (.md and .swift for me) and press Cmd+i (Get Info) and change Open with: to be open-in-nvim.app. Remember to click Change All….

Now double clicking files with that extension will open the file in Neovim and switch to it/Alacritty.

Discussion in the ATmosphere

Loading comments...