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
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
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.
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
>
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 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.
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()
>
This is my solution. Features:
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.