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
@rpapallas
Copy link
Author

Hi @Catyre

I found an easier way to do this using a shell script in Automator, saving this as an app and using it to open files:

image

Reference: https://adrianchongsf.com/blog/macos-open-vim-from-finder

Replace Alacritty with iTerm2, of course.

@Catyre
Copy link

Catyre commented Mar 7, 2023

Thanks for the quick reply!

I followed the link you sent and followed the instructions, but I'm not sure I'm doing it correctly as I'm not completely certain where the correct executable is for iTerm. Currently I'm doing:

Screen Shot 2023-03-07 at 1 21 57 PM

But then I get errors in iTerm about a server not launching correctly, or simply nothing happens. Any ideas?

@tucomel
Copy link

tucomel commented Jul 31, 2023

I created a alfred script below, based on the @rpapallas code

I trigger this code with a F2 key on finder (select a file that i want to open on neovim and press f2, only works on finder)
image

on alfred_script(qr) 

if frontmost of application "Finder" then
	tell application "Finder"
		set finderSelList to selection as alias list
	end tell
	
	if finderSelList ≠ {} then
		repeat with i in finderSelList
			set posixFile to POSIX path of i
			my runIniTerm("nvim " & posixFile)
		end repeat
		
		finderSelList as text
	end if
	
end if
end anfred_script

on is_running(app_name)
	tell application "System Events" to (name of processes) contains app_name
end is_running

on runIniTerm(q)
	if is_running("iTerm2") or is_running("iTerm") then
		run script "
            on run {q}
                tell application \":Applications:iTerm.app\"
                    activate
                    try
                        select first window
                        set onlywindow to true
                    on error
                        create window with default profile
                        select first window
                        set onlywindow to true
                    end try
                    tell the first window
                        if onlywindow is true 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
			delay 0.5

				try
					select first window
				on error
					create window with default profile
					select first window
				end try
				tell the first window
					activate
					tell current session to write text q
				end tell

		end tell
	end run
        " with parameters {q}
	end if
end runIniTerm

@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