简体   繁体   中英

How to comment-out multiple lines in Pluma (Gedit fork)?

Is there a way to comment-out a block of code in Pluma (Gedit fork apparently)? For example in python, I would like to select a block of code:

def foo(bar):
    return bar * 2

And comment it out:

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

Based on MO Kitzka answer, I used the following compact snippet:

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

return output
>

You can use any python code inside the window in snippet manager.

  1. activate snippets plugin
  2. add snippet which might look like: "# $PLUMA_SELECTED_TEXT"

for more info: http://www.tuxradar.com/content/save-time-gedit-snippets

Based on bAnEEd_meeY-dL0 's earlier answer, here's what I came up.

  1. activate snippets plugin
  2. add snippet that looks like,

     $< selected_txt = $PLUMA_SELECTED_TEXT output = "" for line in selected_txt.split("\\n"): line = "#" + line output = output + line+ "\\n" return output > 
  3. Don't forget to fill out "Activation" section. You don't need to fill everything. I put the Ctrl+M in short cut.

Note: This will comment multiple lines, but adds an extra line in the very bottom line.

Based on the previous answers and in some research, I've came up with a more 'featured' version of the snippet :-)

Comment the current line when selected or when it has the cursor, eg:

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

Press CTRL+M

#from requests import post
from collections import defaultdict

Uncomment it again by pressing CTRL+M when selecting or with the cursor in a commented line

Comment multiple lines and toggle the comment on blocks, eg:

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

Press CTRL + M

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

You can always uncomment by CTRL+M when the line is commented. And here is the Snippet:

$<
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()
>

This is my solution. Features:

  • Toggle commenting/uncommenting of code
  • Preserves indentation when commenting/uncommenting
  • If not text selected, comments/uncomments current line

Enjoy.

$<
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())
>

All of the above answers will currently (JAN 2022) not work.

The selected text should be read from STDIN.

In python that would be: selected = sys.stdin.readlines()

and your output should be simply print() ed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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