Bash: Change directory to project: cdp

From FVue
Jump to: navigation, search

Introduction

Cdp eases changing directory to a project. You define an environment variable `CDP_DIRS' for cdp to search and it will jump to the first found project. The project name is tab completed. If the destionation directory contains a file `.projrc' it will be executed. Example:

$ cdp ru  # Press TAB + ENTER
/mnt/proj/ruby$ # Working directory is now /mnt/proj/ruby

Download

Release notes

See RELEASE

Usage

From cdp --help:

Usage: cdp [options] [dir]
Change the current directory to (project) DIR.  The array variable CDP_DIRS
contains a list of directories to be searched.  If the directory is not found,
cd to the first valid directory of CDP_DIRS.

If DIR contains a file .projrc, it is executed unless --noprojrc is specified.

OPTIONS
    -h, --help             display this help and exit
        --prefix=PREFIX    use PREFIX for environment variables.  Default is CDP_
    -p, --projrcfile=FILE  execute commands from FILE instead of standard
                           initialization file .projrc
    -P, --noprojrc         do not execute project initialization file .projrc
    -t, --title=TITLE      specify terminal title.  Default is first dir of DIR.
    -T, --notitle          do not set terminal title
    -v, --version          show version

ENVIRONMENT
    CDP_DIRS
        An array variable whose members are the directories to be searched when
        the cdp command is executed.  Example definition:

            CDP_DIRS=($HOME/proj "$HOME/Program Files")

    CDP_OPTIONS 
        Default options to be placed in front of any explicit options.

Installation

1. Copy the file cdp.sh to a $PATH directory

2. Within ~/.bashrc, create an alias cdp to source cdp.sh in the current shell:

alias cdp='. cdp.sh'

3. Copy the file cdp-completion.inc.sh to /etc/bash_completion.d. If this directory doesn't exist, consider installing http://freshmeat.net/projects/bashcompletion

4. Within ~/.bashrc, set an environment variable CDP_DIRS. CDP_DIRS is an array variable containing directories to be searched by cdp. Example declaration:

CDP_DIRS=($HOME/proj "$HOME/Program Files")

Extending

Suppose you want to have an additional command cdq. With these settings in ~/.bashrc:

alias cdq='. cdp.sh --prefix=CDQ'
complete -o filenames -o nospace -F _cdp cdq

the command cdq will use environment variable CDQ_DIRS instead of CDP_DIRS.

Considerations

Sourced command or executed script

Why is cdp a sourced command and not an executable script?

  1. An executed script can't change the current directory. All it can do is change the current directory of the subshell in which the script is being executed
  2. Variable `CDP_DIRS' now is an array variable. Array variables can't be exported so can't be accessed by a subshell.

Test

The download contains an additional test suite. The test suite requires dejagnu, tcl and expect to be installed.

To run the tests:

$ cd test; ./runAll

Alternatives

Cdpath

You can also include the project directory in the CDPATH environment variable. This way, the project directory will also be searched for by the `cd' command. From `help cd':

"... The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH are separated by a colon (:). A null directory name is the same as the current directory, i.e. `.'. If DIR begins with a slash (/), then CDPATH is not used...

See also

Comments

blog comments powered by Disqus