I use Vim for most all the file editing I do on my Mac. I thought it’d be nice to be able to click on files in the Finder window and have them open up in my terminal for editing with Vim, so I wrote a bit of AppleScript to make it happen. This post shares how I set this up on my Mac so that you can do the same. The guide assumes that the terminal being used will be iTerm2.
1. Create an Application using Automator
You’ll be using the Automator application that comes installed on macOS to create a new application which will open your Finder files with Vim.
First, open the Automator application. Next, select File->New
or press ⌘-N
. Choose the
Application
type when prompted.
Next, search for Run AppleScript
in the action library located in the top left corner of the
Automator application. Double-click the Run AppleScript
item from the library.
Next, you’ll see that a Run AppleScript
input section for code will appear. Copy and paste the
entire contents of the AppleScript in the gist below ⬇️ into the input field.
You’ll then want to click the button with the small hammer 🔨 to make sure the AppleScript builds successfully.
Finally, select File->Save
or press ⌘-S
to save the new application. I chose to name the
new application FinderVim.app
.
2. Open files using FinderVim
Next, you’ll need to select what types of files you want to open using your new FinderVim
application. Start by opening the finder and navigating to a file you’d like to edit
using Vim by default.
I’ve selected a Go file to edit. Once you’ve found a file type you’d
like to always edit using Vim from the Finder, right-click
the file and select Get Info
.
In the Get Info
window, expand and select the drop-down menu in the Open with
section. This
is where you’ll select the default application to open the type of file you’ve chosen. Click
the “Other…” option from the avilable selections.
Navigate to your Applications
directory and select the new FinderVim
application. Then click
Add
in the bottom right corner.
Finally, click the Change All...
button below the Open With
selection box of the Get Info
window in order to change all similar documents to open using FinderVim
.
That’s it! Click the file and see that it opens it open for editing in Vim on iTerm2. You can set
FinderVim
to open additional file types using the same process.