我已经在环境变量的路径中添加了notepad++.exe。

现在在命令提示符中,notepad++.exe filename.txt打开filename.txt。但我想用np filename。txt来打开文件。

我尝试使用DOSKEY np=notepad++。但它只是把一个已经打开的notepad++放在前面,而不打开文件。我怎样才能让它打开文件呢?

谢谢。


当前回答

如果你想在每个目录/每个项目的基础上启用别名,试试下面的方法:

First, create a batch file that will look for a file named aliases in the current directory and initialize aliases from it, let’s call it make-aliases.cmd @echo off if not exist aliases goto:eof echo [Loading aliases...] for /f "tokens=1* delims=^=" %%i in (aliases) do ( echo %%i ^<^=^> %%j doskey %%i=%%j ) doskey aliases=doskey /macros echo -------------------- echo aliases ^=^> list all echo alt+F10 ^=^> clear all echo [Done] Then, create aliases wherever you need them using the following format: alias1 = command1 alias2 = command2 ... for example: b = nmake c = nmake clean r = nmake rebuild Then, add the location of make-aliases.cmd to your %PATH% variable to make it system-wide or just keep it in a known place. Make it start automatically with cmd. I would definitely advise against using HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun for this, because some development tools would trigger the autorun script multiple times per session. If you use ConEmu you could go another way and start the script from the startup task (Settings > Startup > Tasks), for example, I created an entry called {MSVC}: cmd.exe /k "vcvars64 && make-aliases", and then registered it in Explorer context menu via Settings > Integration> with Command: {MSVC} -cur_console:n, so that now I can right-click a folder and launch a VS developer prompt inside it with my aliases loaded automatically, if they happen to be in that folder. Without ConEmu, you may just want to create a shortcut to cmd.exe with the corresponding command or simply run make-aliases manually every time.

如果你碰巧忘记了你的别名,使用aliases宏,如果出现任何问题,只需按Alt+F10重置当前会话,这是cmd中的内置命令。

其他回答

由于doskey不适用于PowerShell或Windows 10终端应用程序,我将分享这个解决方案。

演示如何在Windows 10中创建别名。输入参数的别名:

com=D:\website_development\laragon\bin\php\php-8.1.2-Win32-vs16-x64/php8 D:\website_development\laragon\bin\composer/composer.phar

过程:

在C:驱动器中创建一个名为scripts的文件夹。 在scripts文件夹中,创建com.bat 打开com.bat 使用特定PHP版本运行composer命令的示例: @echo掉 设置路径=D:\website_development\laragon\bin\php\php-8.1.2- win32 -vs16-x64/php8 设置参数= % * 设置命令=%path% %args% 命令% % 保存它 点击“开始” 搜索“编辑环境变量” 点击“高级” 将“scripts”目录添加到PATH。

现在您可以作为该命令的别名运行该命令。

注意:如果你想添加一个新的别名,只需创建一个新的bat文件。

假设您将notepad++.exe添加到PATH变量中,这就非常简单了。 用以下代码在System32文件夹中创建一个名为np.bat的文件:

@echo off
call notepad++.exe %*

%*将您赋予np命令的所有参数传递给notepad++.exe命令。

编辑: 您需要管理员权限才能将文件保存到System32文件夹,这对我来说有点不可靠。我只是在其他地方创建了该文件,并手动将其移动到System32。

此外,您还可以创建别名。在您的路径(例如C:\Windows)中使用命令

@echo %2 %3 %4 %5 %6 > %windir%\%1.cmd

一旦你这样做了,你可以这样做:

alias nameOfYourAlias commands to run 

然后你可以输入命令行

nameOfYourAlias 

这将执行

commands to run 

但对我来说,最好的方法是添加一个程序的路径。

setx PATH "%PATH%;%ProgramFiles%\Sublime Text 3" /M 

现在我崇高地奔跑

subl index.html

非常简单的自定义别名:

在系统的任何地方创建一个专门用于别名的新文件夹。创建一个新文件,命名为你想命名的别名,扩展名为.cmd。将所有命令写入文件,例如

cd /D D:\Folder
g++ -o run something.cpp

复制文件夹的路径。

>系统>关于>系统高级设置>环境变量…

现在在System variables部分中找到Path变量。单击它一次,然后单击Edit。现在单击New并粘贴复制的文本。

单击“确定”、“确定”和“确定”。重新启动Powershell/cmd提示符,瞧,您得到了持久的别名!您也可以使用相同的文件夹来创建其他别名,而无需再次更改Path变量!

如果你只是想要一些简单的命令,你可以按照这些步骤:

创建名为C:\Aliases的文件夹 将C:\ alias添加到路径中(这样每次都会找到其中的任何文件) 在C:\Aliases中为每个你想要的Aliases创建一个.bat文件

也许有点过分,但与@Argyll的回答不同(否则很好),这解决了每次加载的问题。

例如,我有一个名为dig2.bat的文件,其中包含以下内容:

@echo off
echo.
dig +noall +answer %1

你的np文件只会有以下内容:

@echo off
echo.
notepad++.exe %1

然后只需将C:\Aliases文件夹添加到PATH环境变量中。如果您已经打开CMD或PowerShell,则需要重新启动它。

FWIW,我有大约20个别名(单独的.bat文件)在我的C:\ aliases目录-我只是根据需要创建新的。也许不是最简洁的,但效果很好。

更新:根据用户@Mav的一个很好的建议,使用%*比%1更好,这样你就可以将多个文件传递给命令,例如:

@echo off
echo.
notepad++.exe %*

这样,你可以这样做:

np c:\temp\abc.txt c:\temp\def.txt c:\temp\ghi.txt

它会打开所有3个文件。