【问题标题】:How to comment-out multiple lines in Pluma (Gedit fork)?如何在 Pluma(Gedit fork)中注释掉多行?
【发布时间】:2013-05-30 05:27:47
【问题描述】:

有没有办法在 Pluma 中注释掉一段代码(显然是 Gedit fork)? 例如在python中,我想选择一个代码块:

def foo(bar):
    return bar * 2

并注释掉:

#    def foo(bar):
#        return bar * 2

【问题讨论】:

标签: python comments editor text-editor gedit


【解决方案1】:

基于 M.O.基茨卡回答, 我使用了以下紧凑的 sn-p:

$<
lines = $PLUMA_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
    output += "#" + line + "\n";

return output
>

您可以在 sn-p 管理器的窗口内使用任何 python 代码。

【讨论】:

    【解决方案2】:
    1. 激活sn-ps插件
    2. 添加可能如下所示的 sn-p:“# $PLUMA_SELECTED_TEXT”

    更多信息: http://www.tuxradar.com/content/save-time-gedit-snippets

    【讨论】:

      【解决方案3】:

      根据 bAnEEd_meeY-dL0 之前的回答,这就是我想出的。

      1. 激活 sn-ps 插件
      2. 添加看起来像的sn-p,

        $<
        selected_txt = $PLUMA_SELECTED_TEXT
        output = "" 
        for line in selected_txt.split("\n"):
            line = "#" + line
            output = output + line+ "\n" 
        
        return output
        >
        
      3. 不要忘记填写“激活”部分。您不需要填写所有内容。我把 Ctrl+M 放在了快捷方式。

      注意:这将注释多行,但会在最底线添加一行。

      【讨论】:

        【解决方案4】:

        根据之前的答案和一些研究,我想出了一个更“特色”的 sn-p 版本 :-)

        选中当前行或有光标时注释当前行,例如:

        from requests import post # cursor currently here or this line selected
        from collections import defaultdict
        

        按 CTRL+M

        #from requests import post
        from collections import defaultdict
        

        在选择时按 CTRL+M 或在注释行中使用光标再次取消注释

        注释多行并切换块的注释,例如:

        #from requests import post # both lines selected
        from collections import defaultdict
        

        按 CTRL + M

        from requests import post # both lines selected
        #from collections import defaultdict
        

        当行被注释时,您总是可以按 CTRL+M 取消注释。这是片段:

        $<
        lines = $PLUMA_SELECTED_TEXT.split("\n")
        if lines == ['']:
            # Already commented line ...
            if $PLUMA_CURRENT_LINE.startswith("#"):
                return $PLUMA_CURRENT_LINE[1:]
            else:   # ... then uncomment it
                return "#" + $PLUMA_CURRENT_LINE
        else:
            output = "";
            for line in lines:
                if line.startswith("#"):
                    output += line[1:] + "\n"
                else:
                    output += "#" + line + "\n"
            return output.rstrip()
        >
        

        【讨论】:

          【解决方案5】:

          这是我的解决方案。特点:

          • 切换代码的​​注释/取消注释
          • 评论/取消评论时保留缩进
          • 如果未选择文本,则 cmets/uncmets 当前行

          享受吧。

          $<
          import re
          
          def get_lines():
              selected = $PLUMA_SELECTED_TEXT
              if selected:
                  return selected
              else:
                  return $PLUMA_CURRENT_LINE
          
          def toggle(selected_txt):
              lines = [] 
              for line in selected_txt.split("\n"):
                  if not line:
                      lines.append(line)
                      continue
                  try:
                      spaces, content = re.findall(r'^\s+|.+', line)
                  except:
                      spaces = ""
                      content = line
          
                  if content.startswith("#"):
                      lines.append("{}{}".format(spaces, content[1:]))
                  else:
                      lines.append("{}#{}".format(spaces, content))
          
              return "\n".join(lines)
          
          return toggle(get_lines())
          >
          

          【讨论】:

            【解决方案6】:

            上述所有答案目前(2022 年 1 月)都不起作用。

            应从 STDIN 读取所选文本。

            在 python 中,这将是: selected = sys.stdin.readlines()

            你的输出应该是简单的 print() 编辑。

            【讨论】:

              猜你喜欢
              • 2015-12-23
              • 1970-01-01
              • 2015-11-18
              • 1970-01-01
              • 1970-01-01
              • 2015-01-02
              • 2016-01-26
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多