Mac OS X: how to open vim in terminal when double click on a file

19

10

I have defined my own custom vim file type with highlighting etc. I would like to open it using the terminal based vim when I double click on it. I am using mac os x. Any pointers on how to start on this?

sixtyfootersdude

Posted 2010-05-09T19:51:50.170

Reputation: 6 399

Answers

17

Create an Automator Application to run the following applescript:

on run {input}
   set the_path to POSIX path of input
   set cmd to "vim " & quoted form of the_path
   tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
   tell application "Terminal"
      activate
      if terminalIsRunning is true then
         do script with command cmd
      else
         do script with command cmd in window 1
      end if
   end tell
end run

Save the automator application. (eg. name it Vim Launcher)

Right click (or control-click) on your custom vim-type file (eg. use .vim as the extension) and under Open With… choose the bottom option Other… and find your Automator Application (eg. Vim Launcher), double-click it.

Boom.

ghoppe

Posted 2010-05-09T19:51:50.170

Reputation: 6 124

4To create in Automator, you can click New Document, select the Application template. In the Actions->Library tab, click Utilities, then Run AppleScript. – cevaris – 2014-08-24T15:51:46.470

1Doesn't work in Yosemite. – keyvan – 2015-01-30T08:10:33.980

I got things working in Yosemite, though I use iTerm: http://thepugautomatic.com/2015/02/open-in-iterm-vim-from-finder/

– Henrik N – 2015-02-01T10:39:07.030

works for me in Yosemite. there is one strange edge case, though-- – Ilias Karim – 2015-05-22T23:58:41.187

if there is already a terminal open and I open a file, it opens a new terminal window (as expected). but if I open a new tab in that terminal window, that tab tries to open the same file. – Ilias Karim – 2015-05-22T23:59:14.577

if there is NOT initially a terminal window open, when I create a new terminal tab after opening the file in vim, it does not try to open the file again in the new tab, which is nice – Ilias Karim – 2015-05-22T23:59:50.000

1This does not seem to work with iTerm on 10.12.5 – oarfish – 2017-06-20T10:02:14.567

This works well for me - I had to append the third line with & "&& exit;" in order to get the window to close – Max Chuquimia – 2019-10-23T03:12:05.727

1

I just wanted to add a comment to the accepted answer with the code changes needed to make it work in Yosemite, but since I don't have sufficient reputation couldn't add a comment, and hence attempting to reply via an answer.

The "Open File in Terminal from Finder" script was working fine in Mavericks, but it stopped working after the upgrade to Yosemite. In Yosemite, the code in the accepted answer would work only the first time - meaning when I double click the first file in the Finder, it opens fine, but when I click the subsequent files, they would just open blank new terminal windows (vim won't open) with command prompt.

After going through multiple sites, cobbled together a version that works just fine. I am sure there is a better way to do it, but I have no experience with Applescript and so will leave it to others to suggest any improvements.

on run {input}
    set the_path to POSIX path of input
    -- set cmd to "vim " & quoted form of the_path
    -- we can do a change directory to make NerdTree happy
    set cmd to "clear;cd `dirname " & the_path & "`;vim " & quoted form of the_path & "; exit"

    tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
    tell application "Terminal"
        if terminalIsRunning is true then
            -- CHANGED code starts --
            set newWnd to do script with command cmd
            do script with command cmd in newWnd
            -- CHANGED code ends --
        else
            do script with command cmd in window 1
        end if
        activate
    end tell
end run

protoiyer

Posted 2010-05-09T19:51:50.170

Reputation: 11

In a 2017 Macbook Pro with Touchbar, I find that the [accepted answer] (https://superuser.com/a/139949/44785) works fine. But I want to still add the "change directory" (cd) line to ensure the pwd is where the file is. This helps NerdTree to display only the files from the opened file's folder.

– protoiyer – 2017-12-02T21:49:16.767

1

From the five or so minutes I spent playing with it to see if it would I couldn't find a built -in option to do so.

However, you can probably write a simple Applescript that will take the files absolute path and then run vim {path} in a bash shell.

Josh K

Posted 2010-05-09T19:51:50.170

Reputation: 11 754

1

set the_path to POSIX path of input
   set cmd to "vim " & quoted form of the_path & "; exit"
   tell application "System Events" to set terminalIsRunning to exists application process "Terminal"
   tell application "Terminal"
      if terminalIsRunning is true then
         do script with command cmd
      else
         do script with command cmd in window 1
      end if
      activate
   end tell
end run

I use this AppleScript instead. It activates Terminal.app after (not before!) execution to stop things from acting odd when using Spaces. It also closes the window after Vim exits. Just set Terminal.app to close after clean exits.

ggustafsson

Posted 2010-05-09T19:51:50.170

Reputation: 1 698