Last active
September 7, 2024 04:53
-
-
Save Gordin/67c9f5e995f4b625adf485eb791dea3e to your computer and use it in GitHub Desktop.
If you put this in your .bashrc/.zshrc you will be able to use cd to Windows style paths. This is probably only useful for WSL users.
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
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 "$*" | |
} |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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) withz
and it could work? 🤔