Aliases in Windows command prompt

后端 未结 16 2817
故里飘歌
故里飘歌 2020-11-22 10:47

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

Now in command prompt, notepad++.exe filename.txt opens the filename

16条回答
  •  清歌不尽
    2020-11-22 11:20

    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).

提交回复
热议问题