pwd alias on Windows cmd
I want to create a fixed pwd
alias like in this topic, but I want to keep all aliases in one file as in this answer by Argyll. Currently my file cmdAliases.cmd looks like this:
@echo off
doskey ls=dir
doskey pwd=echo ^%cd^%
Running pwd
command now prints:
ECHO is on.
I believe the spaces in the command are the problem. Is there a way to fix it using only the cmdAliases.cmd file?
windows cmd
add a comment |
I want to create a fixed pwd
alias like in this topic, but I want to keep all aliases in one file as in this answer by Argyll. Currently my file cmdAliases.cmd looks like this:
@echo off
doskey ls=dir
doskey pwd=echo ^%cd^%
Running pwd
command now prints:
ECHO is on.
I believe the spaces in the command are the problem. Is there a way to fix it using only the cmdAliases.cmd file?
windows cmd
3
doskey pwd=cd
. Thecd
command without parameters will just print the current working folder.
– Stephan
Nov 20 '18 at 10:33
1
Stephan's solution is the easiest one for command prompt and in a batch file.doskey pwd=echo ^%cd^%
is the right syntax onpwd
definition from within a command prompt window forecho %cd%
. In a batch file must be useddoskey pwd=echo %%cd%%
to define execution ofecho %cd%
on typingpwd
. Please note thatecho %cd%
as alias forpwd
is in general not good in case of current directory contains in path an ampersand because of everything after&
is interpreted bycmd.exe
as additional command to execute. So best is definitelydoskey pwd=cd
.
– Mofi
Nov 20 '18 at 10:46
@Mofi, percent can't actually be escaped in an interactive CMD shell. We escape it in practice by disrupting the name matching, assuming no variable has "^" in its name. Thus in this case we want the "^" character on the right-hand side of the first "%" or the left-hand side of the second "%", or both for good measure. For example, "%^cd^%" has CMD look for a variable named "^cd^", and if none is defined it leaves the literal string "%^cd^%" in place. In the final parsing of the command line, the "^" escape character gets removed, leaving just "%cd%".
– eryksun
Nov 20 '18 at 23:07
@eryksun You are absolutely right and you explained it very good. I knew that already, but I did not want to write it here in a comment to avoid confusing original poster with such deep insight on how Windows command processor parses a line in command prompt window in comparison to parsing a command line in a batch file. However, the difference is explained in detail now by you with your comment. Thank you.
– Mofi
Nov 21 '18 at 9:21
add a comment |
I want to create a fixed pwd
alias like in this topic, but I want to keep all aliases in one file as in this answer by Argyll. Currently my file cmdAliases.cmd looks like this:
@echo off
doskey ls=dir
doskey pwd=echo ^%cd^%
Running pwd
command now prints:
ECHO is on.
I believe the spaces in the command are the problem. Is there a way to fix it using only the cmdAliases.cmd file?
windows cmd
I want to create a fixed pwd
alias like in this topic, but I want to keep all aliases in one file as in this answer by Argyll. Currently my file cmdAliases.cmd looks like this:
@echo off
doskey ls=dir
doskey pwd=echo ^%cd^%
Running pwd
command now prints:
ECHO is on.
I believe the spaces in the command are the problem. Is there a way to fix it using only the cmdAliases.cmd file?
windows cmd
windows cmd
asked Nov 20 '18 at 10:20
koman900koman900
807
807
3
doskey pwd=cd
. Thecd
command without parameters will just print the current working folder.
– Stephan
Nov 20 '18 at 10:33
1
Stephan's solution is the easiest one for command prompt and in a batch file.doskey pwd=echo ^%cd^%
is the right syntax onpwd
definition from within a command prompt window forecho %cd%
. In a batch file must be useddoskey pwd=echo %%cd%%
to define execution ofecho %cd%
on typingpwd
. Please note thatecho %cd%
as alias forpwd
is in general not good in case of current directory contains in path an ampersand because of everything after&
is interpreted bycmd.exe
as additional command to execute. So best is definitelydoskey pwd=cd
.
– Mofi
Nov 20 '18 at 10:46
@Mofi, percent can't actually be escaped in an interactive CMD shell. We escape it in practice by disrupting the name matching, assuming no variable has "^" in its name. Thus in this case we want the "^" character on the right-hand side of the first "%" or the left-hand side of the second "%", or both for good measure. For example, "%^cd^%" has CMD look for a variable named "^cd^", and if none is defined it leaves the literal string "%^cd^%" in place. In the final parsing of the command line, the "^" escape character gets removed, leaving just "%cd%".
– eryksun
Nov 20 '18 at 23:07
@eryksun You are absolutely right and you explained it very good. I knew that already, but I did not want to write it here in a comment to avoid confusing original poster with such deep insight on how Windows command processor parses a line in command prompt window in comparison to parsing a command line in a batch file. However, the difference is explained in detail now by you with your comment. Thank you.
– Mofi
Nov 21 '18 at 9:21
add a comment |
3
doskey pwd=cd
. Thecd
command without parameters will just print the current working folder.
– Stephan
Nov 20 '18 at 10:33
1
Stephan's solution is the easiest one for command prompt and in a batch file.doskey pwd=echo ^%cd^%
is the right syntax onpwd
definition from within a command prompt window forecho %cd%
. In a batch file must be useddoskey pwd=echo %%cd%%
to define execution ofecho %cd%
on typingpwd
. Please note thatecho %cd%
as alias forpwd
is in general not good in case of current directory contains in path an ampersand because of everything after&
is interpreted bycmd.exe
as additional command to execute. So best is definitelydoskey pwd=cd
.
– Mofi
Nov 20 '18 at 10:46
@Mofi, percent can't actually be escaped in an interactive CMD shell. We escape it in practice by disrupting the name matching, assuming no variable has "^" in its name. Thus in this case we want the "^" character on the right-hand side of the first "%" or the left-hand side of the second "%", or both for good measure. For example, "%^cd^%" has CMD look for a variable named "^cd^", and if none is defined it leaves the literal string "%^cd^%" in place. In the final parsing of the command line, the "^" escape character gets removed, leaving just "%cd%".
– eryksun
Nov 20 '18 at 23:07
@eryksun You are absolutely right and you explained it very good. I knew that already, but I did not want to write it here in a comment to avoid confusing original poster with such deep insight on how Windows command processor parses a line in command prompt window in comparison to parsing a command line in a batch file. However, the difference is explained in detail now by you with your comment. Thank you.
– Mofi
Nov 21 '18 at 9:21
3
3
doskey pwd=cd
. The cd
command without parameters will just print the current working folder.– Stephan
Nov 20 '18 at 10:33
doskey pwd=cd
. The cd
command without parameters will just print the current working folder.– Stephan
Nov 20 '18 at 10:33
1
1
Stephan's solution is the easiest one for command prompt and in a batch file.
doskey pwd=echo ^%cd^%
is the right syntax on pwd
definition from within a command prompt window for echo %cd%
. In a batch file must be used doskey pwd=echo %%cd%%
to define execution of echo %cd%
on typing pwd
. Please note that echo %cd%
as alias for pwd
is in general not good in case of current directory contains in path an ampersand because of everything after &
is interpreted by cmd.exe
as additional command to execute. So best is definitely doskey pwd=cd
.– Mofi
Nov 20 '18 at 10:46
Stephan's solution is the easiest one for command prompt and in a batch file.
doskey pwd=echo ^%cd^%
is the right syntax on pwd
definition from within a command prompt window for echo %cd%
. In a batch file must be used doskey pwd=echo %%cd%%
to define execution of echo %cd%
on typing pwd
. Please note that echo %cd%
as alias for pwd
is in general not good in case of current directory contains in path an ampersand because of everything after &
is interpreted by cmd.exe
as additional command to execute. So best is definitely doskey pwd=cd
.– Mofi
Nov 20 '18 at 10:46
@Mofi, percent can't actually be escaped in an interactive CMD shell. We escape it in practice by disrupting the name matching, assuming no variable has "^" in its name. Thus in this case we want the "^" character on the right-hand side of the first "%" or the left-hand side of the second "%", or both for good measure. For example, "%^cd^%" has CMD look for a variable named "^cd^", and if none is defined it leaves the literal string "%^cd^%" in place. In the final parsing of the command line, the "^" escape character gets removed, leaving just "%cd%".
– eryksun
Nov 20 '18 at 23:07
@Mofi, percent can't actually be escaped in an interactive CMD shell. We escape it in practice by disrupting the name matching, assuming no variable has "^" in its name. Thus in this case we want the "^" character on the right-hand side of the first "%" or the left-hand side of the second "%", or both for good measure. For example, "%^cd^%" has CMD look for a variable named "^cd^", and if none is defined it leaves the literal string "%^cd^%" in place. In the final parsing of the command line, the "^" escape character gets removed, leaving just "%cd%".
– eryksun
Nov 20 '18 at 23:07
@eryksun You are absolutely right and you explained it very good. I knew that already, but I did not want to write it here in a comment to avoid confusing original poster with such deep insight on how Windows command processor parses a line in command prompt window in comparison to parsing a command line in a batch file. However, the difference is explained in detail now by you with your comment. Thank you.
– Mofi
Nov 21 '18 at 9:21
@eryksun You are absolutely right and you explained it very good. I knew that already, but I did not want to write it here in a comment to avoid confusing original poster with such deep insight on how Windows command processor parses a line in command prompt window in comparison to parsing a command line in a batch file. However, the difference is explained in detail now by you with your comment. Thank you.
– Mofi
Nov 21 '18 at 9:21
add a comment |
1 Answer
1
active
oldest
votes
The links you provided show how to do it.
I have my doskey macros in a file aliases.txt
in my folder %USERPROFILE%
11:24:16 C:UsersLotPings________________________________________
> type Aliases.txt
~=CD /D "C:UsersLotPings"
=CD
-=CD ..
Alias=Doskey $*
Aliases=Doskey /MACROS:ALL
And an autorun entry which loads this file
> reg query "HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor" /v autorun
HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor
autorun REG_SZ Doskey /MacroFile="C:UsersLotPingsAliases.txt"
To generate this Autorun automatically copy the following lines into cmd line or a batch file
Set "Key=HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor"
Set "Val=Autorun"
Set "Typ=REG_SZ"
Set "Dat=Doskey /Macrofile="%USERPROFILE%Aliases.txt""
reg add "%Key%" /v %Val% /t %Typ% /d "%Dat%" /f
1
Note that the C runtimesystem
function oddly runs commands viacmd /c
without the/d
option that skips the autorun command. It also runs again needlessly for a subshell, which inherits the parent shell's environment and console (i.e. the conhost.exe host process, where doskey aliases are defined and evaluated) and thus usually doesn't need configuration. To avoid this, we can set the autorun command to a script that exits if%CMDCMDLINE%
indicates a non-interactive shell or if it was already run, as indicated by setting an environment variable (e.g.set "CMD_AUTORUN=%0"
).
– eryksun
Nov 20 '18 at 22:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390827%2fpwd-alias-on-windows-cmd%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The links you provided show how to do it.
I have my doskey macros in a file aliases.txt
in my folder %USERPROFILE%
11:24:16 C:UsersLotPings________________________________________
> type Aliases.txt
~=CD /D "C:UsersLotPings"
=CD
-=CD ..
Alias=Doskey $*
Aliases=Doskey /MACROS:ALL
And an autorun entry which loads this file
> reg query "HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor" /v autorun
HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor
autorun REG_SZ Doskey /MacroFile="C:UsersLotPingsAliases.txt"
To generate this Autorun automatically copy the following lines into cmd line or a batch file
Set "Key=HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor"
Set "Val=Autorun"
Set "Typ=REG_SZ"
Set "Dat=Doskey /Macrofile="%USERPROFILE%Aliases.txt""
reg add "%Key%" /v %Val% /t %Typ% /d "%Dat%" /f
1
Note that the C runtimesystem
function oddly runs commands viacmd /c
without the/d
option that skips the autorun command. It also runs again needlessly for a subshell, which inherits the parent shell's environment and console (i.e. the conhost.exe host process, where doskey aliases are defined and evaluated) and thus usually doesn't need configuration. To avoid this, we can set the autorun command to a script that exits if%CMDCMDLINE%
indicates a non-interactive shell or if it was already run, as indicated by setting an environment variable (e.g.set "CMD_AUTORUN=%0"
).
– eryksun
Nov 20 '18 at 22:54
add a comment |
The links you provided show how to do it.
I have my doskey macros in a file aliases.txt
in my folder %USERPROFILE%
11:24:16 C:UsersLotPings________________________________________
> type Aliases.txt
~=CD /D "C:UsersLotPings"
=CD
-=CD ..
Alias=Doskey $*
Aliases=Doskey /MACROS:ALL
And an autorun entry which loads this file
> reg query "HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor" /v autorun
HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor
autorun REG_SZ Doskey /MacroFile="C:UsersLotPingsAliases.txt"
To generate this Autorun automatically copy the following lines into cmd line or a batch file
Set "Key=HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor"
Set "Val=Autorun"
Set "Typ=REG_SZ"
Set "Dat=Doskey /Macrofile="%USERPROFILE%Aliases.txt""
reg add "%Key%" /v %Val% /t %Typ% /d "%Dat%" /f
1
Note that the C runtimesystem
function oddly runs commands viacmd /c
without the/d
option that skips the autorun command. It also runs again needlessly for a subshell, which inherits the parent shell's environment and console (i.e. the conhost.exe host process, where doskey aliases are defined and evaluated) and thus usually doesn't need configuration. To avoid this, we can set the autorun command to a script that exits if%CMDCMDLINE%
indicates a non-interactive shell or if it was already run, as indicated by setting an environment variable (e.g.set "CMD_AUTORUN=%0"
).
– eryksun
Nov 20 '18 at 22:54
add a comment |
The links you provided show how to do it.
I have my doskey macros in a file aliases.txt
in my folder %USERPROFILE%
11:24:16 C:UsersLotPings________________________________________
> type Aliases.txt
~=CD /D "C:UsersLotPings"
=CD
-=CD ..
Alias=Doskey $*
Aliases=Doskey /MACROS:ALL
And an autorun entry which loads this file
> reg query "HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor" /v autorun
HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor
autorun REG_SZ Doskey /MacroFile="C:UsersLotPingsAliases.txt"
To generate this Autorun automatically copy the following lines into cmd line or a batch file
Set "Key=HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor"
Set "Val=Autorun"
Set "Typ=REG_SZ"
Set "Dat=Doskey /Macrofile="%USERPROFILE%Aliases.txt""
reg add "%Key%" /v %Val% /t %Typ% /d "%Dat%" /f
The links you provided show how to do it.
I have my doskey macros in a file aliases.txt
in my folder %USERPROFILE%
11:24:16 C:UsersLotPings________________________________________
> type Aliases.txt
~=CD /D "C:UsersLotPings"
=CD
-=CD ..
Alias=Doskey $*
Aliases=Doskey /MACROS:ALL
And an autorun entry which loads this file
> reg query "HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor" /v autorun
HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor
autorun REG_SZ Doskey /MacroFile="C:UsersLotPingsAliases.txt"
To generate this Autorun automatically copy the following lines into cmd line or a batch file
Set "Key=HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor"
Set "Val=Autorun"
Set "Typ=REG_SZ"
Set "Dat=Doskey /Macrofile="%USERPROFILE%Aliases.txt""
reg add "%Key%" /v %Val% /t %Typ% /d "%Dat%" /f
answered Nov 20 '18 at 10:43
LotPingsLotPings
19.1k61532
19.1k61532
1
Note that the C runtimesystem
function oddly runs commands viacmd /c
without the/d
option that skips the autorun command. It also runs again needlessly for a subshell, which inherits the parent shell's environment and console (i.e. the conhost.exe host process, where doskey aliases are defined and evaluated) and thus usually doesn't need configuration. To avoid this, we can set the autorun command to a script that exits if%CMDCMDLINE%
indicates a non-interactive shell or if it was already run, as indicated by setting an environment variable (e.g.set "CMD_AUTORUN=%0"
).
– eryksun
Nov 20 '18 at 22:54
add a comment |
1
Note that the C runtimesystem
function oddly runs commands viacmd /c
without the/d
option that skips the autorun command. It also runs again needlessly for a subshell, which inherits the parent shell's environment and console (i.e. the conhost.exe host process, where doskey aliases are defined and evaluated) and thus usually doesn't need configuration. To avoid this, we can set the autorun command to a script that exits if%CMDCMDLINE%
indicates a non-interactive shell or if it was already run, as indicated by setting an environment variable (e.g.set "CMD_AUTORUN=%0"
).
– eryksun
Nov 20 '18 at 22:54
1
1
Note that the C runtime
system
function oddly runs commands via cmd /c
without the /d
option that skips the autorun command. It also runs again needlessly for a subshell, which inherits the parent shell's environment and console (i.e. the conhost.exe host process, where doskey aliases are defined and evaluated) and thus usually doesn't need configuration. To avoid this, we can set the autorun command to a script that exits if %CMDCMDLINE%
indicates a non-interactive shell or if it was already run, as indicated by setting an environment variable (e.g. set "CMD_AUTORUN=%0"
).– eryksun
Nov 20 '18 at 22:54
Note that the C runtime
system
function oddly runs commands via cmd /c
without the /d
option that skips the autorun command. It also runs again needlessly for a subshell, which inherits the parent shell's environment and console (i.e. the conhost.exe host process, where doskey aliases are defined and evaluated) and thus usually doesn't need configuration. To avoid this, we can set the autorun command to a script that exits if %CMDCMDLINE%
indicates a non-interactive shell or if it was already run, as indicated by setting an environment variable (e.g. set "CMD_AUTORUN=%0"
).– eryksun
Nov 20 '18 at 22:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390827%2fpwd-alias-on-windows-cmd%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
doskey pwd=cd
. Thecd
command without parameters will just print the current working folder.– Stephan
Nov 20 '18 at 10:33
1
Stephan's solution is the easiest one for command prompt and in a batch file.
doskey pwd=echo ^%cd^%
is the right syntax onpwd
definition from within a command prompt window forecho %cd%
. In a batch file must be useddoskey pwd=echo %%cd%%
to define execution ofecho %cd%
on typingpwd
. Please note thatecho %cd%
as alias forpwd
is in general not good in case of current directory contains in path an ampersand because of everything after&
is interpreted bycmd.exe
as additional command to execute. So best is definitelydoskey pwd=cd
.– Mofi
Nov 20 '18 at 10:46
@Mofi, percent can't actually be escaped in an interactive CMD shell. We escape it in practice by disrupting the name matching, assuming no variable has "^" in its name. Thus in this case we want the "^" character on the right-hand side of the first "%" or the left-hand side of the second "%", or both for good measure. For example, "%^cd^%" has CMD look for a variable named "^cd^", and if none is defined it leaves the literal string "%^cd^%" in place. In the final parsing of the command line, the "^" escape character gets removed, leaving just "%cd%".
– eryksun
Nov 20 '18 at 23:07
@eryksun You are absolutely right and you explained it very good. I knew that already, but I did not want to write it here in a comment to avoid confusing original poster with such deep insight on how Windows command processor parses a line in command prompt window in comparison to parsing a command line in a batch file. However, the difference is explained in detail now by you with your comment. Thank you.
– Mofi
Nov 21 '18 at 9:21