2

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
  • Have you tried the suggestion in http://stackoverflow.com/questions/7649626/block-commenting-in-gedit – Blutack May 30 '13 at 09:08

6 Answers6

0
  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

0

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.

altroware
  • 940
  • 3
  • 13
  • 22
0

Based on M.O. 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.

altroware
  • 940
  • 3
  • 13
  • 22
0

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, e.g.:

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, e.g.:

#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()
>
Eduardo
  • 4,282
  • 2
  • 49
  • 63
0

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())
>
innisfree
  • 2,044
  • 1
  • 14
  • 24
0

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.

Ben Shomer
  • 36
  • 4