Skip to content

Instantly share code, notes, and snippets.

@TimonLukas
Created November 28, 2019 09:16
Show Gist options
  • Save TimonLukas/878e50b6b78c978deb6d27003eb4db16 to your computer and use it in GitHub Desktop.
Save TimonLukas/878e50b6b78c978deb6d27003eb4db16 to your computer and use it in GitHub Desktop.
Register custom glob-based cd commands
typeset -A custom_cd_commands
custom_cd_commands=(ws "~/WebstormProjects/" xc "~/XcodeProjects" py "~/PycharmProjects")
function custom_cd() {
FOLDER="$(find ${1} -name "*${2}*" -type d -maxdepth 1 | head -n 1)"
if [ -n "$FOLDER" ]; then
cd "$FOLDER"
fi
}
for key in "${(@k)custom_cd_commands}"; do
eval "${key}() { custom_cd $custom_cd_commands[$key] \$1 }"
done
@TimonLukas
Copy link
Author

TimonLukas commented Nov 28, 2019

Note: This probably only works in zsh. To use it, copy and paste the above code to your ~/.zshrc.

Commands can be defined in the associative array in line 2. With the above definitions, I have 3 new commands: ws, xc and py.

Example:
I have a directory ~/WebstormProjects/cool-project. If I want to open that, I can just type ws pro. The script will use the find command to find folders matching the passed glob (only one level deep), and will cd into the first one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment