Skip to content

Instantly share code, notes, and snippets.

@nicholaschiasson
Last active May 4, 2017 02:01
Show Gist options
  • Save nicholaschiasson/18edfbbddc5ea8188a239dcb17159bde to your computer and use it in GitHub Desktop.
Save nicholaschiasson/18edfbbddc5ea8188a239dcb17159bde to your computer and use it in GitHub Desktop.
A small group of utilities and aliases to effectively add backward and forward change directory functionality to bash, similar to how internet browser backward and forward navigation buttons work.

cdmod

  • cdmod_aliases.sh

    A group of bash aliases enhancing on the capability to change directory.

    More specifically, these aliases provide the abilities to change directory backward to previous directories (not to be mistaken with parent directories), to change directory forward to directories previously changed back from, and to view the directory history for the current terminal session including all backward directories, the current directory, and all forward directories. Just source this file from your ~/.bashrc or similar files and these aliases will all be applied at the beginning of each new terminal session.

# cd - Change Directory
# Overriden cd to add current working directory to the previous directory
# history and erase the forward directory history before changing directory.
# Unfortunately, due to the limitations of aliases, the forward directory
# history must be discarded regardless of whether or not a valid directory
# change is requested (ie. change to current directory or change to invalid
# directory, both resulting in not actually changing directory).
alias cd='
unset NEXT_DIRECTORIES
unset NEXT_DIRECTORIES_COUNT
if [ -z "$PREVIOUS_DIRECTORIES" ] || ([ -n "$PREVIOUS_DIRECTORIES" ] && [ "$PWD" != "${PREVIOUS_DIRECTORIES[$PREVIOUS_DIRECTORIES_COUNT-1]}" ]); then
PREVIOUS_DIRECTORIES[((PREVIOUS_DIRECTORIES_COUNT++))]="$PWD"
fi
cd' # Do not put a newline between this cd and closing quotation, otherwise the alias will not work!
# b - Back
# Change directory back to the previous working directory.
alias b='
if [ -n "$PREVIOUS_DIRECTORIES" ]; then
[ "${PREVIOUS_DIRECTORIES[$PREVIOUS_DIRECTORIES_COUNT-1]}" == "$PWD" ] && PREVIOUS_DIRECTORIES[((--PREVIOUS_DIRECTORIES_COUNT))]=
PREVIOUS_DIRECTORY="${PREVIOUS_DIRECTORIES[$PREVIOUS_DIRECTORIES_COUNT-1]}"
if [ -n "$PREVIOUS_DIRECTORY" ]; then
PREVIOUS_DIRECTORIES[((--PREVIOUS_DIRECTORIES_COUNT))]=
if [ -z "$NEXT_DIRECTORIES" ] || ([ -n "$NEXT_DIRECTORIES" ] && [ "$PWD" != "${NEXT_DIRECTORIES[$NEXT_DIRECTORIES_COUNT-1]}" ]); then
NEXT_DIRECTORIES[((NEXT_DIRECTORIES_COUNT++))]="$PWD"
fi
\cd "$PREVIOUS_DIRECTORY"
fi
fi
'
# f - Forward
# Change directory forward to the next working directory if a forward directory
# history has been made by changing directory backwards.
alias f='
if [ -n "$NEXT_DIRECTORIES" ]; then
[ "${NEXT_DIRECTORIES[$NEXT_DIRECTORIES_COUNT-1]}" == "$PWD" ] && NEXT_DIRECTORIES[((--NEXT_DIRECTORIES_COUNT))]=
NEXT_DIRECTORY="${NEXT_DIRECTORIES[$NEXT_DIRECTORIES_COUNT-1]}"
if [ -n "$NEXT_DIRECTORY" ]; then
NEXT_DIRECTORIES[((--NEXT_DIRECTORIES_COUNT))]=
if [ -z "$PREVIOUS_DIRECTORIES" ] || ([ -n "$PREVIOUS_DIRECTORIES" ] && [ "$PWD" != "${PREVIOUS_DIRECTORIES[$PREVIOUS_DIRECTORIES_COUNT-1]}" ]); then
PREVIOUS_DIRECTORIES[((PREVIOUS_DIRECTORIES_COUNT++))]="$PWD"
fi
\cd "$NEXT_DIRECTORY"
fi
fi
'
# h - History
# Show the entire directory history including all previous directories, all
# forward directories, and the current directory.
alias h='
echo "Change Directory History:"
if [ -n "$PREVIOUS_DIRECTORIES_COUNT" ] && [ $PREVIOUS_DIRECTORIES_COUNT -gt 0 ]; then
PREV_DIR_COUNT=$PREVIOUS_DIRECTORIES_COUNT
[ "${PREVIOUS_DIRECTORIES[$PREVIOUS_DIRECTORIES_COUNT-1]}" == "$PWD" ] && ((PREV_DIR_COUNT--))
for ((i=0; i<PREV_DIR_COUNT; i++)); do
echo -e "\t${PREVIOUS_DIRECTORIES[$i]}"
done
fi
echo -e "->\t$PWD"
if [ -n "$NEXT_DIRECTORIES_COUNT" ] && [ $NEXT_DIRECTORIES_COUNT -gt 0 ]; then
NEXT_DIR_COUNT=$NEXT_DIRECTORIES_COUNT
[ "${NEXT_DIRECTORIES[$NEXT_DIRECTORIES_COUNT-1]}" == "$PWD" ] && ((NEXT_DIR_COUNT--))
for ((i=NEXT_DIR_COUNT-1; i>=0; i--)); do
echo -e "\t${NEXT_DIRECTORIES[$i]}"
done
fi
'
MIT License
Copyright (c) 2017 Nicholas Omer Chiasson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment