sd -- a drop-in replacement for `cd'

The `sd' man page
Login

This is a mostly verbatim copy of the sd internal manpage that can also be accessed with the command dirs -m after sd has been correctly installed. You might also be interested in looking at the quickstart guide.



NAME

sd − switch between directories using a dynamic directory stack

SYNOPSIS

sd  [pattern|pathname|-]
sdirs [−hmpfsict] [−l num] [−d pattern] [pattern]

DESCRIPTION

The sd utility consists of a set of (ksh) shell functions enabling rapid navigation between recently visited directories. sd keeps track of all recent cd activities with the help of a logfile. This logfile is analyzed to generate a frequency distribution of the visits to different directories. The directory stack can be queried by further sd commands using pattern matching. A sliding window (containing a limited number of trailing entries from the logfile) is used. This leads to a dynamically changing stack which is updated after each sd action. While the logfile is shared by all running shells, the directory stack is not. Therefore, modifications of the logfile (and stack) by sd actions in one shell do not immediately affect the stack in another shell, which is only updated after the next sd action. sdirs -f can be used to enforce an update.

Some care is taken to maintain compatibility with bash and zsh. sd can be used in combination with tcsh, too, through the auxiliary ksh scripts sdcmd and sdirs. However, functionality is somewhat restricted in this case.

In all shells, interaction with sd proceeds solely via sd and sdirs.

USAGE

After correct setup sd can be used as a replacement for cd. sd takes one or more arguments that together define a string pattern (possibly containing blanks) which is first tried as a pathname. The special cases "sd" and "sd -" work as expected. If pathname interpretation fails, pattern is used for a lookup in the last sdlines directories visited (not counting your home directory). In this case pattern can be any valid regular expression. Characters special to the shell might need quoting and so do characters with special meaning to regex matching. Thus a lookup of a verbatim a.b requires the pattern a\\.b in order to get the quoting through.

The search is performed top-down starting at the most frequently visited (most recently visited, if sdbytime=1) directory. The working directory is changed to the first match found. If this match is not the correct one, the search pattern has to be refined. Frequently, trailing substrings from the full pathname, notably the basename, work fine. Alternatively, it is also possible to switch between directories with sd =rank where rank is the rank index displayed by sdirs. sd = is equivalent to sd =1. Quoting of the = might be necessary with older versions of tcsh and with zsh.

By default sdirs lists the directory stack in the three column format frequency rank directory_name sorted top-down according to search order (usually by frequencies). Specifying a pattern as argument restricts the display to matching entries.

sdirs also serves as user interface for performing other tasks according to chosen option as detailed below.

OPTIONS

sdirs accepts these options:

−h      Show short usage note.

−m      Show manpage.

−p      If specified before −m, convert manpage to postscript and send to
        stdout.

−f      Forces a refresh of the directory stack. This might be helpful if
        sdlines" and "sdbytime" are modified interactively or if several
        shell incarnations are running in parallel.

−s      Displays the elements in the directory stack alphabetically
        instead of according to search order. Might be helpful for
        locating some ill-remembered pathname to find out its rank for a
        "sd =rank" command.

−i      Show status info for logfile and stack.

−c      Remove stale entries no longer pointing to an existing directory
        (and the root directory) from the logfile and update the directory
        stack.

−t      Toggle sorting of directory stack (and, thus, search order)
        between "frequency of visit" and "time of visit".

−l num  Change the number of recently visited directories considered when
        constructing the directory stack. As a special case, when the
        provided argument is zero or non-numeric, the complete currently
        available logfile content is used (so the easiest call to achieve
        this would be sdirs -ll).

−d pattern
        Delete all entries matching the pattern from the logfile (deletion
        is performed only after a final confirmation by the user) and
        update the directory stack.

Options −t and −l are not available when used with tcsh. You have to modify the corresponding environment variables directly (see section Setup for tcsh below).

SHELL VARIABLES

There are a few user-settable shell or environment variables recognized by sd. Regarding meaning of sdmax and sdlines see INITIAL SETUP section. sdlines can be changed with dirs -l num. The variable sdbytime is used to influence the way the stack is sorted. By default the stack is sorted according to frequencies (top-down from high to low) which corresponds to sdbytime=0. If sdbytime=1, sorting occurs by time of visit (top-down from new to old). Value can be toggled between 0 and 1 with sdirs -t.

INITIAL SETUP

Setup for ksh, bash, and zsh

  1. Put the file sd.ksh in a directory on the search path.

  2. Add a source sd.ksh command to your startup file. The source command can be preceded by assignments to the variables sdmax and sdlines. sdlines defines the number of previous cd actions which are analyzed by sd. sdmax is the maximum number of cd actions logged in the file ~/.sd/dirv. If this limit is reached the file is pruned to the sdmax*3/4 or sdlines most recent cd actions, whichever is larger. If these parameters are not explicitly assigned, the defaults sdmax=8192 and sdlines=512 are used. sdlines can be modified interactively at any time to alter the "time window" accessible via the stack.

If a cd function is not already existing at the time sd.ksh is sourced, sd defines it as

   function cd {
      sd "$*"
   }

which effectively aliases cd to sd. If you have your own cd function you might want to include the sd call in that function. sd furthermore defines

   alias dirs=sdirs

which overrides the dirs builtin in bash and zsh. You probably don’t want to use the builtin in parallel to sd anyway, but if you need it simply issue

   unalias dirs

after sourcing sd.ksh.

bash users

Note that sd sets the non-standard option shopt -s extglob which activates ksh like extended shell glob patterns. If this interferes with your setup, don’t use sd.

zsh users

Note that sd sets the non-standard options set -o KSH_GLOB (which activates ksh like extended shell glob patterns) and set -o POSIX_BUILTINS (which enables the command builtin to execute shell builtins). If this interferes with your setup, don’t use sd.

Setup for tcsh

  1. Put files sd.ksh, sdcmd, and sdirs in a directory on the search path. Make the last two of these executable.

  2. Add the following lines verbatim (including the quotes) to your startup file:

    alias cd   ’eval "`sdcmd \\!*`"’
    alias dirs ’sdirs’
    alias dirs ’ds’
    

Note that the second alias overwrites the dirs builtin of tcsh. If you don’t like this, omit this alias definition.

To modify variables relevant to sd, you have to export them to the environment of your shell with setenv.