Doubleclick to open a file in VIM from OSX

Doubleclick to open a file in VIM from OSX

I use VIM for just about everything from note taking to coding to keeping track of my recipes.

Of course I've dabbled with other text editors, but nothing I've liked so far.

But I found myself more and more dealing with a txt file inside Mac's Finder app and then dropping to a terminal and spending time switching directories over to that file to edit it.

I wanted to just double click on the txt file and have it automatically open in VIM inside my iTerm. This functionality isn't built into OSX but with just a bit of Applescript we can make it work for us.

Example:

Example of double click to VIM

Here's what we're going to do...

Step #1 - Open your Automator application on your Mac

Find the "Automator" app in your Applications folder and launch it.

Step #2 - Build an Applescript application to launch VIM

Choose the "Application" type and click "Choose"...

Double click on "Run Applescript"...

Copy and paste the following Applescript code...

on run {input, parameters}

set filename to POSIX path of input

    set cmd to "clear && 'vim' '" & filename & "' && exit"

    tell application "iTerm"
        set newWindow to (create window with default profile)
            tell current session of newWindow
                write text cmd
            end tell
    end tell

end run

Into the applescript window...

Save the Automator script as a new application in your Applications folder...

Step #3 - Associate your new app with files you want to open with VIM via doubleclick

Now find a file of whatever type you want to associate with opening in VIM. I'm doing .txt files for this example.

Right click on the file and click "Get Info"...

Now set your new .app file to be used whenever that file type is opened...

Step #4 - Done! Try it out

Now when you doubleclick on one of your .txt files, it will be opened in VIM in your iTerm terminal...

Example of double click to VIM
Pause reading for 5 seconds...
Join My Mailing List!
Note: I will never share your email with anyone else.
Awesome! Thanks so much.
Ok, lets continue.

Hopefully this has been helpful for you. If you've tried out the code here, drop me a tweet at twitter.com/mattccrampton and let me know.