If you regularly use the OS X Terminal for various operations on your Mac, then you might benefit from some of the unique ways Apple has implemented the Terminal and some of its related services. Even though the Terminal is a more abstract, text-based environment for managing your Mac, there are several features it supports that somewhat blends it with the OS X GUI, allowing you at times to more intuitively manage your Mac.
Drag and Drop
The first of these is Drag and Drop support, where objects in the OS X GUI can be dropped into the Terminal to append the content they represent at the Terminal’s cursor. These items are often filesystem objects (files and folders), where dropping them onto a Terminal window will input the full path to the selected objects, separated by spaces. However, other objects can be used as well, including selected text and text clippings that you can drag from one item to another.
For the most part, the only items you can drag and drop into the Terminal will be those that have some sort of text representation, such as the path for a filesystem object, or the URL for an image you drag from a Web browser. If the dragged content represents an image or other content the Terminal cannot handle, then nothing will enter.
Note that in addition to objects in the Finder, you can also drag them from Spotlight search results, and from the window titles of open documents, as these all represent the file’s path on disk when dropped on a Terminal window.
Unfortunately there is no way to drag items managed in the Terminal to the Finder, but selected text in the Terminal can be dragged to the Finder as a text clipping, or directly to applications with text entry fields.
One of the features in OS X is its Services, where the content being managed in one program can be directly piped into another program without having to first save it as a document and then open it in the second program. There are two such services available for the Terminal in OS X:
New Terminal at Folder
This will allow you to select an object that represents a filesystem path, and then open a Terminal window at that location on the file system. This can be done in the Finder by right-clicking any file or folder, and then choosing “New Terminal at Folder” in the Services contextual submenu. You can also do this by selecting a typed file path in a program like TextEdit, and then similarly right-click it and choose the option for a new Terminal window at the specified location. Try it here by selecting the following path for the system library, and then right-clicking it to open a Terminal at that location:
Open Man page
Another service is the ability to look up a command in the Terminal’s manual pages, by selecting it and then choosing the option to open its man page in the Terminal. Try this by right-clicking the following command.
Even though this is perhaps outdated given the plethora of information on the Internet, it can be useful if you are following some written instructions and are offline.
Note that for these services to be available, you may have to enable them in the Shortcuts tab of the Keyboard system preferences. In this section, select “Services” on the right, and then in the “Files and Folders” section on the left, check the options for New Terminal at Folder. Then scroll to the “Text” section and check the box to “Open man page in Terminal.”
The “open” command
Apple includes a command called “open” which at its default will open a specified filesystem object in its default application. For instance, if you have a text document in your home folder called “test.txt” and you double-click it to launch TextEdit, you can also do this by running the following command in the Terminal:
Perhaps one of the more useful features of the open command is its ability to open the current folder or reveal a specific file in the Finder, which can be especially useful if you are navigating the filesystem in the Terminal and would like to handle a specific file or folder in the Finder.
To reveal the present folder in the Finder, simply run the following command:
To reveal a specified file, simply use the “-R” flag in the following manner, where “PATH” is a full or partial path to a given file of interest:
open -R PATH
As an interesting side note, for file paths that are already printed in the Terminal (such as if you had already entered the “pwd” command), you can select the file path and then right-click the selection and choose the option to open it in the Finder, which is also in the Services menu. This is a somewhat redundant, but available alternative to using the “open .” option.
The “pbcopy” and “pbpaste” commands
Another set of commands that Apple includes is its pbcopy and pbpaste commands, which interface the Terminal with the system’s clipboard, allowing you to copy content to and from it. To use these commands, you will need to be familiar with piping in the Terminal, where you use a vertical line character between commands to send the output of the first command directly to the second command.
For example, if you run the command “pwd,” to show the present working directory (go ahead, type it in a Terminal window and press Return), the Terminal will simply output a line that shows the full path to your current directory.
If you would like to copy this to the clipboard, you can pipe this output directly into the “pbcopy” command in the following way:
pwd | pbcopy
Once run, you can go to the Finder and view the clipboard (in the Edit menu) to see the results of the “pwd” command now present there.
To retrieve any content from the clipboard, simply run the pbpaste command directly, or optionally follow this by piping it to an additional command, to handle the clipboard contents directly in that second command:
pbpaste | SECOND_COMMAND
Note that as with other Terminal commands, you will only be able to handle text-based content using the pbcopy and pbpaste commands. If any image content is in the clipboard, then pasting it will result in no output in the Terminal when the “pbpaste” command is run.
No doubt you turned on the Terminal services you mention a long time ago, but they are not turned on by default. Given that Services are only slightly less obscure than the Terminal itself, it would have been helpful if you had mentioned how to turn on Terminal services in the Services category of the Shortcuts tab in Keyboard preferences. New Terminal at Folder is under the Files and Folders heading and is not too hard to find. But Open man Page in Terminal is under the Text heading and can be hard to locate on a system where many third party apps have added their own services to the list. I took the opportunity to turn off some that I never use. Otherwise, the Services menu can get very unwieldy.