ZSH Template Function Suites

From OS X Scientific Computing

Jump to: navigation, search
Return to ZSH on OS X.


Several sets of functions work together in a cluster or program suite. I'm singling these functions out because they are a bit more elaborate and powerful than some others, and many illustrate interactions between the zsh shell and the higher-level components of OS X.

Completions for the OS X open Command

We have written an augmented open command that is a wrapper for Apple's more limited open command, and an accompanying nuanced programmed completion system.

"open" is the command-line equivalent of double-clicking to open OS X applications, files, urls, x-windows unix programs, preference panes and directories.

Main features of the augmented open command

We've added a number of new options, so the complete list now includes:

-F  -- open with edit function
-a  -- specify application
-d  -- open with default application
-e  -- open with TextEdit
-f  -- open piped in file
-p  -- open PreferencePane of System Preferences
-s  -- open with sudo_open function
-w  -- open widget
-x  -- open with open-X11
-h  -- help menu

Main features of the open completion command

The accompanying _open function is based on one authored by Motoi Washida and distributed with recent versions of zsh, but modified extensively by Wataru Kagawa and William Scott.

The completions produced are specific to the option flag given. So for example, issuing

open -p A\tab

will give a list of all Preference Panes that start with A, eg:

% open -p A\tab
--- Preference Panes of System Preferences ---
Accounts.prefPane               Appearance.prefPane             Application\ Enhancer.prefPane

and the command

open -a w\tab 

gives as options all applications that start with W or w (including programs like Microsoft Word, where the W is the second part of the string):

% open -a w\tab
---- Mac OS X application
Java\ Web\ Start        Ocean\ Waves            Welcome                 WhatSize                Wipe\ Creator         
Microsoft\ Word         Wacom\ Tablet           Whamb                   Windows\ Media\ Player  wClock


Speeding up the "open -a" completions

The "open -a" completion uses the following command in real-time to find all of the applications:

mdfind -onlyin  /Applications -onlyin /Developer   "kMDItemContentType == 'com.apple.application-*'"

this is essentially the command-line version of SpotLight, and is not available prior to 10.4. Previously this completion relied on a cache regenerated by the locate command, so this is a major improvement made possible by the operating system.

Unfortunately, it is not instantaneous. I have found that periodically issuing the above command, when you don't need it, speeds up the completion (there must be a short-term cache mechanism). To exploit this, you can (optionally) use two LaunchAgent files:

If you want to try using these, I suggest placing these in your home directory ~/Library/LaunchAgents, but they can also go in the systemwide /Library/LaunchAgents directory.

A GUI for a shared directory stack

gdirs is documented here


User and Host completions

The two functions

are GUI menus to help you customize completions for commands (like ssh) that use either a list of users or a list of computer hostnames as completion options. This is discussed further in the Host and User names Completions Settings section of the environment.local explanation.


Terminal-finder interactions

Gary Kerbaugh has authored a number of functions, including

that augment the ability of the shell to interact with the OS X GUI file browser, called the Finder. These are described in detail on my Terminal-Finder Interactions web page.


Selecting a Prompt

The command


command generates a GUI menu that allows you to choose between the zsh-distributed prompts and two others I added ("simple" is just zsh-%, and "kerbaugh" is Gary Kerbaugh's elaborate and colorful prompt). This is also discussed in Customizing the Command-Line Prompt.


Please be aware that some distributed prompt themes may not work properly without additional fonts. Other prompt themes accept optional arguments that can be added by editing the file ~/.zsh/zshprompt after selecting the theme. The prompt functions are found in $fpath.


The switch_users command is a command-line access to Fast User Switching. With no argument, it gives you the login screen without having to log out. With another user's Username as an argument, it switches you directly to that user (then prompting for a password).

The USERS completion system works with this:


Command-line User management and other Sys-Admin Tools

Sometimes it is easier to add users, groups and so forth via the Command Line. For this we have

see also System Administrative Functions

Various Edit commands

The edit function allows you to customize what applications are used to edit different sorts of documents. So typing

edit foo.jpg

allows you to edit the figure in (say) Adobe Photoshop, whereas

edit foo.txt

allows you to edit the file foo.txt in your favorite designated ascii text editor.

The composer command allows you to edit an HTML file using SeaMonkey Composer or another selected GUI WYSIWYG HTML editor. (This is also available via the edit command).

The commands

are wrappers for vi(m) that allow you to edit files and save them, even if they require administrative permissions. The function discovers the ownership, and if you do not own the file, it invokes "sudo vim". There are a few other enhancements, including the ability to edit binary plist files.


I (privately) alias this to the unix "mail" command. macmail uses the Apple Mail.app interface as a drop-in replacement for sending mail via the unix mail command. macmail is a zsh function wrapper for a public-domain perl script called macmail.pl written by Nathaniel Nystrom.

NFS functions

make NFS networking setup easier.

Hardware Interactions

  • function_key_toggle lets you toggle on and off function keys on a laptop without pushing buttons.
  • scollspeed [1-7] incrementally increases/decreases mouse scroll speed.

Osascript and Pashua OS X GUI metafunction

These are completely general Osascript (Applescript for the command-line) functions to invoke menus (lists, file browsers, display dialogs, direcotry browsers) that make writing osascript/shellscripts much easier:


osa_display_dialog  foo bar default

gives a display with three buttons, the last being the default option. Pushing the button returns the value of the corresponding argument.

Similarly, for writing Pashua-enabled shell scripts, Wataru Kagawa developed a function called

It is based on the original script by Carsten Blum, developer of Pashua and Modified by Wataru Kagawa (06/02/05). The original bash script that executes Pashua was modified to a zsh function that can be autoloaded.


 pashua_run "<configuration>"

For example,

pashua_run "windowtitle = Hello World! \n tx_type = text \n tx_text = This is a test."

For details on the configuration syntax, see inside the Documentation folder that comes with the Pashua program.

A nice example of this is shown in the next section.

X-windows functions


Issuing the command

startwm twm

will start the (fugly) twm X-window manager nested in a single (rootless) X11.app window. startwm can be used to launch other window managers as well, including

  • evilwm
  • enlightenment
  • twm
  • startkde
  • gnome-session
  • xfce

and probably several others that I have not installed and tested. (All but twm have to be installed. You can do so with fink.)


This does not work properly on OS X >= 10.5

customize_x11_gui and customize_x11 are Pashua-GUI and shell scripts that do the same thing. They allow you to customize the behavior of X11.app, change default behaviors, and other options.


Apple distributes a command called "open-x11" that is in my humble opinion poorly written. Our [1] function is a drop-in replacement.

PLEASE NOTE: 10.5 (and more recent versions of OS X) make this obsolete.

The open-x11 command runs a program within the environment of the X-server running in the current user's session. In this augmented version, the function first opens the X11.app application if it finds it is not already running. It then correctly assigns the DISPLAY variable, permitting Fast User Switching compatibility when hosting several simultaneous X11 users. If fink is present, it detects that and sources the /sw/bin/init.sh file, setting all of the required environment variables.

Return to ZSH on OS X.
Personal tools