Last active
March 2, 2023 06:03
-
-
Save thingsiplay/4973b1d76ed4cde685a43bfd3930e433 to your computer and use it in GitHub Desktop.
CDIRS - Set of functions for Bash, to add and jump to user defined folders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# ~/.bashrc | |
# | |
# CDIRS | |
# Set of functions for Bash, to add and jump to user defined folders. | |
# | |
# Examples: | |
# cadd | |
# cadd ~/Downloads/new | |
# cadd bin | |
# cdirs | |
# cdir | |
# cdir new | |
# cdel | |
# | |
# User file with all added paths. | |
cdirs="$HOME/.cdirs" | |
# List content of cdirs file. | |
cdirs () { | |
# echo "${cdirs}:" | |
cat "${cdirs}" | |
} | |
# Change directory to argument matching a regular expression in the file. | |
# The last part of directory is checked for a match. | |
# If that fails, then a regex is checked against entire path. | |
# The first match is selected to change into. | |
# Alternatively, if no argument is given, then a menu for selection is created. | |
cdir () { | |
if [ -z $1 ] | |
then | |
# Create menu with preview. | |
dir=$(cat "${cdirs}" | sort | uniq | fzf --preview "ls -A \$(echo {} | sed \"s:~:$HOME:g\")") | |
else | |
# First try to match last part of directory... | |
dir=$(grep -m 1 -i -E "/[^/]*${1}[^/]*\$" "${cdirs}") | |
if [ $? -eq 1 ] | |
then | |
# ... otherwise match entire path. | |
dir=$(grep -m 1 -i -E "${1}" "${cdirs}") | |
fi | |
fi | |
if [ $? -eq 0 ] | |
then | |
# Change directory. | |
dir=$(echo "${dir}" | sed "s:^~:${HOME}:g") | |
cd "${dir}" | |
pwd | sed "s:^${HOME}:~:g" | |
ls | |
fi | |
} | |
# Add a new path to the list. | |
# Remove all duplicates of the new path, if any present. | |
# Either add current path or through argument. | |
cadd () { | |
if [ -z $1 ] | |
then | |
dir="${PWD}" | |
else | |
dir=$(readlink -f "${1}") | |
fi | |
dir=$(echo "${dir}" | sed "s:^${HOME}:~:g") | |
grep -vx -F "${dir}" "${cdirs}" > "${cdirs}".tmp | |
echo "${dir}" >> "${cdirs}".tmp | |
mv "${cdirs}".tmp "${cdirs}" | |
echo "${dir}" | |
} | |
# Remove a path from file, with all its duplicates. | |
# Either delete current path or through argument. | |
cdel () { | |
if [ -z $1 ] | |
then | |
dir="${PWD}" | |
else | |
dir=$(readlink -f "${1}") | |
fi | |
dir=$(echo "${dir}" | sed "s:^${HOME}:~:g") | |
grep -vx -F "${dir}" "${cdirs}" > "${cdirs}".tmp | |
mv "${cdirs}".tmp "${cdirs}" | |
echo "${dir}" | |
} | |
# Validate all current paths in file and remove non existing paths. | |
# Does not remove duplicates. | |
cval () { | |
while read line; do | |
line=$(echo -n "${line}" | sed "s:^~:${HOME}:g") | |
readlink -e "${line}" | sed "s:^${HOME}:~:g" >> "${cdirs}".tmp | |
done < "${cdirs}" | |
rm "${cdirs}" | |
mv "${cdirs}".tmp "${cdirs}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was bored and created my own set of functions to have some basic functionality in the vein of AutoJump and similar tools. Most important functions are
cadd
to add current directory (accepts argument too) andcdir
to create a menu with folder preview to jump to. Ifcdir
is given a search term, then it will try to match basename, otherwise tries to match entire path.All users home path is automatically converted back and to "~" (even in
fzf
menu with functioning preview). So a search term does not match useless home path, that is probably inside of most paths.This was little fun project in the night and I hope you have fun with it too. Just copy the functions into your ".bashrc" and you are good to go. This is not rock solid or anything, so don't depend your life on them.