Skip to content

Latest commit

 

History

47 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cd-bookmarks.sh - add bookmarks to cd (in bash)

This (ab)uses the CDPATH functionality of bash to add bookmark functionality, with tab completion, to the built in cd command. It also offers optional pushd / popd integration.

Why: CDPATH is cool, but it's useability really sucks. Turning it on and off, or using aliases, or other attempts to tame it just didn't give me the desired result. By dynamically setting CDPATH we can have as many bookmarks as we want, and only use them when we explicitly ask for it.

Under the hood it's all just the normal cd command (with some pushd and popd. All your favorite flags like -P or -@ still get passed along.

Tab completion is a slightly modified version of the normal cd completion.

export cd_includebookmarks=1 includes the bookmarks in tab completion for the first argument.

export cd_includebookmarks=2 will show ONLY bookmarks in tab completion for the first argument

Usage:

cd -b  # list bookmarks

cd [-b] bookmark # cd to a bookmark

cd my_bookmark subdir
cd -b my_bookmark subdir
cd subdir -b my_bookmark # cd to a directory "subdir" below bookmark "my_bookmark"

# bookmark creation via cd/cdb
cd --save my_bookmark /path/to/dir  # save bookmark persistently
cd --save my_bookmark               # save current directory ($PWD)
cd --mem scratch /tmp               # temporary/session-only bookmark
bookmark my_bookmark /path/to/dir   # compatibility alias

# with pushd enabled
~$ cd /dir1
/dir1$ cd /dir2
/dir2$ cd /dir3
/dir3$ cd -p
/dir2$ cd -p
/dir1$ cd -p
~$

Setup:

Load cd-bookmarks, in .bashrc or elsewhere:

source /path/to/cd-bookmarks.sh

And set your bookmarks:

cd_bookmarks[project1]=/path/to/bookmark
cd_bookmarks[project2]=/other/bookmark
cd_bookmarks[both_projects]=/path/to/bookmark:/other/bookmark
cd_bookmarks[local_tmp]=~/.local/tmp

# Update the index after changing bookmarks
cd --update

Testing

Run the regression suite:

bash tests/regression.sh

The suite covers bookmark persistence, argument resolution, pushd behavior, and configuration/index edge-cases.

Persistent bookmark file

By default, persistent bookmarks are stored in:

$HOME/.cd_bookmarks

You can override this by setting CD_BOOKMARKS_FILE before loading cd-bookmarks.sh:

export CD_BOOKMARKS_FILE="$HOME/.config/cd-bookmarks/bookmarks.sh"

The default bookmark is ., but you can change that if you want.

cd_bookmarks["default"]=.:~/work

You may optionally have it use pushd and add "cd -p" to call popd. These let you keep a history of the paths you have been in and return to them.

    cd -p # run "popd"
    cd -v # run "dirs -v"
    cd -c # run "dirs -c"

e.g.:
  ~$ cd mydir

  ~/mydir$ cd /usr/mydir2

  /usr/mydir2$ cd -p

  ~/mydir$ cd -p

  ~$

About

cd-bookmarks.sh - add bookmarks to cd (in bash)

Resources

Stars

10 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages