Create a gist now

Instantly share code, notes, and snippets.

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 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
Owner

mkitt commented Jan 19, 2012

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))

Er-c commented Jul 14, 2016

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

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