Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use Terminal's vi as default text editor System
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: 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...]
    •    
  • Currently 2.30 / 5
  You rated: 2 / 5 (10 votes cast)
 
[39,725 views]  

Use Terminal's vi as default text editor | 16 comments | Create New Account
Click here to return to the 'Use Terminal's vi as default text editor' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use Terminal's vi as default text editor
Authored by: acrollet on Mar 13, '08 08:18:02AM
I actually find that the newest snapshot of MacVim is very nice indeed. The cocoa/OS X integration is very well done, and comes with an installer for an Input driver that will give you a single keystroke to edit any cocoa text field in MacVim. (safari, mail, etc. all work) I would recommend that anyone who likes using vim go and download the snapshot, and donate to the developer if you like using it - his ibook recently died...

[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: Seth Milliken on Mar 13, '08 05:15:18PM

+1 MacVim



[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: lsloan on Dec 23, '08 02:12:07PM

+1 MacVim && +1 iTerm

I prefer iTerm over Terminal, but I think it's wise to write this hint using Terminal, since everybody should have it. An iTerm version should be added, too, though.

---
Lance E Sloan



[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: robleach on Mar 13, '08 08:29:59AM

Well, I don't have 10.5, but this works for me in 10.4 to open a new terminal window and run vi (I left the command-t in there even though it opens up the fonts window).

[code]tell application "Terminal"
activate
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
do script "/usr/bin/vi"
end tell[/code]

Rob



[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: lsequeir on Mar 13, '08 08:35:45AM
Regarding your problem that "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. "

It is easy to have your VI application also open by double clicking. You just have to put in there a similar AppleScript piece of code with "on run", as well as "on open inputfile" and do the appropriate adjustments. You would end up with something the following (which seems to be working for me).

on run
ignoring application responses
tell application "Terminal"
activate
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
do script "vi " in selected tab of the front window
end tell
end ignoring
end run

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

---
LuĂ­s

[ Reply to This | # ]

Use Terminal's vi as default text editor
Authored by: ob1cannoli on Mar 13, '08 11:51:24AM
or one better, add an input for a file name/path: on run display dialog "Enter file name:" default answer "filename" copy the result as list to {theFilename, theButton} 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 fileName in selected tab of the front window end tell end ignoring end run 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

[ Reply to This | # ]
Re: Use Terminal's vi as default text editor
Authored by: Uncle Asad on Mar 13, '08 07:25:50PM

When I'm writing an applet that duplicates a lot of code in its on run and on open handlers, I find it useful to prevent duplication and ease maintenance by making those handlers "stubs" that collect some information and then run the shared code in another method.

For example,

on run
	set inputfile to ""
	my launchVI(inputfile)
end run

on open inputfile
	my launchVI(inputfile)
end open

on launchVI(inputfile)
	ignoring application responses
		tell application "Terminal"
			activate
			tell application "System Events" to tell process ¬
				"Terminal" to keystroke "t" using command down
			if inputfile ≠ "" then
				do script "vi " & quoted form of POSIX path of inputfile ¬
					in selected tab of the front window
			else
				do script "vi " in selected tab of the front window
			end if
		end tell
	end ignoring
end launchVI

There's probably a way to eliminate the if/else, but it's late and I'm not thinking very hard right now ;)

[ Reply to This | # ]

Fixing "Change All"
Authored by: Uncle Asad on Mar 13, '08 07:06:44PM
[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...]

This is because the applet doesn't have a unique bundle identifier or bundle signature ("creator code") by default; when Script Editor saves an Application Bundle (or a PPC-only Application), it uses the dplt (or aplt, for applets that don't handle on open) bundle signature (and does not specify a bundle identifier at all). dplt and aplt are shared with every other applet/droplet on your Mac—not useful for trying to set a default binding to a specific app….

First, make sure you're saving the script as an "Application Bundle" in Script Editor, not an "Application", because you can't fix this problem with an old-school "Application" (and because Applications are PPC-only; who wants to start Rosetta to launch vi on an Intel Mac?!).

The easiest way to fix this is to change the bundle signature in your applet's Info.plist and PkgInfo files to something unique; I used dpVI when verifying this. "Show Package Contents" on your app and open both of those files in a text editor (vi, even!) and find dplt in each file, replace it with dpVI, and save. Finally, move the VI.app out of the Applications folder to the Desktop, and then back in to Applications (to get LaunchServices to pick up your Info.plist changes). The drawback to this change is that every time you open the app in Script Editor to make further changes, you have to dismiss an alert about the app not being scriptable.

You could also (or in addition) add a CFBundleIdentifier key to the Info.plist, something like:

<key>CFBundleIdentifier</key>
<string>com.macosxhints.vi-app</string>
Simply add that with your favorite text editor (or Property List Editor) among the other CFBundle* keys in your Info.plist, save, and repeat the "move the app from the Applications folder" dance to get LaunchServices on board.

If it still doesn't work, be sure to launch the app once manually (by double-clicking) to clear the "you've never run this app before" roadblock, but that roadblock shouldn't be hit by apps built on your own Mac.

[ Reply to This | # ]
Fixing AppleScript apps
Authored by: hayne on Mar 13, '08 08:27:49PM
I wrote a Bash script a while back to fix this kind of issue in AppleScript apps - you can get my script here:
http://hayne.net/MacDev/Bash/fixAppleScriptApp


[ Reply to This | # ]
Fixing "Change All"
Authored by: robg on Mar 17, '08 06:18:03AM

Thanks for explaining the reason for the problem!

-rob.



[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: ctuft on Mar 18, '08 08:42:22PM
I tend to use iTerm instead of Terminal, so I modified the script a bit so it works in iTerm. This will also deal with multiple files (opens a new tab for each), and it will also leave you in the same directory where the opened text file resides:
on run
	set this_inputfile to ""
	set this_inputfiledir to "~/Desktop"
	my launchVI(this_inputfile, this_inputfiledir)
end run

on open inputfiles
	repeat with this_inputfile in inputfiles
		tell application "Finder"
			set this_inputfiledir to quoted form of POSIX path of (container of this_inputfile as string)
		end tell
		my launchVI(this_inputfile, this_inputfiledir)
	end repeat
end open

on launchVI(this_inputfile, this_inputfiledir)
	tell application "iTerm"
		activate
		tell the first terminal
			set mySession to (make new session at the end of sessions)
			tell mySession
				set short_name to system attribute "USER"
				exec command "login -fp " & short_name
				if this_inputfile ≠ "" then
					write text "cd " & this_inputfiledir
					write text "vi " & quoted form of POSIX path of this_inputfile & return
				else
					write text "cd " & this_inputfiledir
					write text "vi"
				end if
			end tell
		end tell
	end tell
end launchVI
-Chris

[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: sominsky on Mar 20, '08 10:42:35PM

Ooh, this is nice. I'm thrilled that my first hint submission has gone so well. Maybe I'll do more :)

I'm saving your script in place of my own, by the way. This does *exactly* what I want.



[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: Paul Burney on May 21, '09 06:44:36AM

Awesome tip, Chris! One of the best on this site!



[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: tub78 on May 14, '08 01:46:39PM

I found that it was necessary to add a delay of 1 second before the command: do script "vi" ... . Otherwise, the "vi" command would sometimes run in the existing tab, and not the newly created tab. For example,

	...
	tell application "System Events" to tell process ¬
		"Terminal" to keystroke "t" using command down
	delay 1
	do script "vi " & quoted form of POSIX path of inputfile ¬
		in selected tab of the front window
	...

- S.

[ Reply to This | # ]

Changes I had to make to the script.
Authored by: roystonlodge on Jan 23, '09 08:11:09PM
(FYI: I'm running 10.4.)

When I tried to save the script I got a "expected end of line" error when it hit the word "tab". So I removed the "in selected tab of front window" bit.

I also removed the bit about keystoke "t" because it caused the font dialog box to open up.

I also changed it from Vi to Nano, just cuz that's the editor I like to use.


on run
	ignoring application responses
		tell application "Terminal"
			activate
			do script "nano "
		end tell
	end ignoring
end run

on open inputfile
	ignoring application responses
		tell application "Terminal"
			activate
			do script "nano " & quoted form of POSIX path of inputfile
		end tell
	end ignoring
end open


[ Reply to This | # ]
Use Terminal's vi as default text editor
Authored by: cjmarone on Mar 13, '11 07:03:26PM

Any idea if I can us vi as the default editor when I 'reply' using mail? I hate the mouse-click-type approach in mail (textedit, etc.) and would love to have vi instead.



[ Reply to This | # ]