命令提示符中的别名

命令提示符中的别名

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

问题描述

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

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

现在在命令提示符下,notepad++.exe filename.txt 打开 filename.txt.但我只想做 np filename.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++.但它只是在不打开文件的情况下将已经打开的记事本++带到了最前沿.我怎样才能让它打开文件?

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?

谢谢.

推荐答案

要添加到 josh 的答案中,

To add to josh's answer,

您可以通过以下步骤使别名持久化

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

  1. 使用 DOSKEY 命令创建 .bat 或 .cmd 文件.
  2. 运行 regedit 并转到 HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor
  3. 使用名称 AutoRun 和 .bat/.cmd 文件的完整路径添加字符串值条目.

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

这样,每次运行 cmd 时,都会加载别名.

This way, every time cmd is run, the aliases are loaded.

对于 Windows 10,将条目添加到 HKEY_LOCAL_MACHINESOFTWAREMicrosoftCommand 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 FilesSublime 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)Sketchpad5GSP505en.exe"
DOSKEY alias=notepad %USERPROFILE%Dropboxalias.cmd

:: Common directories

DOSKEY dropbox=cd "%USERPROFILE%Dropbox$*"
DOSKEY research=cd %USERPROFILE%DropboxResearch

  • 请注意,$* 语法在目录字符串以及接受参数的可执行文件之后起作用.所以在上面的例子中,用户定义的命令dropbox research 指向与research 相同的目录.
  • 正如 Rivenfall 所指出的,包含一个允许方便地编辑 alias.cmd 文件的命令是一个好主意.请参阅上面的 alias.如果您在 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.现在已将其设置为上面建议的 Dropbox 文件夹的示例.

      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_USERSoftwareMicrosoftCommand Processor]
      "AutoRun"="%USERPROFILE%\alias.cmd"
      

      对于单用户应用程序,以上都可以.然而,有些情况需要首先检查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.

      在托管潜在跨用户配置的 C:UsersPublicinit.cmd 文件中:

      In a C:UsersPublicinit.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:UsersPublicinit.cmd 或使用 .reg 文件:

      The registry key should be updated correspondly to C:UsersPublicinit.cmd or, using the .reg file:

      Windows Registry Editor Version 5.00
      
      [HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor]
      "AutoRun"="C:\Users\Public\init.cmd"
      

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

08-04 13:58