可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have added notepad++.exe
to my Path in Environment variables.
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.
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?
Thanks.
回答1:
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.
- Run regedit and go to
HKEY_CURRENT_USER\\Software\\Microsoft\\Command Processor
Add String Value entry with the name AutoRun
and the full path of your .bat/.cmd file.
For example, %USERPROFILE%\\alias.cmd
, replacing the initial segment of the path with %USERPROFILE%
is useful for syncing among multiple machines.
This way, every time cmd is run, the aliases are loaded.
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 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.
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!
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\"
回答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?
doskey
is a textual command that is interpreted by the command processor (e.g. cmd.exe), it can\'t know to modify state in some other process (especially one that hasn\'t started yet).
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).
cmd.exe /K env.cmd
env.cmd:
title \"Foo Bar\"
doskey np=notepad++.exe $*
...
回答3:
If you\'re just going for some simple commands, you could follow these steps:
- Create a folder called C:\\Aliases
- Add C:\\Aliases to your path (so any files in it will be found every time)
- Create a .bat file in C:\\Aliases for each of the aliases you want
Maybe overkill, but unlike the (otherwise excellent) answer from @Argyll, this solves the problem of this loading every time.
For instance, I have a file called dig2.bat with the following in it:
@echo off
echo.
dig +noall +answer %1
Your np file would just have the following:
@echo off
echo.
notepad++.exe %1
FWIW, I have about 20 aliases (separate .bat files) in my C:\\Aliases directory - I just create new ones as necessary. 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, e.g.:
@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.
回答4:
Given that you added notepad++.exe to your PATH variable, it\'s extra simple.
Create a file in your System32 folder called np.bat
with the following code:
@echo off
call notepad++.exe %*
The %*
passes along all arguments you give the np
command to the notepad++.exe
command.
EDIT:
You will need admin access to save files to the System32 folder, which was a bit wonky for me. I just created the file somewhere else and moved it to System32 manually.
回答5:
Alternatively you can use cmder
which lets you add aliases just like linux:
alias subl=\"C:\\Program Files\\Sublime Text 3\\subl.exe\" $*
回答6:
Also, you can create an alias.cmd in your path (for example C:\\Windows) with the command
@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
回答7:
Console Aliases in Windows 10
To define a console alias, use Doskey.exe
to create a macro, or use the AddConsoleAlias
function.
doskey
doskey test=cd \\a_very_long_path\\test
To also pass parameters add $*
at the end: doskey short=longname $*
AddConsoleAlias
AddConsoleAlias( TEXT(\"test\"),
TEXT(\"cd \\\\<a_very_long_path>\\\\test\"),
TEXT(\"cmd.exe\"));
More information here Console Aliases, Doskey, Parameters
回答8:
Actually, I\'ll go you one better and let you in on a little technique that I\'ve used since I used to program on an Amiga. On any new system you use, be it personal or professional, step one is to create two folders: C:\\BIN and C:\\BATCH. Then modify your path statement to put both at the start in the order C:\\BATCH;C:\\BIN;[rest of path].
Having done that, if you have little out-of-the-way utilities that you need access to simply copy them to the C:\\BIN folder and they\'re in your path. To temporarily override these assignments, you can add a batch file with the same name as the executable to the C:\\BATCH folder and the path will find it before the file in C:\\BIN. It should cover anything you might ever need to do.
Of course, these days the canonical correct way to do this would be to create a symbolic junction to the file, but the same principle applies. There is a little extra added bonus as well. If you want to put something in the system that conflicts with something already in the path, putting it in the C:\\BIN or C:\\Batch folder will simply pre-empt the original - allowing you to override stuff either temporarily or permanently, or rename things to names you\'re more comfortable with - without actually altering the original.
回答9:
Expanding on roryhewitt answer.
An advantage to using .cmd files over DOSKEY is that these \"aliases\" are then available in other shells such as PowerShell or WSL (Windows subsystem for Linux).
The only gotcha with using these commands in bash is that it may take a bit more setup since you might need to do some path manipulation before calling your \"alias\".
eg I have vs.cmd which is my \"alias\" for editing a file in Visual Studio
@echo off
if [%1]==[] goto nofiles
start \"\" \"c:\\Program Files (x86)\\Microsoft Visual Studio
11.0\\Common7\\IDE\\devenv.exe\" /edit %1
goto end
:nofiles
start \"\" \"C:\\Program Files (x86)\\Microsoft Visual Studio
11.0\\Common7\\IDE\\devenv.exe\" \"[PATH TO MY NORMAL SLN]\"
:end
Which fires up VS (in this case VS2012 - but adjust to taste) using my \"normal\" project with no file given but when given a file will attempt to attach to a running VS opening that file \"within that project\" rather than starting a new instance of VS.
For using this from bash I then add an extra level of indirection since \"vs Myfile\" wouldn\'t always work
alias vs=\'/usr/bin/run_visual_studio.sh\'
Which adjusts the paths before calling the vs.cmd
#!/bin/bash
cmd.exe /C \'c:\\Windows\\System32\\vs.cmd\' \"`wslpath.sh -w -r $1`\"
So this way I can just do
vs SomeFile.txt
In either a command prompt, Power Shell or bash and it opens in my running Visual Studio for editing (which just saves my poor brain from having to deal with VI commands or some such when I\'ve just been editing in VS for hours).
回答10:
You want to create an alias by simply typing:
c:\\>alias kgs kubectl get svc
Created alias for kgs=kubectl get svc
And use the alias as follows:
c:\\>kgs alfresco-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
alfresco-svc ClusterIP 10.7.249.219 <none> 80/TCP 8d
Just add the following alias.bat
file to you path. It simply creates additional batch files in the same directory as itself.
@echo off
echo.
for /f \"tokens=1,* delims= \" %%a in (\"%*\") do set ALL_BUT_FIRST=%%b
echo @echo off > C:\\Development\\alias-script\\%1.bat
echo echo. >> C:\\Development\\alias-script\\%1.bat
echo %ALL_BUT_FIRST% %%* >> C:\\Development\\alias-script\\%1.bat
echo Created alias for %1=%ALL_BUT_FIRST%
An example of the batch file this created called kgs.bat
is:
@echo off
echo.
kubectl get svc %*
回答11:
This solution is not an apt one, but serves purpose in some occasions.
First create a folder and add it to your system path. Go to the executable of whatever program you want to create alias for. Right click and send to Desktop( Create Shortcut). Rename the shortcut to whatever alias name is comfortable. Now, take the shortcut and place in your folder.
From run prompt you can type the shortcut name directly and you can have the program opened for you. But from command prompt, you need to append .lnk and hit enter, the program will be opened.
回答12:
Since you already have notepad++.exe
in your path. Create a shortcut in that folder named np
and point it to notepad++.exe
.
回答13:
First, you could create a file named np.cmd and put it in the folder which in PATH search list.
Then, edit the np.cmd file as below:
@echo off
notepad++.exe
回答14:
Using doskey is the right way to do this, but it resets when the Command Prompt window is closed. You need to add that line to something like .bashrc equivalent. So I did the following:
- Add \"C:\\Program Files (x86)\\Notepad++\" to system path variable
- Make a copy of notepad++.exe (in the same folder, of course) and rename it to np.exe
Works just fine!