本文翻译自: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, 您可以通过以下步骤使别名持久化

  1. Create a .bat or .cmd file with your DOSKEY commands. 使用DOSKEY命令创建.bat或.cmd文件。
  2. Run regedit and go to HKEY_CURRENT_USER\\Software\\Microsoft\\Command Processor 运行regedit并转到HKEY_CURRENT_USER\\Software\\Microsoft\\Command Processor
  3. 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 startupset PATH=%PATH%;"C:\Program Files\Sublime Text 2\":: Add to path by commandDOSKEY add_python26=set PATH=%PATH%;"C:\Python26\"
DOSKEY add_python33=set PATH=%PATH%;"C:\Python33\":: CommandsDOSKEY 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 directoriesDOSKEY 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 command dropbox research points to the same directory as research . 因此,在上述例子中,用户定义的命令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文件的命令。 See alias above. 请参阅上面的alias If you are in a cmd session, enter cmd to restart cmd and reload the alias.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: 如果您只需要一些简单的命令,则可以按照以下步骤操作:

  1. Create a folder called C:\\Aliases 创建一个名为C:\\ Aliases的文件夹
  2. Add C:\\Aliases to your path (so any files in it will be found every time) C:\\ Aliases添加到您的路径(这样每次都会在其中找到任何文件)
  3. 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个文件。

Windows命令提示符中的别名相关推荐

  1. 在命令提示符输出c语言代码_您可以在Windows命令提示符中更改输出缓冲区的大小吗?...

    在命令提示符输出c语言代码 If you are someone who loves using the Windows Command Prompt, you may have found your ...

  2. windows命令提示符_如何在Windows命令提示符中使用命令历史记录

    windows命令提示符 The Windows Command Prompt has a built-in history feature, allowing you to quickly view ...

  3. 关于Windows命令提示符中的 xxx > nul 2 > nul

    写博客就像写作文,是要素材哒~ 大家在运行程序的时候,有时候需要让他输出到文件(控制台输出),又不能改程序(别人给你的编译好的程序),就可以利用重定向>符号: rem aaa.exe是要运行的程 ...

  4. c语言 输出定向到文件 命令提示符窗口也能看见,关于cmd:显示Windows命令提示符输出并将其重定向到文件...

    如何在Windows命令提示符下运行命令行应用程序并同时显示输出并重定向到文件? 例如,如果我要运行命令dir > test.txt,则会将输出重定向到名为test.txt的文件而不显示结果. ...

  5. 【Windows】中DOMAIN域

    网上扒到整理的,如有错误请留言指出,谢谢. 前导 Windows命令提示符中,命令的用法后面常见有一个**[/DOMAIN]**,例如: D:\dos>net user /? 此命令的语法是:N ...

  6. Microsoft Windows 环境中NLS_LANG的正确设置 (文档 ID 1577370.1)

    Microsoft Windows 环境中NLS_LANG的正确设置 (文档 ID 1577370.1) 适用于: Oracle Database - Enterprise Edition - 版本 ...

  7. 如何在Python脚本中调用外部命令(就像在linux shell或Windows命令提示符下输入一样)

    如何在Python脚本中调用外部命令(就像在linux shell或Windows命令提示符下输入一样) python标准库中的subprocess可以解决这个问题. from subprocess ...

  8. 在 Windows 10 中通过命令提示符或控制面板启用 Telnet

    在 Windows 10/8/7 上,默认情况下禁用Telnet 客户端和服务器.如果要启用它,可以使用命令提示符或通过控制面板来实现.供您参考,Telnet(tel etype net work 的 ...

  9. 如何在命令提示符中备份Windows设备驱动程序

    如果清理安装Windows,则需要为系统中的每个设备安装驱动程序.制造商可能不再提供其中一些设备驱动程序,或者你放错了制造商的驱动程序安装文件备份. 在进行干净安装之前备份设备驱动程序是个好主意,这样 ...

最新文章

  1. linux下inotify的使用
  2. 初步学习用超图处理三维场景
  3. python怎么更新数据库_在Python的Django框架中更新数据库数据的方法
  4. html用jq设置动态效果,jQuery实现基本动画效果的方法详解
  5. HTTP header中的 Cache-control
  6. 渠道下沉 阿里争食社区经济最后一公里
  7. 小程序仿微视_争抢流量!腾讯微视也要开始搞短视频带货
  8. Go开发关键技术指南
  9. php转繁体代码,php实现简体转繁体的方法
  10. 【前端】一步一步使用webpack+react+scss脚手架重构项目
  11. Android Studio连接海马玩模拟器
  12. WAS6.1JNDI数据源配置测试代码
  13. 网络中超难的75道逻辑题及答案
  14. 二进制及二进制的转换
  15. 【线性分类器】(四)万字长文解释拉格朗日乘子与支持向量机
  16. Canny边缘检测方法中的非极大抑制
  17. 玩转「Wi-Fi」系列之测试工具(三)
  18. [LeetCode]Buy and Sell Stocks 买卖股票问题
  19. [玩转UE4/UE5动画系统>Control Rig篇] 之 使用Control Rig实现目标偏移(Aim Offset)(附项目代码)
  20. 计算机网络(自顶向下方法)-Internet概述

热门文章

  1. R-Sys.time计算程序运行时间
  2. windows 批处理把所有java源码导入一个txt文件中
  3. kettle job如何利用java的反射机制获取执行的sql语句
  4. 把JS和CSS合并到1个文件
  5. 理解asp.net中DropDownList编辑数据源,绑定数据库数据。
  6. input type = submit 提交方式和用js的form.submit()有什么区别?
  7. SpringBoot启动时 提示没有主清单属性 MANIFEST
  8. 1e9个兵临城下(容斥原理)
  9. ubuntu系列-很好用的截图工具shutter
  10. ASP.NET 文件操作类