How to open a file in a Terminal editor from the OS X Finder

While you can manually enter the commands to edit files in Terminal editors, in OS X you can set up a service to open files in a desired terminal editor from the Finder's contextual menu.

If you regularly use the Terminal to modify or manage files in the filesystem, it may be helpful to be able to select a file in the Finder and open it directly in a specified Terminal editor such as vi, pico/nano, or emacs. While you can always launch a Terminal window and enter the command to launch the file in the desired editor, there is a way to do this directly in the Finder.

Luckily, like most Terminal commands, common text editors use the syntax "command targetfile" to launch and target a file. Using this, you can implement a system service that will grab a selected file and pass it to the Terminal to run in the desired editor program. Here is how to do this:

  1. Open Apple's Automator program.

  2. Select "Service" when you get the option to choose the type of file to create.

  3. Make sure the input variables (at the top of the workflow window) are set to "Files or folders" and set the application to be the Finder.

  4. Locate and add the Run AppleScript action, and drag it to the workflow.

  5. Replace the line "(* Your script goes here *)" with the following two lines:

    set thePath to POSIX path of input
    tell application "Terminal" to do script "pico \"" & thePath & "\""

  6. The current command will use the pico editor, but if you want to use vi, emacs, or another editor then change the text "pico" to the name of the editor of your choice. You can optionally also remove the "return input" line.

  7. Save the service, and give it a name (such as "Open with pico").

Automator workflow
The workflow in Automator should look like this when you are done (click for larger view).

Note that the Services option is only available for Snow Leopard users. If you are using OS X 10.5 Leopard, then you can just create a new work flow in Automator and choose "Save as plugin" from the File menu to do the same thing. Save it as a plug-in for the Finder, and it will then be available in the More > Automator submenus of the contextual menu when you right-click a file.

With the service saved, you can now right-click on any file in the Finder and select the service in the Services contextual submenu, and the file will open in the Terminal. Be sure the file can be read by the editor before you use the service; otherwise you may see a lot of garbled text appear in the Terminal window.

Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Featured Video
This content is rated TV-MA, and is for viewers 18 years or older. Are you of age?
Sorry, you are not old enough to view this content.

Man flies 54-propeller superdrone, almost flips it, Ep. 217

This week on Crave, we walk you through a futuristic new automated restaurant in San Francisco, get navigation directions from the sultry voice of Stephen Colbert on Waze, and fly a drone with 54 propellers that can carry a full-grown man. It's the Crave show!

by Stephen Beacham