Skip to content

Instantly share code, notes, and snippets.

@Huluk
Last active December 8, 2023 22:34
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Huluk/5117702 to your computer and use it in GitHub Desktop.
Save Huluk/5117702 to your computer and use it in GitHub Desktop.
Open Files with Terminal Vim by default [mac, iterm]
If you want your terminal vim to open files you double click, follow the following steps (MacOS only):
1. Open Automator
2. Select Application
3. Copy the attached file
4. Save and set as default for opening files
Multiple files are opened in vim tabs.
If there is already a vim instance running, files are opened in it.
Bugs / Problems:
- does not open files when vim instance is in insert mode
- vim instances running e.g. in tmux are not recognized
Also thank Daniel Beck, who posted the script I adjusted
http://superuser.com/questions/283418/how-can-i-make-terminal-vim-my-default-editor-application-in-mac-os-x
on run {input, parameters}
if (count of input) > 0 then
tell application "System Events"
set runs to false
try
set p to application process "iTerm"
set runs to true
end try
end tell
tell application "iTerm"
activate
if (count of terminals) = 0 then
make new terminal
end if
set numItems to the count of items of input
set numTerms to the count of terminals
set launchPaths to ""
repeat with t from 0 to (numTerms - 1)
tell item (numTerms - t) of terminals
if (count of sessions) = 0 then
launch session "Default"
end if
repeat with s from 1 to count of sessions
set currentSession to item s of sessions
if name of currentSession contains "vim" then
tell currentSession
write text (":silent! tablast")
repeat with x from 1 to numItems
set filePath to quoted form of POSIX path of item x of input
write text (":execute 'tabedit '.fnameescape(" & filePath & ")")
end repeat
return
end tell
end if
end repeat
end tell
end repeat
tell current terminal
tell (launch session "Default")
repeat with x from 1 to numItems
set filePath to quoted form of POSIX path of item x of input
set launchPaths to launchPaths & " " & filePath
end repeat
write text ("mvim -p " & launchPaths)
end tell
end tell
end tell
end if
end run
@dctucker
Copy link

Big ups on this one, works excellently!

@luketurcotte
Copy link

Thanks for the script, it works great. I made a couple of small changes as I prefer to use buffers instead of tabs in vi. If you're this far into the weeds, I'm sure you could figure out how to do this yourself, but just in case here's what I changed.

on run {input, parameters}
if (count of input) > 0 then
tell application "System Events"
set runs to false
try
set p to application process "iTerm"
set runs to true
end try
end tell
tell application "iTerm"
activate
if (count of terminals) = 0 then
make new terminal
end if
set numItems to the count of items of input
set numTerms to the count of terminals
set launchPaths to ""
repeat with t from 0 to (numTerms - 1)
tell item (numTerms - t) of terminals
if (count of sessions) = 0 then
launch session "Default"
end if
repeat with s from 1 to count of sessions
set currentSession to item s of sessions
if name of currentSession contains "vim" then
tell currentSession
write text (":silent! tablast")
repeat with x from 1 to numItems
set filePath to quoted form of POSIX path of item x of input
write text (":execute 'e'.fnameescape(" & filePath & ")")
end repeat
return
end tell
end if
end repeat
end tell
end repeat
tell current terminal
tell (launch session "Default")
repeat with x from 1 to numItems
set filePath to quoted form of POSIX path of item x of input
set launchPaths to launchPaths & " " & filePath
end repeat
write text ("vim" & launchPaths)
end tell
end tell
end tell
end if
end run

@owensuls
Copy link

I get "The action “Run AppleScript” encountered an error." when attempting to use this. I'm using macOS Sierra v10.12. Any suggestions? Thanks.

@oliverseal
Copy link

for iterm2 on latest os x things change a little. Here's what I use. It's not 100% the same, but it suits my needs.

on run {input, parameters}
	if (count of input) > 0 then
		tell application "System Events"
			set runs to false
			try
				set p to application process "iTerm"
				set runs to true
			end try
		end tell
		tell application "iTerm"
			activate
			set numItems to the count of items of input
			set launchPaths to ""
			repeat with x from 1 to numItems
				set filePath to quoted form of POSIX path of item x of input
				set launchPaths to launchPaths & " " & filePath
			end repeat
			tell current window
				create tab with default profile command "vim " & launchPaths
			end tell
		end tell
	end if
end run

@nevertoday
Copy link

can this be shell command make all feature same???

@mariano-daniel
Copy link

mariano-daniel commented May 16, 2023

for iterm2 on latest os x things change a little. Here's what I use. It's not 100% the same, but it suits my needs.

But this is only for iTerm, what about the regular zsh terminal?

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