PowerShell ISE - 是否有用于注释行或多行的键盘快捷键

Ste*_*Ste 8 powershell-ise

在 PowerShell ISE 中,我想使用键盘快捷键一次注释掉一行或多行,就像 Sublime Text 的做法一样。

是否可以#通过键盘快捷键添加或删除下面示例中显示的内容?

[console]::beep(350,400)<在这些之间切换>#[console]::beep(350,400)

Geo*_*ope 6

不优雅但实用...

\n

评论

\n

在所有行的开头“块选择”:

\n
    \n
  • AltMouse-Left-Click-Drag\也不
  • \n
  • AltShift尽管\xe2\x86\x91\xe2\x86\x93
  • \n
\n

... 然后 ...

\n
    \n
  • Shift3
  • \n
\n

取消注释

\n

通过以下方式“块选择”所有#行开头的所有字符:

\n
    \n
  • AltMouse-Left-Click-Drag\也不
  • \n
  • AltShift尽管\xe2\x86\x91\xe2\x86\x93\xe2\x86\x90\xe2\x86\x92
  • \n
\n

... 然后 ...

\n
    \n
  • Delete
  • \n
\n


asm*_*ley 3

我在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 )