Skip to content

Instantly share code, notes, and snippets.

Created February 13, 2011 03:24
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
AppleScript for use with Automator which allows opening files within Terminal Vim

What It Does

This AppleScript used in conjunction with Automator will allow you to set preferences for opening files via mouse click in the Terminal version of Vim. It will also allow you to fire up the Terminal version of Vim through Spotlight within the home directory.

Installation Notes

  • Fire up Automator
  • Choose "Application" from the workflow templates
  • Under the "Actions" panel select "Utilities"
  • To the right of "Utilities" drag an instance of "Run AppleScript" to the editor window
  • Paste in the .scpt file listed in this gist
  • Save As... "Vim" to the "Applications" folder with a file format of "Application"

Then you'll need to set your file types from Finder to open via the launcher like any other Application:

  • Select a file within the Finder
  • Go "File > Get Info" or use the command-I key shortcut
  • In the dialog window, select the list under "Open with:"
  • Select "Vim"
  • Then click the button "Change All..." for all file types like the one you are changing to open with Terminal Vim
  • Repeat this for any other file types you want to open with Terminal Vim

You'll also be able to fire up Terminal Vim via spotlight by typing "Vim Launcher". This will open Terminal Vim from your home directory.

on run {input}
if length of input is equal to 1 then
my openFile(input)
my justOpen()
end if
end run
on openFile(input)
set the_file to quoted form of POSIX path of input
tell application "System Events"
set the_path to POSIX path of (container of (item 1 of input))
end tell
set cwd to "cd " & the_path
set cmd to "vim " & the_file
my launchVim(cwd, cmd)
end openFile
on justOpen()
set cwd to "cd"
set cmd to "vim"
my launchVim(cwd, cmd)
end justOpen
on launchVim(cwd, cmd)
tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
tell application "Terminal"
if terminalIsRunning is true then
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
do script with command cwd in selected tab of the front window
do script with command cmd in selected tab of the front window
do script with command cwd in window 1
do script with command cmd in window 1
end if
end tell
end launchVim
Copy link

Any idea how to make this work for iTerm????

Copy link

For iTerm, change the contents of the launchVim function to this:

	tell application "iTerm"
		tell the current window
			create tab with default profile
			tell the current session
				write text cwd
				write text cmd
			end tell
		end tell
	end tell

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