Skip to content

Use neovim remote to create a Neovim.app on Mac OS X

Steven Noble edited this page Sep 29, 2017 · 2 revisions

The following code will create an app on Mac OS X.

Double-click the app to open Neovim in iTerm2.

Double-click a file of a filetype that is mapped to this app and it will open in Neovim in iTerm.

Drag a files onto the app and they will open in Neovim in iTerm.

Behind the scenes, neovim-remote ensures just one instance of Neovim is opened.

on run {input}
	
  # input will be empty if Neovim.app was double-clicked
  # input will contain the POSIX path to one or more files if:
  # => the files were dragged onto Neovim.app
  # => the files were double-clicked, opening Neovim.app		
	
  if input as text is "current application" then
    set the_files to "."
  else
    set the_files to POSIX path of input
  end if
	
  log("set the_files to " & the_files)
	
  # check if a Neovim-Remote server is running
  # https://github.com/mhinz/neovim-remote
	
  set nvr_serverlist to do shell script "/usr/local/bin/nvr --serverlist"
	
  tell application "iTerm"		
    activate
		
    if (count of windows) = 0 then
      set target to (create window with default profile)
    else
      set target to current window
    end if
		
    if (nvr_serverlist is equal to "") then
			
      # find or create an active iTerm2 window
      # open the file in Neovim in that window
			
      set command to "NVIM_LISTEN_ADDRESS=/tmp/nvimsocket nvim " & quote & the_files & quote
					
      tell target
        tell current session
          write text (command)
          do shell script ("printf 'told iTerm2 to open the file(s) in Neovim'")
        end tell
      end tell
			
    else
			
      # tell the Neovim-Remote server to open the file
      # in the existing Neovim instance
			
      set command to "/usr/local/bin/nvr --remote -O split " & quote & the_files & quote
      do shell script ("printf 'set command to " & command & "'")
			
      do shell script (command)
      do shell script ("printf 'told Neovim-Remote to open the file(s) in Neovim'")
			
    end if
  end tell
end run

Compile and save this as an app via Automator.

Clone this wiki locally