Use Terminal's vi as default text editor

Mar 13, '08 07:30:00AM

Contributed by: sominsky

I've always been a vi guy, but OS X GUI-based vi versions leave a lot to be desired. So for years I've put up with TextEdit as the default application when I double click a text file in Finder or an attachment in Mail. Until yesterday, that is. Using this old macosxhints forum thread as the base, I updated and modified the script to behave properly.

Double-clicking a text file now opens it in vi in a new tab in Terminal, opening Terminal if it's not already running (which is rare, as I usually have four or five tabs open all the time). Here's the script:

on open inputfile
  ignoring application responses
    tell application "Terminal"
      activate
      tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
      do script "vi " & quoted form of POSIX path of inputfile in selected tab of the front window
    end tell
  end ignoring
end open
Copy it into Script Editor, and save it as an Application called VI. To use your new application to open any text file, control-click on a text file and choose Get Info from the pop-up menu. Under Open With, at the bottom of the pull-down menu, is Other. Select that, and a window will open where you can choose your saved application (check the Always Open With box, too). I also checked the Change All box back in the Get Info window.

Unfortunately, the one thing I haven't figured out is how to get it to open without an input file, so to start a new text file I have to go to the Terminal and type vi filename. Please let me know if this script can be improved to handle that case, or in any other ways.

[robg adds: While I could make this script work for files on a one-at-a-time basis, I could not get the Change All solution to work -- regardless of how I specified the app, or set the file's extension, Change All had no effect. Only the selected file was modified to open with vi; a commenter on the queue review site had the same experience. If you know how to resolve that issue, please comment...]

Comments (16)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080305012955463