Skip to content

Instantly share code, notes, and snippets.

@chaadow
Last active December 17, 2021 16:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chaadow/5e8ed49c333008eec0c3ced07451ee48 to your computer and use it in GitHub Desktop.
Save chaadow/5e8ed49c333008eec0c3ced07451ee48 to your computer and use it in GitHub Desktop.
-- TerminalVim.app
-- This creates a shim Application that will enable you to open files from the Finder in vim using iTerm
-- To use this script:
-- 1. Open Automator and create a new Application
-- 2. Add the "Run Applescript" action
-- 3. Paste this script into the Run Applescript section
-- 4. Save the application as TerminalVim.app in your Applications folder
-- 5. In the Finder, right click on a file and select "Open With". In that window you can set TerminalVim as a default
on run {input, parameters}
if input is not in {} then
set myPath to POSIX path of input
set cmd to "vim " & quote & myPath & quote
end if
tell application "iTerm"
if it is running then
-- This is in the case where Iterm is open and no window is open.
if (count windows) is 0 then
create window with profile "Base16"
tell current window
tell current session
write text (cmd)
end tell
end tell
else
tell current window
-- We create a separate tab to the current window
create tab with profile "Base16"
tell current session
write text (cmd)
end tell
end tell
end if
else
-- At startup of iTerm...
tell application "iTerm"
-- We do not create a tab since we know it's at startup
tell current window
tell current session
write text (cmd)
end tell
end tell
end tell
end if
end tell
end run
@chaadow
Copy link
Author

chaadow commented Dec 31, 2016

For now I don't think it handles some cases with files with single/double quotes, or that sort of edge cases..

@steveinatorx
Copy link

nice script - here's a slight improvement that will focus the iterm2 window and uses the 'Default' profile:

-- TerminalVim.app
-- This creates a shim Application that will enable you to open files from the Finder in vim using iTerm

-- To use this script: 
-- 1. Open Automator and create a new Application
-- 2. Add the "Run Applescript" action
-- 3. Paste this script into the Run Applescript section
-- 4. Save the application as TerminalVim.app in your Applications folder
-- 5. In the Finder, right click on a file and select "Open With". In that window you can set TerminalVim as a default
on run {input, parameters}
	if input is not in {} then
		set myPath to POSIX path of input
		set cmd to "vim " & quote & myPath & quote
	end if
	
	
	tell application "iTerm"
		if it is running then
			-- This is in the case where Iterm is open and no window is open.
			if (count windows) is 0 then
				create window with profile "Default"
				tell current window
					tell current session
						write text (cmd)
					end tell
				end tell
				activate
			else
				tell current window
					-- We create a separate tab to the current window
					create tab with profile "Default"
					tell current session
						write text (cmd)
					end tell
				end tell
				activate
			end if
			
		else
			-- At startup of iTerm...
			tell application "iTerm"
				-- We do not create a tab since we know it's at startup	
				tell current window
					tell current session
						write text (cmd)
					end tell
				end tell
				activate
				
			end tell
			
		end if
		
	end tell
end run 

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