Skip to content

Instantly share code, notes, and snippets.

@ajitid
Last active July 22, 2024 16:24
Show Gist options
  • Save ajitid/81a4993be410586c038f8b3fc140b1c7 to your computer and use it in GitHub Desktop.
Save ajitid/81a4993be410586c038f8b3fc140b1c7 to your computer and use it in GitHub Desktop.
Up — Go up till you get to a directory that contains the folder name that you've specified

Up

Go up till you get to a directory that contains the folder name that you've specified.

Usage

Using just up results in cd ...

If you have a dir structure like:

work/
└── project-name
    ├── .git
    └── src
        ├── backend
        └── frontend
            └── src
                └── components
                    └── App

and you are in work/project-name/src/frontend/src/components/App/, then

  • doing up src will change your CWD to work/project-name/src/frontend/, or
  • doing up .git will change your CWD to work/project-name/

And you can go back to work/.../components/App using cd -.

You can help!

  • This script doesn't give any completion. If you have an idea or an implementation, please reply back in the comments.
  • This isn't published as a plugin yet.
  • Improvements are always welcome!

But but...

But my usecase is opposite, I need to cd into a nested directory!

Use autojump's jc command. (Or use fd-find+FZF)

function up
set -l dir_to_be_child $argv[1]
if [ "$dir_to_be_child" = "" ]
cd ..
else
cd -
set -l alt_dir $PWD
cd -
set -l curr_dir $PWD
set -l is_found 0
while [ "$PWD" != "/" ]; and test $is_found -eq 0
cd ..
set -l res (find $dir_to_be_child -maxdepth 0 -type d 2>/dev/null | wc -l)
if test $res -eq 1
set is_found 1
end
end
if [ "$PWD" = "/" ]
cd $alt_dir
cd $curr_dir
echo "Couldn't find $dir_to_be_child"
return 1
end
set -l found_dir $PWD
cd $curr_dir
cd $found_dir
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment