【发布时间】: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
有没有办法在 Pluma 中注释掉一段代码(显然是 Gedit fork)? 例如在python中,我想选择一个代码块:
def foo(bar):
return bar * 2
并注释掉:
# def foo(bar):
# return bar * 2
【问题讨论】:
标签: python comments editor text-editor gedit
基于 M.O.基茨卡回答, 我使用了以下紧凑的 sn-p:
$<
lines = $PLUMA_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
output += "#" + line + "\n";
return output
>
您可以在 sn-p 管理器的窗口内使用任何 python 代码。
【讨论】:
更多信息: http://www.tuxradar.com/content/save-time-gedit-snippets
【讨论】:
根据 bAnEEd_meeY-dL0 之前的回答,这就是我想出的。
添加看起来像的sn-p,
$<
selected_txt = $PLUMA_SELECTED_TEXT
output = ""
for line in selected_txt.split("\n"):
line = "#" + line
output = output + line+ "\n"
return output
>
不要忘记填写“激活”部分。您不需要填写所有内容。我把 Ctrl+M 放在了快捷方式。
注意:这将注释多行,但会在最底线添加一行。
【讨论】:
根据之前的答案和一些研究,我想出了一个更“特色”的 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()
>
【讨论】:
这是我的解决方案。特点:
享受吧。
$<
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())
>
【讨论】:
上述所有答案目前(2022 年 1 月)都不起作用。
应从 STDIN 读取所选文本。
在 python 中,这将是:
selected = sys.stdin.readlines()
你的输出应该是简单的 print() 编辑。
【讨论】: