I do a lot of programming and just starting to do a bit of web development. At the moment I am going to be making a few files and folders, because of this, is there a way to open a application in vim (in terminal not visual vim) when double/right clicking the file in finder?


  • 1
    Any reason why gVim won't do? If you aren't already using Terminal.app it just seems a tad odd...Though there's always the argument for not installing more cruft on a system, I guess… – forquare Oct 26 '15 at 18:02
  • @forquare It's because after I have edited that file I would like to run some command line tools, for example when going back to C++, I would like to run the g++ command then execute it after saving the file. I also don't want to download another application unless I have to. – iProgram Oct 26 '15 at 18:26

Create an Applet. The script below should get you started. Save that Applet to place of your preference (/Applications/). Select any text file and press ⌘+I(nfo) > Open With: > Other > Select you Applet, then click change all. Now whenever you open any text file that applet will run, which will open the file in vim.

on open theFiles
    tell application "Terminal"
        -- If there are no open windows, open one.
        if (count of windows) is less than 1 then
            do script ""
        end if
        set theTab to selected tab in first window
        set filePath to POSIX path of item 1 of theFiles
        do script "/usr/bin/vim " & quoted form of filePath in theTab
    end tell
end open

on run
    --  Handle the case where the script is launched without any dropped files
    open (choose file with multiple selections allowed)
end run
  • Good point! I could then put this in automator and set it as a service, which will then allow me to have a keyboard shortcut. Thanks – iProgram Oct 27 '15 at 10:56
  • dear @user14492: I would also be very interested in trying that solution. I have no experience in developping app for apple. How should I save that script? in a bash script in applications? which extension should i put (".sh"?). should I put it directly in applications or inside "openVim" folder and then "contents" folder and then the script? many thanks! – ecjb Mar 29 '20 at 9:41
  • 1
    @ecjb Open Script Editor app (/System/Applications/Utilities). Create new document. Copy paste the script. Save as "File format: Application" wherever you want. Now you can select a file and do open with and select this; you can do "Open always with" for a certain extension if you want. Or you can create a workflow quick action. Open Automator. Add and change script as here: imgur.com/JtIIwci Now you can also assign a shortcut in System Prefs - Keyboard as shown here or right click - quick action - .... imgur.com/kq6Ebt0 (choose something with Cmd in it.) – user14492 Mar 29 '20 at 18:17
  • many thanks @user14492. That worked! May I ask you a last question? What if I want to open it with "iTerm" (iTerm2) instead of Terminal. I tried to write tell application "iTerm" instead tell application "Terminal" but I could not save the script and got a message Syntax error: Expected end of line but found “script”. Is there a workaround? – ecjb Mar 29 '20 at 20:10
  • @ecjb iTerm behaves differently than Terminal so it needs to be different command. You can explore what command it has by Open Script Editor. File -> Open Dictionary -> Select iTerm. Here you can see all the commands specific to iTerm in 'iTerm Suit'. You are probably looking to modify the script so it's similar to gist.github.com/lordlycastle/068c374de87c3e1482cd03017f1103d6 Change the command to be the same as one in Terminal which opens vim with the path. Let me know if you get stuck. – user14492 Mar 31 '20 at 10:29

Code below tested with Mojave and supports a file path where both directory and filename components can optionally contain spaces. It sets the working directory to the location of the underlying file.

on run {input}
  set filename to quoted form of POSIX path of input
  -- support both directories and file names with spaces within
  set cmd to "clear;cd \"$(dirname " & filename & ")\";/usr/bin/vim " & filename & "; exit"
  tell application "System Events" to set termRunning to exists application process "Terminal"
  tell application "Terminal"
    if termRunning is true then
      set newWnd to do script with command cmd
      do script with command cmd in window 1
    end if
  end tell
end run

Use Automator to create a new "Run AppleScript" action with above contents and save script to an appropriate location (e.g. in /Applications/)


I created a github project with two versions of these apple scripts. One can open files and one can edit any selected text in place (e.g. in Mail, Safari etc.).


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .