Skip to content

💽 Easily access the directories you 'cd' into most often

Notifications You must be signed in to change notification settings

balbuf/cd-collection.sh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 

Repository files navigation

cd Collection

Easily manage and access the directories you cd into most often. cd Collection allows you to "bookmark" any local directory with an alias of your choosing to quickly jump to that location or use it as a jumping point to access relative subdirectories or parent directories. Aliases are unique, but a directory may have more than one alias.

Basic Usage

# navigate to a directory you cd into often
/$ cd ~/Documents/

# add a new cd alias to the current directory
~/Documents$ cdc add docs
Success: Added 'docs' pointing to /Users/balbuf/Documents

# navigate elsewhere
~/Documents$ cd /

# execute `cdc` with your new alias name
/$ cdc docs

# you are taken to the target directory of your new alias
~/Documents$

Installation

To install, run the following line in your shell:

curl -sL https://raw.githubusercontent.com/balbuf/cd-collection.sh/master/install.sh | sh

This will automatically download the latest release of the cdc source file and attempt to source it from your shell initialization file (e.g. .bashrc - determined based on the shell you are using). You can modify the behavior of the install script with the following environment variables:

  • $CDC_LOCATION to specify where you would like the source file to be downloaded to
  • $INIT_FILE to specify the path to your shell initialization file

Commands

add

Add a new alias for the current directory without overwriting an existing alias. Returns an error if the alias already exists, so it is safe from affecting other aliases.

cdc add <alias-name>

To create a new alias, first navigate to the desired target location:

$ cd ~/Documents
$ cdc add docs

Alias names can contain any characters that an actual directory name can contain, i.e. pretty much anything besides a forward slash (/) or null character.

get

Get the path of a given alias. Similar to (and using) pwd, this command will print the full target target path for the alias.

cdc get <alias-name>[/<relative-path>]

The get commmand can be used with an alias alone:

$ cdc get logs
> /var/logs

or with an additional relative path along with the alias:

$ cdc get logs/apache2
> /var/logs/apache2

If cdc detects that it is executed outside of an interactive shell, the get command is implied in the absense of an explicit command:

$ echo "The path for the 'logs' alias is '$(cdc logs)'"
> The path for the 'logs' alias is '/var/logs'

$ tail -f $(cdc logs/apache2)/error_log

To avoid collisions with cdc command names, the alias name can be preceded by --:

$ echo "Help files are located at $(cdc -- help)"
> Help files are located at /Users/me/Documents/help-files

go

Change current directory to the path of the given alias.

cdc go <alias-name>[/<relative-path>]

As with get, go can act upon a plain alias:

$ cdc go home

or with a relative path under or above the alias target location:

$ cdc go home/..

If cdc detects that it is executed inside of an interactive shell, the go command is implied in the absense of an explicit command. This is the most common use case:

$ cdc repos

help

Display a brief help message covering basic usage and a list of commands.

cdc help

$ cdc help

The same help message is displayed if cdc is executed with no arguments.

ls (list)

See a list of some or all existing aliases.

cdc ls [<path>]

List all aliases:

$ cdc ls

List all aliases pointing to the current directory:

$ cdc ls .

List all aliases pointing to your ssh directory:

$ cdc ls ~/.ssh

set

Add or overwrite an existing alias pointing to the current directory. Unlike add, this command will forcefully replace an existing alias with the same name.

cdc set <alias-name>

Navigate to the directory of your choosing to set an alias pointing there:

$ cd ~/.ssh
$ cdc set ssh

rm (remove)

Attempt to remove an existing alias. Returns an error if the alias is not valid or if it cannot be removed.

cdc rm <alias-name> cdc remove <alias-name>

Delete an alias named "docs":

$ cdc rm docs

Tab Completion

cdc offers tab completion for completing alias names and directory names relative to the target directory of the alias. This behaves similarly to tab completion with the cd command itself.

Other Solutions

Here are some other solutions you might consider installing for jumping around in your shell:

Some solutions you can add to your .bashrc or equivalent:

  • To navigate up n number of levels from a subdirectory:

    # Usage: `up` or `up n`, e.g. `up 3` to go up 3 directories, equivalent to cd ../../..
    function up() {
      [ -z "${1//[0-9]}" -a ${1:-1} -ne 0 ] || return
      cd $(printf '../%.s' $(seq 1 $1))
    }
  • To navigate to the root directory of the git repo you are within:

    # Goto root directory of current git repo
    alias gcd='cd "$(git rev-parse --show-toplevel)"'

Some tricks you can use directly with cd:

  • cd (no arguments) to jump to your home directory, equivalent to cd ~
  • cd - to jump to the previous directory you were in before the last time you used cd

About

💽 Easily access the directories you 'cd' into most often

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages