8

The ~ character is a universal alias in the Unix word for referencing the home directory of the user. It is a special character that will be parsed and replaced by the full path of the current user's home directory. Is there an equivalent of this in cmd.exe ? (not Powershell)

adamency
  • 376

1 Answers1

18

No.

You can use commands like cd /D %homedrive%\%homepath% or cd /D %userprofile% but typing them is just not the same even if end result is. The closest to the simplicity of cd ~ I've ever seen is Señor CMasMas's elegant solution below.

Create a new bat file with one single line:

@cd /d %UserProfile% –

Save it with name cd~.bat into any folder in your %PATH%. After that you can get from anywhere in the system back to home directory by typing command

cd~
phuclv
  • 27,773
Peregrino69
  • 4,664
  • 13
    It is funny that you mention this. I have a cd~.bat file in my path that has one line in it.. @cd /d %UserProfile% – Señor CMasMas Sep 23 '21 at 02:18
  • 1
    That's the most elegant solution I've ever seen :D – Peregrino69 Sep 23 '21 at 09:35
  • 6
    Be aware that %homepath% does not include the drive, just the path relative to that drive. %homedrive% contains the drive letter. – Ross Presser Sep 23 '21 at 12:14
  • @RossPresser Good point, let's update the answer :-) – Peregrino69 Sep 23 '21 at 12:16
  • Batch file from batch file should be called via call command. – pbies Sep 23 '21 at 20:45
  • 1
    In Unix you wouldn't even type cd ~, just cd because $HOME is already the default destination. ~ is useful when you're cd'ed somewhere else and want to reference a file in your home dir, e.g. vim ~/notes.txt or cp -a foo ~/bin/. (Or in a script where you might be.) Anyway, isn't %homedrive%\%homepath% basically equivalent to $HOME, expanding an environment variable? When you say it's "not the same", do you mean it's too long to type? – Peter Cordes Sep 23 '21 at 22:30
  • @PeterCordes Yap, I generally drop the tilde except in scripts, cd ~ just has a bit of charming nostalgia :-) And of course written down cd and cd ~ don't mean the same thing. %homedrive%\%homepath% as concept is more /users/$USER, %UserProfile% is more $HOME, methinks. I updated the answer again. – Peregrino69 Sep 23 '21 at 23:07
  • In Unix, cd and cd ~ do mean the same thing. Tilde expansion happens after word splitting, so even if your $HOME contains spaces, it still works like cd "$HOME" instead of breaking. Maybe you meant in Windows? I don't know Windows cmd.exe very well. – Peter Cordes Sep 23 '21 at 23:15
  • They do, typed in shell, but "typed in shell" != "written down". Writing on a paper, forum etc. cd = "change directory", but cd ~ = "change directory to $HOME". That's what I meant :-) – Peregrino69 Sep 23 '21 at 23:25