Created
November 28, 2019 09:16
-
-
Save TimonLukas/878e50b6b78c978deb6d27003eb4db16 to your computer and use it in GitHub Desktop.
Register custom glob-based cd commands
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
andpy
.Example:
I have a directory
~/WebstormProjects/cool-project
. If I want to open that, I can just typews pro
. The script will use thefind
command to find folders matching the passed glob (only one level deep), and willcd
into the first one.