Read more

Working on the Linux command line: How to use bookmarks for directories

Judith Roth
September 17, 2021Software engineer at makandra GmbH

Bookmarks for directories will be most helpful if you are forced to work in deeply nested projects. Then it's really helpful!

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

This makes use of the CDPATH variable. Similar to the PATH variable, which holds the list of directories which are searched for executables, CDPATH contains the list of directories that are available for cd. Besides the current directory (.), you can add others to that.

The trick is to add a directory for bookmarks to CDPATH.

First, create the directory with: mkdir ~/.bookmarks.

Then add the following to your ~/.bashrc:

export CDPATH=.:~/.bookmarks/
function mark {
  ln -sr "$(pwd)" ~/.bookmarks/"$1"
}

# Always resolve symbolic links (e.g. for bookmarks)
alias cd="cd -P" 

This will add a new symlink to your bookmarks directory for the directory you're currently in.
The alias on the last line is optional.

To make sure you're not running into conflicts with the content of your current directory, always use a prefix for your bookmarks. @ works well. This has also has benefits for the usage with autocompletion.

Example usage:

>cd Projects/makandra-cards/
./Projects/makandra-cards/
>mark @cards
>cd ~
>pwd
/home/judith
>cd @cards
/home/judith/.bookmarks/@cards
>pwd
/home/judith/Projects/makandra-cards
Posted by Judith Roth to makandra dev (2021-09-17 19:20)