lag*_*lex 7 windows command-line prompt cmd.exe
在 Windows CMD.EXE 中,我将提示字符串设置为$P$G
,因此,如果我当前的工作目录是
C:\Some\long\folder\inner
Run Code Online (Sandbox Code Playgroud)
我的提示是这样的:
C:\Some\long\folder\inner>
Run Code Online (Sandbox Code Playgroud)
我希望它只显示最后一个(最低级别)文件夹名称,如下所示:
inner>
Run Code Online (Sandbox Code Playgroud)
其中“inner”只是最内层文件夹的名称,它应该自动更改为我当前所在目录的最内层文件夹 - 相当于仅显示当前目录名称(不是完整路径)中讨论的功能 ) 在 bash 提示符下。我怎样才能做到这一点?
如果您愿意稍微改变一下习惯,这里有一个解决方法。
\n首先,在 PATH 中选择一个可以写入\xc2\xa0的目录。\xc2\xa0\n假设你在自己的计算机上,并且拥有管理员权限,\n你可以使用 或 .\ \\Windows
xc2 \xa0 \n但使用类似.\xc2\xa0\n 的东西可能会更好\n(如果你还没有,请将\xc2\xa0it添加到你的路径中。)\\Program\xc2\xa0Files\\something
\\Users\\username\\bin
然后创建一个名为CH.BAT
或类似名称的文件,\n其中包含以下内容:
@echo off\nREM Pass the argument(s) to the real "cd". If it fails, don't do anything else.\ncd %* || exit /b\nREM Get our current directory name.\nset "dirname=%CD%"\n:loop\nREM Strip off one directory level -- remove everything through the first \\.\nset "remove_one=%dirname:*\\=%"\nREM If there's nothing left, that means dirname ENDS with a \\.\nREM The only (?) way this can happen is if it is the root directory\nREM of a filesystem (partition), e.g., C:\\.\nREM In this case, set the prompt to the normal thing, C:\\>.\nif "%remove_one%" == "" goto exit_loop\nREM If "%remove_one%" == "%dirname%", that means we are down to\nREM the last directory name (i.e., there are no backslashes left).\nif "%remove_one%" == "%dirname%" goto exit_loop\nset "dirname=%remove_one%"\nREM Keep on removing levels until we get to the bottom.\ngoto loop\n\n:exit_loop\nREM To handle the case where a directory name contains dollar sign(s)\nREM (e.g., $P$G), replace each $ with $$ to remove its special meaning\nREM in the prompt, and just display a $.\nset "dirname=%dirname:$=$$%"\nprompt %dirname%$G\n
Run Code Online (Sandbox Code Playgroud)\n然后养成打字ch
而不是cd
\n(或chdir
,如果您足够大,可以记住它的 \xc2\xa0 原始名称\n(仍然受支持))。\xc2\xa0\n我 \xc2\xa0 相信评论解释得相当好,但是,概括一下:
cd
命令行参数。\xc2\xa0\n如果\xc2\xa0it\xc2\xa0失败,则退出脚本。%varname:old=new%
C:\\
C:\\>
C:\\top\\outer\\middle\\inner
inner>
, 。$$
提示符,它将显示为\xc2\xa0 $
。\xc2\xa0\n所以\xc2\xa0我们将目录名称中的所有美元符号\n替换为$$
以使它们正确显示。cd
而不是 \xc2\xa0 ch
,\n您的提示将不会更改。\xc2\xa0\n如果发生\xc2\xa0,只需键入\xc2\xa0 ch
(或 \xc2\xa0 )\n到 \xc2\xa0 根据新的当前目录设置提示符。ch\xc2\xa0.
cd
(或\xc2\xa0 echo\xc2\xa0%CD%
)。CD.BAT
当然,将脚本命名为或并没有什么好处CHDIR.BAT
,\n因为 CMD.EXE 总是解释cd
\xc2\xa0 chdir
\nas \xe2\x80\x9cchange 目录\xe2\x80\x9d 内置命令。\xc2\xa0\你\xc2\xa0可以通过\xc2\xa0输入\xc2\xa0路径名来运行这样的\xc2\xa0脚本,\n但\n\nxc2\xa0显然\xe2\x80\x99s不可行(从\xc2\xa0工作流程的角度来看)\nas an\ xc2\xa0 覆盖/替换 \xc2\xa0 cd
。
归档时间: |
|
查看次数: |
2380 次 |
最近记录: |