Windows命令提示符中的别名 [英] Aliases in Windows command prompt

查看:138
本文介绍了Windows命令提示符中的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 notepad ++。exe 添加到环境变量路径中。

I have added notepad++.exe to my Path in Environment variables.

现在在命令提示符下, notepad ++。exe filename.txt 打开 filename.txt 。但我只想 $ file $ .txt 来打开文件。

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.

我尝试使用 DOSKEY np = notepad ++ 。但这只是在不打开文件的情况下将已经打开的notepad ++带到了前台。如何使其打开文件?

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?

谢谢。

推荐答案

要添加到乔什的答案中,

To add to josh's answer,

您可以别名 persistent 并执行以下步骤,

you may make the alias(es) persistent with the following steps,


  1. 使用 DOSKEY 命令。

  2. 运行regedit并转到 HKEY_CURRENT_USER\Software\Microsoft\Command Processor

  3. 添加名称为 AutoRun full 的字符串值条目.bat / .cmd文件的路径。

  1. Create a .bat or .cmd file with your DOSKEY commands.
  2. Run regedit and go to 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.

例如,%USERPROFILE%\alias.cmd ,将路径的初始段替换为%USERPROFILE%对于在多台计算机之间进行同步非常有用。

For example, %USERPROFILE%\alias.cmd, replacing the initial segment of the path with %USERPROFILE% is useful for syncing among multiple machines.

对于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\







  • 请注意 $ * 语法在目录字符串以及带有参数的可执行文件之后起作用。因此,在上面的示例中,用户定义的命令 dropbox research 指向与 research 相同的目录。

  • 正如Rivenfall指出的那样,最好包含一个命令,该命令可以方便地编辑 alias.cmd 文件。请参见上面的别名。如果您处于cmd会话中,请输入 cmd 重新启动cmd并重新加载 alias.cmd 文件。


    • 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.
    • As Rivenfall pointed out, it is a good idea to include a command that allows for convenient editing of the alias.cmd file. See alias above. If you are in a cmd session, enter cmd to restart cmd and reload the alias.cmd file.
    • 当我在互联网上搜索问题的答案时,讨论要么集中在仅持久性或仅在DOSKEY上的某些用法。我希望有人将从这两个方面中受益!

      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!

      这是一个 .reg 文件来帮助您安装 alias.cmd 。现在已将其设置为上述建议的保管箱文件夹的示例。

      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.

      Windows Registry Editor Version 5.00
      
      [HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
      "AutoRun"="%USERPROFILE%\\alias.cmd"
      






      对于单用户应用程序,上述操作即可。但是,在某些情况下,有必要检查注册表项中是否首先存在 alias.cmd 。见下面的示例。

      C:\Users\Public\init.cmd 文件托管中跨用户配置:

      In a C:\Users\Public\init.cmd file hosting potentially cross-user configurations:

      @ECHO OFF
      REM Add other configurations as needed
      IF EXIST "%USERPROFILE%\alias.cmd" ( CALL "%USERPROFILE%\alias.cmd" )
      

      注册表项应相应更新为 C:\Users\Public\init.cmd 或使用 .reg 文件:

      The registry key should be updated correspondly to C:\Users\Public\init.cmd or, using the .reg file:

      Windows Registry Editor Version 5.00
      
      [HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
      "AutoRun"="C:\\Users\\Public\\init.cmd"
      

      这篇关于Windows命令提示符中的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆