Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active August 17, 2023 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RichardBronosky/05b3c59b406a6425ad2cf660b1b8b850 to your computer and use it in GitHub Desktop.
Save RichardBronosky/05b3c59b406a6425ad2cf660b1b8b850 to your computer and use it in GitHub Desktop.
A bash plugin to make cd better

bash-cdl

A bash plugin to make cd better

Installation

Set up bash_plugins folder

(Skip this step if you already have a bash_plugins folder.)

{
# simply copy-pasta this whole block, or customize these vars and copy-pasta the rest
bash_plugins_dir=~/.bash_plugins
bash_startup_file=~/.bash_profile

if ! [[ -d $bash_plugins_dir ]]; then
  mkdir $bash_plugins_dir
fi
if ! grep -q '\*\.plugin\.sh' $bash_startup_file; then
  cat >> $bash_startup_file <<BASH

export BASH_PLUGINS_DIR="$bash_plugins_dir"
for plugin in \$(find \$BASH_PLUGINS_DIR -name '*.plugin.sh'); do source \$plugin; done
BASH
  export BASH_PLUGINS_DIR="$bash_plugins_dir"
fi
}

Set up this plugin

{
# simply copy-pasta this whole block
plugin_url=https://gist.github.com/05b3c59b406a6425ad2cf660b1b8b850

if bashplug -vq >/dev/null 2>&1; then
  bashplug add "$plugin_url"
elif [[ -n "$BASH_PLUGINS_DIR" ]]; then
  (
    set -e
    cd "$BASH_PLUGINS_DIR"
    git clone $plugin_url
    dir_name="$(ls -rtd1 $(find * -maxdepth 0 -type d) | tail -n1)"
    plugin_name="$(basename $(find $dir_name -name '*.plugin.sh') .plugin.sh)"
    if ! [[ "$dir_name" == "$plugin_name" ]] && ! [[ -d "$plugin_name" ]]; then
      mv "$dir_name" "$plugin_name"
      dir_name="$plugin_name"
    fi
    for plugin in $(find $dir_name -name '*.plugin.sh'); do source $plugin; done
  )
else
  cat<<EOF

Could not find your BASH_PLUGINS_DIR. Did not install plugin.
Have you followed the *Set up bash_plugins folder* steps yet?

EOF
fi
}
function init_via_source(){
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local script=$(basename ${BASH_SOURCE[0]} | sed 's/\.plugin//')
source $script_dir/$script
}
function init_via_wrapper(){
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local script=$(basename $BASH_SOURCE | sed 's/\.plugin//')
local wrapper=$(basename $script .sh | sed 's/^bash-//')
alias $wrapper="$script_dir/$script"
}
init_via_source
# vim: set ts=2 sts=2 sw=2 et:
export CD_HIST_FILE=/tmp/cd_hist.txt
function cd(){
builtin cd "$@"
pwd>>$CD_HIST_FILE
}
function cdl(){
cd $(tail -n1 $CD_HIST_FILE)
};
# vim: set ts=2 sts=2 sw=2 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment