本文翻译自:Aliases in Windows command prompt
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
. 现在在命令提示符下, notepad++.exe filename.txt
将打开filename.txt
。 But I want to do just np filename.txt
to open the file. 但是我只想执行np filename.txt
来打开文件。
I tried using DOSKEY np=notepad++
. 我尝试使用DOSKEY np=notepad++
。 But it is just bringing to the forefront an already opened notepad++ without opening the file. 但这只是在不打开文件的情况下将已经打开的notepad ++带到了前台。 How can I make it open the file? 如何使其打开文件?
Thanks. 谢谢。
#1楼
参考:https://stackoom.com/question/1O936/Windows命令提示符中的别名
#2楼
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 (eg 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
设置其初始命令外壳环境的人员通常使用/K
选项(通常通过快捷方式)运行一个批处理文件,该文件执行所有常见的设置(如设置窗口的标题,颜色等)。
cmd.exe /K env.cmd
env.cmd: env.cmd:
title "Foo Bar"
doskey np=notepad++.exe $*
...
#3楼
To add to josh's answer, 为了增加乔什的答案,
you may make the alias(es) persistent with the following steps, 您可以通过以下步骤使别名持久化 ,
- Create a .bat or .cmd file with your
DOSKEY
commands. 使用DOSKEY
命令创建.bat或.cmd文件。 - Run regedit and go to
HKEY_CURRENT_USER\\Software\\Microsoft\\Command Processor
运行regedit并转到HKEY_CURRENT_USER\\Software\\Microsoft\\Command Processor
Add String Value entry with the name
AutoRun
and the full path of your .bat/.cmd file. 添加名称为AutoRun
和.bat / .cmd文件的完整路径的字符串值条目。For example,
%USERPROFILE%\\alias.cmd
, replacing the initial segment of the path with%USERPROFILE%
is useful for syncing among multiple machines. 例如,%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
. 因此,在上述例子中,用户定义的命令dropbox research
指向相同的目录中research
。 - As Rivenfall pointed out, it is a good idea to include a command that allows for convenient editing of the
alias.cmd
file. 正如Rivenfall指出的那样,最好包含一个允许方便地编辑alias.cmd
文件的命令。 Seealias
above. 请参阅上面的alias
。 If you are in a cmd session, entercmd
to restart cmd and reload thealias.cmd
file. 如果您处于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. 当我在互联网上搜索该问题的答案时,某种程度上,讨论要么集中在持久性上,要么仅集中在DOSKEY的使用上。 I hope someone will benefit from these two aspects being together here! 我希望有人将从这两个方面中受益!
Here's a .reg
file to help you install the alias.cmd
. 这是一个.reg
文件,可以帮助您安装alias.cmd
。 It's set now as an example to a dropbox folder as suggested above. 现在将其设置为上述建议的保管箱文件夹的示例。
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="%USERPROFILE%\\alias.cmd"
#4楼
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
#5楼
Given that you added notepad++.exe to your PATH variable, it's extra simple. 鉴于您已将notepad ++。exe添加到PATH变量中,因此非常简单。 Create a file in your System32 folder called np.bat
with the following code: 使用以下代码在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. 编辑:您将需要管理员访问权限才能将文件保存到System32文件夹,这对我来说有点不可思议。 I just created the file somewhere else and moved it to System32 manually. 我只是在其他地方创建了文件,然后将其手动移至System32。
#6楼
If you're just going for some simple commands, you could follow these steps: 如果您只需要一些简单的命令,则可以按照以下步骤操作:
- Create a folder called C:\\Aliases 创建一个名为C:\\ Aliases的文件夹
- Add C:\\Aliases to your path (so any files in it will be found every time) 将C:\\ Aliases添加到您的路径(这样每次都会在其中找到任何文件)
- Create a .bat file in C:\\Aliases for each of the aliases you want 在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.bat with the following in it: 例如,我有一个名为dig2.bat的文件, 其中包含以下内容:
@echo off
echo.
dig +noall +answer %1
Your np file would just have the following: 您的np文件将包含以下内容:
@echo off
echo.
notepad++.exe %1
Then just add the C:\\Aliases folder to your PATH environment variable. 然后只需将C:\\ Aliases文件夹添加到PATH环境变量中即可。 If you have CMD or PowerShell already opened you will need to restart it. 如果您已经打开了CMD或PowerShell,则需要重新启动它。
FWIW, I have about 20 aliases (separate .bat files) in my C:\\Aliases directory - I just create new ones as necessary. FWIW,我的C:\\ Aliases目录中大约有20个别名(单独的.bat文件)-我只是根据需要创建新的别名。 Maybe not the neatest, but it works fine. 也许不是最整洁的方法,但是效果很好。
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, eg: 更新 :根据用户@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个文件。