File Bindings

The File Binding Editor dialog lets you specify which program should be used to open specific types of files. In the normal case, you specify a file type by its file name suffix, e.g., .gif. These suffixes are not case sensitive. (The suffix does not have to start with a period, but it usually does.) You can also specify a file type via a regular expression that matches the beginning of a file's content. You signal that it is a regular expression by starting it with an up caret (^). (This also reminds you that it only matches at the beginning of the file.) As an example of a content type, the default set of file bindings includes a regular expression for recognizing email messages.

For each file type, the associated command specifies how to open the file. For most files, it is sufficient to simply enter the name of the program. The name of the file will be automatically appended as an argument.

In some special cases, the file name needs to be part of an expression rather than being simply the last argument. The variables $q and $u are provided for this purpose. $q is replaced by the file name enclosed in quotes, while $u is replaced by the plain file name. If you are unsure which to use, $q is always the safer bet because it allows file names to contain spaces. An example of the need for $u is the command for displaying a file in Netscape:

netscape -noraise -remote "openBrowser($u)"

The quotes are around the entire argument, so there should not be an extra set of quotes around the file name itself.

In rare cases, it may be necessary to use the full path to the file. In this case, use $qf and $uf.

The "Type" column at the right specifies additional information about a command. Typically, the command type is "Plain," which means that nothing special will be done when the command is run. If the command type is "Shell," the shell command near the bottom of the dialog will be used to run the command. This is necessary if the command contains shell meta-characters, e.g., "tar -tf $q | less". (Note that if the "Automatically use shell command when necessary" checkbox is on, the shell command will be used whenever the command contains shell meta-characters, even if the command type is "Plain.") If the command type is "Window," a terminal window will be created via the window command near the bottom of the dialog, and the command will then be run using the shell command. This is necessary if the program is text-based and designed to interact with the user via the keyboard, e.g., pine or vi.

Most programs can accept an arbitrary number of file names as arguments. The default behavior is therefore to group together all the files that should be opened by the same program and then to run this program once with all the files as arguments. However, there are some programs, such as most MPEG players, that can only open one file. If there is a dot in the "One at a time" column at the right, then the command will be invoked once for each file to be opened.

The default command

If the "Command for all other files" checkbox is on, the associated command will be used whenever a file does not match any of the types specified in the table. If the checkbox is off, you will be asked to specify the command for each file that does not match any of the types in the table. Since most miscellaneous files on UNIX are text files, it often makes sense to turn on the checkbox and specify your favorite text editor as the default command.

Working with the dialog

The "New," "Duplicate," and "Remove" buttons operate on the table of file types. "Save" saves the current contents of the dialog. "Revert" discards all changes since the dialog was opened or "Save" was clicked.

It is not necessary to close the dialog in order to use the bindings. You can keep the dialog open, changing the commands, clicking "Save," and then moving to another window and opening a file to test each command.

Useful tricks

If the command type is "Window," you can make the window remain open until you press Return (after the program finishes) by appending "; read" after the command. (This requires that the shell command use either sh or bash.)

System-wide bindings

System administrators can install system-wide bindings by adding them to the file /usr/lib/jx/jfs/file_bindings (or /usr/local/lib/jx/jfs/file_bindings on macOS). This is especially useful when a new package is installed on the system. By creating a binding for each file type defined by the package, all users will automatically be able to correctly open the files.

The format of the file is:

2 F F shell_command window_command
"" default_command command_type one_at_a_time
file_type command command_type one_at_a_time
...

The file_type and all the commands must be in quotes, and the standard backslashing rules apply. command_type is a single character: p for Plain, s for Shell, and w for Window. one_at_a_time is a Boolean value, and is specified by a single character: T for true, and F for false. The ellipsis indicates that an arbitrary number of file types can be included, with each one on a separate line.


Opening a file with an arbitrary command

If a file to be opened does not have an associated command, the "Run command with file" dialog will be opened to allow you to specify the command to use. The options are the same as in the File Binding Editor dialog. The "Browse" button lets you search for the program that you want to run.

If the file name has a suffix, e.g., .gif, then the "Always use this program for files of this type" checkbox will be visible. If you turn this on, the command that you specify will be saved in the File Binding Editor dialog and will be used from then on, so you will never be asked about files of this type again.


Running an arbitrary command

The "Run command" dialog lets you run an arbitrary command without first having to open a terminal window. You can also specify the directory in which to run the command. The "Run in shell" and "Run in window" checkboxes correspond to the "Shell" and "Window" command types, respectively, in the File Binding Editor dialog. If you turn off the "Stay open" checkbox, the dialog will close when you click the "Run" button.