简体   繁体   English

如何使用参数将命令别名为同一命令?

[英]How to alias a command to the same command with parameters?

I'm looking for a way to map "emacs" to "emacs -nw" in powershell.我正在寻找一种在 powershell 中将“emacs”映射到“emacs -nw”的方法。 I tried this (screenshot below), and it adds it as an alias, but it doesn't work.我试过这个(下面的截图),并将它添加为别名,但它不起作用。 However, the command "emacs -nw" works before setting the alias.但是,命令“emacs -nw”在设置别名之前起作用。

在此处输入图片说明

And I also want a way to save that alias for future sessions (restarting the powershell gets me back to square zero)而且我还想要一种方法来为以后的会话保存该别名(重新启动 powershell 让我回到零方格)

EDIT:(aditional info) Also tried creating a function, but when calling that function powershell freezes for a while, then I get the following message (screenshot below)编辑:(附加信息)还尝试创建一个函数,但是当调用该函数时,powershell 冻结了一段时间,然后我收到以下消息(下面的屏幕截图) 在此处输入图片说明

EDIT2:(aditional info) At first, function enw {emacs.exe -nw} (changed function name to 'enw' for explanation purposes) seems to work. EDIT2:(附加信息)起初, function enw {emacs.exe -nw} (为了解释目的将函数名更改为“enw”)似乎有效。 But then there's a problem.但是接下来有一个问题。 For the standard emacs, I can type emacs -nw filename.txt , and that would open the file filename.txt in emacs -nw .对于标准 emacs,我可以输入emacs -nw filename.txt ,这将在emacs -nw打开文件filename.txt Calling the function enw filename.txt will not open the filename.txt file, providing the same result as just typing enw .调用函数enw filename.txt不会打开filename.txt文件,提供与输入enw相同的结果。

The solution to this is function enw {Param($myparam) emacs.exe -nw $myparam}对此的解决方案是function enw {Param($myparam) emacs.exe -nw $myparam}

创建一个函数,但在函数内显式调用 .exe :

function emacs {emacs.exe -nw}

try creating a function:尝试创建一个函数:

function emacs {emacs.exe -nw}

then add it to your powershell profile ( $profile )然后将其添加到您的 powershell 配置文件( $profile

I see you have your answers but I just wanted to spell it out here.我看到你有你的答案,但我只是想在这里拼出来。 You created a recursive function.您创建了一个递归函数。

By not specifying the .exe as part of the extension PowerShell was able to match emacs back to your function definition.通过不指定 .exe 作为扩展名的一部分,PowerShell 能够将emacs匹配回您的函数定义。 So it would call itself infinitely.所以它会无限地调用自己。

You can see this by checking the results of Get-Command emacs both before and after you register your function.您可以通过在注册函数之前和之后检查Get-Command emacs的结果来看到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM