[英]How to add a command line argument to powershell in profile.ps1 for Alias?
我对Powershell还是很陌生,但是我想将我最喜欢的编辑器添加到Powershell中的Alias中。
我在C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\profile.ps1
中编辑了profile.ps1
,它将在PowerShell启动时自动运行。
我尝试输入New-Alias np notepad.exe
,它在每次启动PowerShell时都能正常运行。
但是,我想使用Sublime Text 3
作为编辑器。 我遵循了此SO页面中的指示: 如何编写中间带有参数的PowerShell别名?
我需要Sublime Text
的命令行是"C:\\Program Files\\Sublime Text 3\\sublime_text.exe" -n [FirstArg]
我出来的像这样: function sublime { 'C:\\Program Files\\Sublime Text 3\\sublime_text.exe' -n $args }
它不起作用,我得到了这样的错误:
At C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1:5 char:72
+ ... lime { 'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
+ ~~
Unexpected token '-n' in expression or statement.
At C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1:5 char:75
+ ... lime { 'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
+ ~~~~~
Unexpected token '$args' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
任何帮助将不胜感激。 谢谢!
如果没有成为Sublime用户,我怀疑这应该可以工作:
function Start-Sublime {
param([string]$args)
$limeArgs = [string]::Empty
if ($args -ne $null) {
$limeArgs = $args
}
Start-Process "$env:ProgramFiles\Sublime Text 3\sublime_text.exe" -n $limeArgs
}
Set-Alias lime Start-Sublime
不是最漂亮的PowerShell代码,但我想它会做您所追求的。 它比隐式&
运算符更容易理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.