在 PowerShell ISE 中,我想使用键盘快捷键一次注释掉一行或多行,就像 Sublime Text 的做法一样。
是否可以#通过键盘快捷键添加或删除下面示例中显示的内容?
[console]::beep(350,400)<在这些之间切换>#[console]::beep(350,400)
不优雅但实用...
\n评论
\n在所有行的开头“块选择”:
\n... 然后 ...
\n取消注释
\n通过以下方式“块选择”所有#行开头的所有字符:
... 然后 ...
\n我在https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/toggling-comments-in-powershell-ise找到了一个我喜欢的解决方案。
它适用于一行或多行,甚至行内。将此函数放入您的 $Profile 文件中。
function Toggle-Comment
{
$file = $psise.CurrentFile
$text = $file.Editor.SelectedText
if ($text.StartsWith("<#")) {
$comment = $text.Substring(2).TrimEnd("#>")
}
else
{
$comment = "<#" + $text + "#>"
}
$file.Editor.InsertText($comment)
}
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Toggle Comment', { Toggle-Comment }, 'CTRL+K')
Run Code Online (Sandbox Code Playgroud)
每次打开 ISE 时都会运行 $Profile,因此该功能始终可用。该函数使用键盘快捷键 Ctrl-K 创建一个新菜单项,用于注释或取消注释所选行。
如果 $Profile 文件尚未创建(通常不会),您可以像这样创建它:
New-Item -Path $profile -ItemType "file" -Force
Run Code Online (Sandbox Code Playgroud)
(命令来源:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item ?view=powershell-7.2 )
| 归档时间: |
|
| 查看次数: |
25785 次 |
| 最近记录: |