Skip to content

Instantly share code, notes, and snippets.

@mkitt
Created February 13, 2011 03:24
Show Gist options
  • Save mkitt/824401 to your computer and use it in GitHub Desktop.
Save mkitt/824401 to your computer and use it in GitHub Desktop.
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 Launcher.app" 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 Launcher.app"
  • 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)
else
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"
activate
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
else
do script with command cwd in window 1
do script with command cmd in window 1
end if
end tell
end launchVim
@mkitt
Copy link
Author

mkitt commented Jan 19, 2012 via email

@pjdoland
Copy link

You need to quote the_path or you'll run into problems cd-ing into directories with spaces their names:

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))
        set the_quoted_path to quoted form of the_path
    end tell

    set cwd to "cd " & the_quoted_path
    set cmd to "vim " & the_file
    my launchVim(cwd, cmd)
end openFile

I tried the following, but it didn't work:

set the_path to quoted form of POSIX path of (container of (item 1 of input))

@BTCCoffeeTable
Copy link

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

@jhkuperus
Copy link

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

	tell application "iTerm"
		activate
		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