While at work, I want to use the CLI to be more productive on my Windows machine. What command, in Windows, would be a replacement for the alias command?
2 Answers
doskey com=a long command $*
Here $* expands to everything typed after com. For example, com Hi! would be expanded to a long command Hi!
- This not limited to the
cmd.exeshell – it works with any program that uses a Win32 console window. (For example, for Python's interactive shell, usedoskey /exefile:python.exe ...)
See doskey /? for usage. The aliases can also be added programatically; see Console Aliases for the API.
To apply aliases automatically whenever cmd.exe is launched:
Put them in a text file, in the form
alias=expansion:com=very long example command cd=cd /d $*I keep my aliases in
%AppData%\doskey.txt.Create a batch script containing the
doskeycommand:@echo off doskey /macrofile:"%AppData%\doskey.txt"Of course, point
/macrofileto the location you have chosen in step 1.A good name for this script is
%AppData%\autorun.cmd.In Registry, open key
HKEY_CURRENT_USER\Software\Microsoft\Command Processorand point the valueAutoRunto the script.- Run
regedit, navigate to the given key. - If a value named
AutoRundoes not exist, create it: right-click → New → String - Modify the associated data to point to wherever your autorun script from #2 is located.
- Run
- 452,512
I assume you mean the "alias" command. Dos/NT command prompts support "alias" as a command with the following syntax:
alias
See the wikipedia article for details: http://en.wikipedia.org/wiki/Alias_(command)
- 41,771
-
Doesn't look like it, I just tried it. Win XP. But that wikipedia article does mention powershell and 4DOS and 4NT having a command for aliases but maybe not that syntax. – barlop Apr 11 '11 at 20:05
-
What about using Set?
I just tested in XP, set ckh = "George" and it set an environment variable ckd as "George". Is this an analog to alias?
– music2myear Apr 11 '11 at 20:25 -
@music2myyear No. An alias is like a substitution. So typing C:>doskey tt=blah
that sets up an alias tt=blah, means when you type tt and press ENTER, then it acts like you typed blah and pressed enter. So it's like a shortcut. With set, if you do set ckh=George Then ckh ENTER will still be ckh ENTER. But %ckh% will use the contents of the ckh environemnt variable i.e. George. The %s are not so convenient as with an alias, – barlop Apr 11 '11 at 23:31 -
@music2myyear but it's a very different concept really. A variable is like a container with a name and a value and is meant to change at times you want. An alias is like a shorthand way of saying something, it's more static.. if you kept changing your aliases you'd get confused.. 'cos they're meant to be easily memorable shorthands for specific commands with certain options you don't want to have to retype. – barlop Apr 11 '11 at 23:33
-
Got it and thank you for the explanation. I've never much gotten into programming or scripting or admin-ing on *nix systems and so was (obviously) not familiar with the concept. But now I'm a little more so thanks to y'all. – music2myear Apr 12 '11 at 02:45
autorun.cmd, I get anInvalid macro definitionerror (I turned@echo onto see the output). are there any suggestions for what might cause this? – ewok May 15 '12 at 15:22doskey /macrofile=.... note the=instead of the:– ewok May 15 '12 at 15:26doskey /macros:all > "%AppData%\doskey.txt". This file has sections per EXE name, such as[cmd.exe]. – Eryk Sun Dec 23 '14 at 00:32