-
-
Save Gordin/67c9f5e995f4b625adf485eb791dea3e to your computer and use it in GitHub Desktop.
cd() { | |
# Check if no arguments to make just typing cd<Enter> work | |
# Also check if the first argument starts with a - and let cd handle it | |
if [ $# -eq 0 ] || [[ $1 == -* ]] | |
then | |
builtin cd $@ | |
return | |
fi | |
# If path exists, just cd into it | |
# (also, using $* and not $@ makes it so you don't have to escape spaces any more) | |
if [[ -d "$*" ]] | |
then | |
builtin cd "$*" | |
return | |
else | |
# Try converting from Windows to absolute Linux path and try again | |
WSLP=$(wslpath -ua "$*") | |
if [[ -d "$WSLP" ]] | |
then | |
builtin cd "$WSLP" | |
return | |
fi | |
fi | |
# If both options don't work, just let the builtin cd handle it | |
builtin cd "$*" | |
} |
This needs to be updated to support cd enhancing tools like https://github.com/rupa/z or https://github.com/skywind3000/z.lua
@dufferzafar I don't use these tools, but I would guess that you could just replace all instances of cd
(including the function name in line 1) with z
and it could work? 🤔
Is there a way to use this function when CDing into windows directories where you don't have to surround the path with quotes. This is what I mean:
\# works but you need quotes on first argument
\# cdw() { cd $(wslpath "$1"); }
\# Your Function does not support this functionality.
\# In bash shell
cdw C:\tmp
\#causes an error
@morphykuffour Probably not. The problem with Windows paths is that they contain \
, which will probably lead to your terminal thinking parts of the path are actually escape sequences. Because this happens before the command is actually called, you can't really do anything about this.
This needs to be updated to support cd enhancing tools like https://github.com/rupa/z or https://github.com/skywind3000/z.lua