Skip to content

Instantly share code, notes, and snippets.

@chanux

chanux/xs.fish Secret

Last active March 12, 2021 03:40
Show Gist options
  • Save chanux/08c6f53472190a02b33bd49100163d93 to your computer and use it in GitHub Desktop.
Save chanux/08c6f53472190a02b33bd49100163d93 to your computer and use it in GitHub Desktop.
Jump to a directory up the tree by name
function xs
# go to /home/chanux/down from /home/chanux/down/the/rabbithole/we/go
# by typing xs down
# you might also like za http://wp.me/p1rVu-bO
set matches (echo "$PWD" | grep -o "/$argv[1][^/]*/" | wc -l)
if test $matches -gt 1
cd (echo $PWD | sed "s:/$argv[1]/.*:/:")$argv[1]
else if echo "$PWD" | grep -q "/$argv[1]/"; test $status -eq 0
cd (echo $PWD | sed "s:$argv[1].*::")$argv[1]
end
end
xs() {
# For BASH and ZSH
if [[ "$PWD" == */$1/* ]]
then
cd ${PWD%$1*}$1
fi
}
@chanux
Copy link
Author

chanux commented Dec 12, 2017

Had this idea while using za today. Quickly implemented for my shell <"><
Not too shabby!

Known bug: If there are multiple directories by the same name, it'd go to the farthest

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