How to define a DOSKEY alias for multiple commands?
Asked Answered
D

4

16

Following on from an answer to this question

For the Windows command prompt I can define aliases as follows:

@echo off
DOSKEY ns=npm start
DOSKEY nsr=npm run serve

I want to define an alias that will combine these two commands. I tried the following:

@echo off
DOSKEY nss=npm start && npm run serve

When I try to open the command prompt the window will open but the > prompt does not appear.

I think that the inclusion of && is causing a problem.

Deputize answered 27/11, 2017 at 9:6 Comment(2)
See Also: Aliases in Windows command promptBaggage
see alse superuser.com/a/1349294/965349Hawkweed
D
18

I looked at an answer to a question on superuser. The following approach resolved my problem:

@echo off
DOSKEY nss=npm start ^&^& npm run serve
Deputize answered 28/11, 2017 at 9:47 Comment(2)
I hope you realize that this is just a substitution rule in the console via AddConsoleAlias. It does the substitution in the input buffer, before a console client program (e.g. cmd.exe) even reads a line of input. These aliases only work at the start of a line entered in the console (i.e. you can't pipe to them) and not in batch scripts. For doskey.exe, you can use $T$T in place of &&, which is more flexible then using ^ to escape the & operator in the command line.Ultramontanism
Thanks for the information. I'm not aware of any need to pipe to these commands at the moment.Deputize
M
34

The command separator for DOSKEY is $T

For your example:

DOSKEY nss=npm start $T npm run serve
Montespan answered 27/11, 2017 at 19:19 Comment(3)
Thanks for answering. I tried this and the > prompt would appear. I encountered a different problem after that. If the first command failed for some reason then the second command would still execute. I opted for a different approach which I will post.Deputize
Add another $T to form &&, and this will be the best answer.Ultramontanism
DOSKEY nss=npm start $T$T npm run serve - works perfectly! From microsoft docs using $T is preferred method. doskeys via MicrosoftEductive
D
18

I looked at an answer to a question on superuser. The following approach resolved my problem:

@echo off
DOSKEY nss=npm start ^&^& npm run serve
Deputize answered 28/11, 2017 at 9:47 Comment(2)
I hope you realize that this is just a substitution rule in the console via AddConsoleAlias. It does the substitution in the input buffer, before a console client program (e.g. cmd.exe) even reads a line of input. These aliases only work at the start of a line entered in the console (i.e. you can't pipe to them) and not in batch scripts. For doskey.exe, you can use $T$T in place of &&, which is more flexible then using ^ to escape the & operator in the command line.Ultramontanism
Thanks for the information. I'm not aware of any need to pipe to these commands at the moment.Deputize
C
4

My approach is to load the macros from a text file:

a.bat:
    @doskey /macrofile=C:\%HOMEPATH%\bin\aliases.txt

and in the macro file, && works:

aliases.txt:
    cl=cd /d $* && dir

Then if I type "cl bin" from HOMEPATH, it does "cd /d bin" and then "dir":

C:\Users\mike> cl bin
09/17/2020  09:27 AM             1,303 a.bat
09/30/2020  03:17 PM               886 aliases.txt
Cuticula answered 30/9, 2020 at 22:25 Comment(0)
P
2

Try writing it as a batch file and then calling the batch file use the DOSKEY command

REM do_npm.bat
npm start
npm run serve

Then, run the DOSKEY command

DOSKEY npm=do_npm.bat

See if that works for you

Panay answered 27/11, 2017 at 16:48 Comment(1)
Thanks for answering. I've opted for a different approach though as I want to avoid additional files.Deputize

© 2022 - 2024 — McMap. All rights reserved.