Skip to content

Instantly share code, notes, and snippets.

@rpapallas
Last active July 11, 2024 01:54
Show Gist options
  • Save rpapallas/31dfdf0626d078a357fccd46cdf6eafd to your computer and use it in GitHub Desktop.
Save rpapallas/31dfdf0626d078a357fccd46cdf6eafd to your computer and use it in GitHub Desktop.
Open file into NeoVim on macOS from Finder

Create a script that will allow macOS to open a source code file in iTerm2/Terminal and Vim/NeoVim

  • Open Automator and create a new application. You can name it "TerminalVim"
  • Works with any command basically, just change the following line set q to "nvim " & quote & myPath & quote to match what you want in the code below. For example: set q to "vim " & quote & myPath & quote to use Vim instead of NeoVim.
  • Paste the following code:
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

on run {input, parameters}
	set myPath to POSIX path of input
	set q to "nvim " & quote & myPath & quote
	
	if appIsRunning("iTerm") or appIsRunning("iTerm2") then
		run script "
			on run {q}
				tell application \":Applications:iTerm.app\"
					activate
					try
						select first window
						set onlywindow to false
					on error
						create window with default profile
						select first window
						set onlywindow to true
					end try
					tell the first window
						if onlywindow is false then
							create tab with default profile
						end if
						tell current session to write text q
					end tell
				end tell
			end run
		" with parameters {q}
	else
		run script "
			on run {q}
				tell application \":Applications:iTerm.app\"
					activate
					try
						select first window
					on error
						create window with default profile
						select first window
					end try
					tell the first window
						tell current session to write text q
					end tell
				end tell
			end run
		" with parameters {q}
	end if

end run
  • Save this App under /Applications.
  • Find a file, right-click, "Get Info" and under "Open With" click "Other..." change its default application to be our new app. If you can't see the app in the list provided, then change next to "Enable" from "Recommended Applications" to "All Applications"
  • Optionally click "Change All..." to make it default for every file.

For Terminal instead of iTerm2

Follow previous steps but with the following script:

on run {input}
	set the_path to POSIX path of input
	set cmd to "vim " & quoted form of the_path
	tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
	tell application "Terminal"
		activate
		if terminalIsRunning then
			tell application "System Events" to keystroke "t" using {command down}
  			delay 0.2
			do script with command cmd in window 1
		else
			do script with command cmd in window 1
		end if
	end tell
end run
@cppxor2arr
Copy link

If I try to open my editor of choice (helix) without any files (e.g. opening it from spotlight or applications) I get this error:

Can’t get POSIX path of {}.

Sometimes I want to do this because I want to use my editor to quickly take notes.

I've never used AppleScript before, but after some searching, this is how I fixed it for Terminal. The same can be done for iTerm2.

--- before
+++ after
@@ -1,6 +1,10 @@
 on run {input}
-	set the_path to POSIX path of input
-	set cmd to "hx " & quoted form of the_path
+	if input = {} then
+		set cmd to "hx"
+	else
+		set the_path to POSIX path of input
+		set cmd to "hx " & quoted form of the_path
+	end if
 	tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
 	tell application "Terminal"
 		activate

@alexya
Copy link

alexya commented May 13, 2024

Thanks for sharing, this is mine and it works fine.
It supports 1 file selection and more files selection. The Neovim will open all the files in different Tabs.
If you want to close the alacritty terminal window after quit the Neovim, please remove the last ; $SHELL

files=""
for f in "$@"
do
  files+=" '$f'"
done

/Applications/Alacritty.app/Contents/MacOS/alacritty -e $SHELL -c "/opt/homebrew/bin/nvim -p $files; $SHELL"

The quick action setting screenshot
image

cc: @Catyre

@rpapallas
Copy link
Author

rpapallas commented May 13, 2024

Since then, I found an easier way too (one-liner):

Using Kitty and Neovim:

/opt/homebrew/bin/kitty -e $SHELL -c "/opt/homebrew/bin/nvim '$@'" >/dev/null 2>&1 &

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment