Open Text File From Finder In Vim
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?
Thanks
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"
activate
-- 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
return
end open
on run
-- Handle the case where the script is launched without any dropped files
open (choose file with multiple selections allowed)
return
end run
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"
activate
if termRunning is true then
set newWnd to do script with command cmd
else
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/)