Windows 命令提示符中的别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20530996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Aliases in Windows command prompt
提问by Romonov
I have added notepad++.exe
to my Path in Environment variables.
我已notepad++.exe
在环境变量中添加到我的路径中。
Now in command prompt, notepad++.exe filename.txt
opens the filename.txt
. But I want to do just np filename.txt
to open the file.
现在在命令提示符下,notepad++.exe filename.txt
打开filename.txt
. 但我只想np filename.txt
打开文件。
I tried using DOSKEY np=notepad++
. But it is just bringing to the forefront an already opened notepad++ without opening the file. How can I make it open the file?
我尝试使用DOSKEY np=notepad++
. 但它只是将已经打开的记事本++带到最前沿,而无需打开文件。我怎样才能让它打开文件?
Thanks.
谢谢。
回答by Argyll
To add to josh's answer,
添加到乔希的答案,
you may make the alias(es) persistentwith the following steps,
您可以通过以下步骤使别名持久化,
- Create a .bat or .cmd file with your
DOSKEY
commands. - Run regedit and go to
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Add String Value entry with the name
AutoRun
and the fullpath of your .bat/.cmd file.For example,
%USERPROFILE%\alias.cmd
, replacing the initial segment of the path with%USERPROFILE%
is useful for syncing among multiple machines.
- 使用您的
DOSKEY
命令创建 .bat 或 .cmd 文件。 - 运行 regedit 并转到
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
添加带有.bat/.cmd 文件的名称
AutoRun
和完整路径的字符串值条目。例如,
%USERPROFILE%\alias.cmd
用 替换路径的初始段%USERPROFILE%
对于多台机器之间的同步很有用。
This way, every time cmd is run, the aliases are loaded.
这样,每次运行 cmd 时,都会加载别名。
For Windows 10, add the entry to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
instead.
对于 Windows 10,请HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
改为添加条目。
For completeness, here is a template to illustrate the kind of aliases one may find useful.
为了完整起见,这里有一个模板来说明可能有用的别名类型。
@echo off
:: Temporary system path at cmd startup
set PATH=%PATH%;"C:\Program Files\Sublime Text 2\"
:: Add to path by command
DOSKEY add_python26=set PATH=%PATH%;"C:\Python26\"
DOSKEY add_python33=set PATH=%PATH%;"C:\Python33\"
:: Commands
DOSKEY ls=dir /B
DOSKEY sublime=sublime_text $*
::sublime_text.exe is name of the executable. By adding a temporary entry to system path, we don't have to write the whole directory anymore.
DOSKEY gsp="C:\Program Files (x86)\Sketchpad5\GSP505en.exe"
DOSKEY alias=notepad %USERPROFILE%\Dropbox\alias.cmd
:: Common directories
DOSKEY dropbox=cd "%USERPROFILE%\Dropbox$*"
DOSKEY research=cd %USERPROFILE%\Dropbox\Research\
- Note that the
$*
syntax works after a directory string as well as an executable which takes in arguments. So in the above example, the user-defined commanddropbox research
points to the same directory asresearch
. - As Rivenfall pointed out, it is a good idea to include a command that allows for convenient editing of the
alias.cmd
file. Seealias
above. If you are in a cmd session, entercmd
to restart cmd and reload thealias.cmd
file.
- 请注意,该
$*
语法在目录字符串以及接受参数的可执行文件之后起作用。所以在上面的例子中,用户定义的命令dropbox research
指向与research
. - 正如 Rivenfall 指出的那样,包含一个允许方便地编辑
alias.cmd
文件的命令是一个好主意。见alias
上文。如果您在 cmd 会话中,请输入cmd
以重新启动 cmd 并重新加载alias.cmd
文件。
When I searched the internet for an answer to the question, somehow the discussions were either focused on persistence only or on some usage of DOSKEY only. I hope someone will benefit from these two aspects being together here!
当我在互联网上搜索问题的答案时,不知何故,讨论要么只关注持久性,要么只关注 DOSKEY 的一些用法。我希望有人能从这两个方面一起在这里受益!
Here's a .reg
file to help you install the alias.cmd
. It's set now as an example to a dropbox folder as suggested above.
这是一个.reg
帮助您安装alias.cmd
. 它现在被设置为上面建议的 dropbox 文件夹的示例。
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="%USERPROFILE%\alias.cmd"
For single-user applications, the above will do. Nevertheless, there are situations where it is necessary to check whether alias.cmd
exists first in the registry key. See example below.
对于单用户应用程序,以上都可以。然而,有些情况需要首先检查alias.cmd
注册表项中是否存在。请参见下面的示例。
In a C:\Users\Public\init.cmd
file hosting potentially cross-user configurations:
在C:\Users\Public\init.cmd
托管潜在跨用户配置的文件中:
@ECHO OFF
REM Add other configurations as needed
IF EXIST "%USERPROFILE%\alias.cmd" ( CALL "%USERPROFILE%\alias.cmd" )
The registry key should be updated correspondly to C:\Users\Public\init.cmd
or, using the .reg
file:
注册表项应相应地更新为C:\Users\Public\init.cmd
或使用以下.reg
文件:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="C:\Users\Public\init.cmd"
回答by josh poley
You need to pass the parameters, try this:
你需要传递参数,试试这个:
doskey np=notepad++.exe $*
Edit(responding to Romonov's comment) Q: Is there any way I can make the command prompt remember so I don't have to run this each time I open a new command prompt?
编辑(响应 Romonov 的评论) 问:有什么方法可以让我记住命令提示符,这样我就不必每次打开新命令提示符时都运行它?
doskey
is a textual command that is interpreted by the command processor (e.g. cmd.exe), it can't know to modify state in some other process (especially one that hasn't started yet).
doskey
是由命令处理器(例如cmd.exe)解释的文本命令,它不知道在其他某个进程(尤其是尚未启动的进程)中修改状态。
People that use doskey
to setup their initial command shell environments typically use the /K
option (often via a shortcut) to run a batch file which does all the common setup (like- set window's title, colors, etc).
人们在使用doskey
设置初始命令shell环境中通常使用的/K
选项(通常是通过一个快捷键)来运行一个批处理文件,它完成了所有的常用的设置(喜欢-集窗口的标题,颜色等)。
cmd.exe /K env.cmd
env.cmd:
环境.cmd:
title "Foo Bar"
doskey np=notepad++.exe $*
...
回答by roryhewitt
If you're just going for some simple commands, you could follow these steps:
如果你只是想要一些简单的命令,你可以按照以下步骤操作:
- Create a folder called C:\Aliases
- Add C:\Aliasesto your path (so any files in it will be found every time)
- Create a .batfile in C:\Aliases for each of the aliases you want
- 创建一个名为C:\Aliases的文件夹
- 将C:\Aliases添加到您的路径中(因此每次都会找到其中的任何文件)
- 在 C:\Aliases 中为您想要的每个别名创建一个.bat文件
Maybe overkill, but unlike the (otherwise excellent) answer from @Argyll, this solves the problem of this loading every time.
也许矫枉过正,但与@Argyll 的(其他方面非常出色)答案不同,这解决了每次加载的问题。
For instance, I have a file called dig2.batwith the following in it:
例如,我有一个名为dig2.bat的文件,其中包含以下内容:
@echo off
echo.
dig +noall +answer %1
Your npfile would just have the following:
您的np文件将仅包含以下内容:
@echo off
echo.
notepad++.exe %1
Then just add the C:\Aliasesfolder to your PATH environment variable. If you have CMD or PowerShell already opened you will need to restart it.
然后只需将C:\Aliases文件夹添加到您的 PATH 环境变量中。如果您已经打开了 CMD 或 PowerShell,则需要重新启动它。
FWIW, I have about 20 aliases (separate .bat files) in my C:\Aliases directory - I just create new ones as necessary. Maybe not the neatest, but it works fine.
FWIW,我的 C:\Aliases 目录中有大约 20 个别名(单独的 .bat 文件) - 我只是根据需要创建新的。也许不是最整洁的,但它工作正常。
UPDATE: Per an excellent suggestion from user @Mav, it's even better to use %*rather than %1, so you can pass multiple files to the command, e.g.:
更新:根据用户@Mav 的一个很好的建议,使用%*而不是%1更好,因此您可以将多个文件传递给命令,例如:
@echo off
echo.
notepad++.exe %*
That way, you could do this:
这样,你可以这样做:
np c:\temp\abc.txt c:\temp\def.txt c:\temp\ghi.txt
and it will open all 3 files.
它将打开所有 3 个文件。
回答by hthserhs
回答by Velixo
Given that you added notepad++.exe to your PATH variable, it's extra simple.
Create a file in your System32 folder called np.bat
with the following code:
鉴于您将 notepad++.exe 添加到您的 PATH 变量中,这非常简单。在 System32 文件夹中创建一个文件,np.bat
使用以下代码调用:
@echo off
call notepad++.exe %*
The %*
passes along all arguments you give the np
command to the notepad++.exe
command.
该%*
通行证沿所有参数你给的np
命令的notepad++.exe
命令。
EDIT:You will need admin access to save files to the System32 folder, which was a bit wonky for me. I just created the file somewhere else and moved it to System32 manually.
编辑:您需要管理员权限才能将文件保存到 System32 文件夹,这对我来说有点奇怪。我只是在其他地方创建了文件并手动将其移动到 System32。
回答by Николай Михно
Also, you can create an alias.cmd in your path (for example C:\Windows) with the command
此外,您可以使用以下命令在您的路径(例如 C:\Windows)中创建一个 alias.cmd
@echo %2 %3 %4 %5 %6 > %windir%\%1.cmd
Once you do that, you can do something like this:
一旦你这样做了,你可以做这样的事情:
alias nameOfYourAlias commands to run
And after that you can type in comman line
之后你可以输入命令行
nameOfYourAlias
this will execute
这将执行
commands to run
BUT the best way for me is just adding the path of a programm.
但对我来说最好的方法就是添加程序的路径。
setx PATH "%PATH%;%ProgramFiles%\Sublime Text 3" /M
And now I run sublime as
现在我以崇高的身份运行
subl index.html
回答by Qwerty
Console Aliases in Windows 10
Windows 10 中的控制台别名
To define a console alias, use Doskey.exe
to create a macro, or use the AddConsoleAlias
function.
要定义控制台别名,请用于Doskey.exe
创建宏,或使用该AddConsoleAlias
函数。
doskey
剂量键
doskey test=cd \a_very_long_path\test
To also pass parametersadd $*
at the end: doskey short=longname $*
要传递参数$*
,请在末尾添加:doskey short=longname $*
AddConsoleAlias
添加控制台别名
AddConsoleAlias( TEXT("test"),
TEXT("cd \<a_very_long_path>\test"),
TEXT("cmd.exe"));
More information here Console Aliases, Doskey, Parameters
回答by Steaton
You want to create an alias by simply typing:
您想通过简单地输入以下内容来创建别名:
c:\>alias kgs kubectl get svc
Created alias for kgs=kubectl get svc
And use the alias as follows:
并使用别名如下:
c:\>kgs alfresco-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
alfresco-svc ClusterIP 10.7.249.219 <none> 80/TCP 8d
Just add the following alias.bat
file to you path. It simply creates additional batch files in the same directory as itself.
只需将以下alias.bat
文件添加到您的路径即可。它只是在与自身相同的目录中创建额外的批处理文件。
@echo off
echo.
for /f "tokens=1,* delims= " %%a in ("%*") do set ALL_BUT_FIRST=%%b
echo @echo off > C:\Development\alias-script\%1.bat
echo echo. >> C:\Development\alias-script\%1.bat
echo %ALL_BUT_FIRST% %%* >> C:\Development\alias-script\%1.bat
echo Created alias for %1=%ALL_BUT_FIRST%
An example of the batch file this created called kgs.bat
is:
这个创建的批处理文件的一个例子kgs.bat
是:
@echo off
echo.
kubectl get svc %*
回答by David
Actually, I'll go you one better and let you in on a little technique that I've used since I used to program on an Amiga. On anynew system you use, be it personal or professional, step one is to create two folders: C:\BIN
and C:\BATCH
. Then modify your path statement to put both at the start in the order C:\BATCH;C:\BIN;[rest of path]
.
实际上,我会给你一个更好的方法,让你了解我在 Amiga 上编程以来一直使用的一个小技巧。在您使用的任何新系统上,无论是个人系统还是专业系统,第一步是创建两个文件夹: C:\BIN
和C:\BATCH
. 然后修改您的路径语句,将两者都放在 order 的开头C:\BATCH;C:\BIN;[rest of path]
。
Having done that, if you have little out-of-the-way utilities that you need access to simply copy them to the C:\BIN
folder and they're in your path. To temporarily override these assignments, you can add a batch file with the same name as the executable to the C:\BATCH
folder and the path will find it before the file in C:\BIN. It should cover anything you might ever need to do.
完成后,如果您几乎没有需要访问的偏僻实用程序,只需将它们复制到C:\BIN
文件夹中,并且它们就在您的路径中。要临时覆盖这些分配,您可以将与可执行文件同名的批处理文件添加到C:\BATCH
文件夹中,路径将在 C:\BIN 中的文件之前找到它。它应该涵盖您可能需要做的任何事情。
Of course, these days the canonical correct way to do this would be to create a symbolic junction to the file, but the same principle applies. There is a little extra added bonus as well. If you want to put something in the system that conflicts with something already in the path, putting it in the C:\BIN
or C:\Batch
folder will simply pre-emptthe original - allowing you to override stuff either temporarily or permanently, or rename things to names you're more comfortable with - without actually altering the original.
当然,如今执行此操作的规范正确方法是创建到文件的符号连接,但同样的原则也适用。还有一些额外的额外奖励。如果你想要把东西在系统中把它在与路径的东西已经发生冲突,C:\BIN
或C:\Batch
文件夹就干脆抢先原-让你忽略的东西暂时或永久或重命名的东西的名字你更舒适 - 无需实际更改原件。
回答by Alex Perry
Expanding on roryhewittanswer.
扩展roryhewitt 的答案。
An advantage to using .cmd files over DOSKEY is that these "aliases" are then available in other shells such as PowerShell or WSL (Windows subsystem for Linux).
使用 .cmd 文件而不是 DOSKEY 的一个优势是,这些“别名”可以在其他 shell 中使用,例如 PowerShell 或 WSL(Linux 的 Windows 子系统)。
The only gotcha with using these commands in bash is that it may take a bit more setup since you might need to do some path manipulation before calling your "alias".
在 bash 中使用这些命令的唯一问题是它可能需要更多的设置,因为您可能需要在调用“别名”之前进行一些路径操作。
eg I have vs.cmd which is my "alias" for editing a file in Visual Studio
例如我有 vs.cmd 这是我在 Visual Studio 中编辑文件的“别名”
@echo off
if [%1]==[] goto nofiles
start "" "c:\Program Files (x86)\Microsoft Visual Studio
11.0\Common7\IDE\devenv.exe" /edit %1
goto end
:nofiles
start "" "C:\Program Files (x86)\Microsoft Visual Studio
11.0\Common7\IDE\devenv.exe" "[PATH TO MY NORMAL SLN]"
:end
Which fires up VS (in this case VS2012 - but adjust to taste) using my "normal" project with no file given but when given a file will attempt to attach to a running VS opening that file "within that project" rather than starting a new instance of VS.
使用我的“正常”项目启动 VS(在这种情况下是 VS2012 - 但根据口味进行调整),但没有给出文件,但是当给定文件时,文件将尝试附加到正在运行的 VS,在“该项目中”打开该文件,而不是启动一个VS 的新实例。
For using this from bash I then add an extra level of indirection since "vs Myfile" wouldn't always work
为了从 bash 使用它,我添加了一个额外的间接级别,因为“vs Myfile”并不总是有效
alias vs='/usr/bin/run_visual_studio.sh'
Which adjusts the paths before calling the vs.cmd
在调用 vs.cmd 之前调整路径
#!/bin/bash
cmd.exe /C 'c:\Windows\System32\vs.cmd' "`wslpath.sh -w -r `"
So this way I can just do
所以这样我就可以做
vs SomeFile.txt
In either a command prompt, Power Shell or bash and it opens in my running Visual Studio for editing (which just saves my poor brain from having to deal with VI commands or some such when I've just been editing in VS for hours).
在命令提示符、Power Shell 或 bash 中,它会在我正在运行的 Visual Studio 中打开以进行编辑(当我刚刚在 VS 中编辑数小时时,这只是使我可怜的大脑不必处理 VI 命令或其他类似命令)。