在一定数量的字符后自动中断注释

时间:2013-06-07 17:43:12

标签: sublimetext2

是否可以突出显示一个大的注释块并在一定长度后自动插入中断?

简单示例

# This is a super long message that has too much information in it. Although inline comments are cool, this sentence should not be this long.

# This is a super long message that has too much information in it.
# Although inline comments are  cool, this sentence should not be this
# long.

3 个答案:

答案 0 :(得分:26)

是的!它比这个答案的长度更简单!

背景

有一个名为wrap_lines的命令,它有能力完全符合你的要求。 wrap_linesPackages/Default/paragraph.py第112行中定义,the Unofficial Docs' list of commands上的文档很少记录:

  

<强> wrap_lines
  包裹线条。默认情况下,它会将行包裹在第一个标尺的列中。

     
      
  • width [Int]:指定应包装行的列。
  •   

wrap_lines已经可以通过Edit -> Wrap菜单中的项目访问了。有一些选项可以包含光标位于第70,78,80,100和120列的段落,以及第一个标尺。默认情况下,Wrap Paragraph at Ruler映射到Windows上的Alt+Q和OSX上的Super+Alt+Q

使用标尺

第一个标尺是什么意思?您可以通过View -> Ruler召唤一个标尺,但是如果您想在屏幕上显示多个(或者更愿意使用您的标尺),您可以添加一个整数的JSON数组 - 每个整数定义一个标尺任何.sublime-settings文件。例如,我添加到用户首选项文件中的数组如下所示:

"rulers":
[
    79,
    80,
    72
],

由于第一个标尺规则,Alt+Q将在79列标记处包含一个超过79个字符的行,即使在第72列“之前”有一个标尺。“第一个标尺”并不是指最左边的标尺,而是第一个定义的标尺。如果我将80,移动到索引0,就像我在下面那样,那么这些行将换行为80列。同样适用于72

"rulers":
[
    80,
    79,
    72
],

使用密钥绑定

你说,统治者是弱者吗?您还可以编写一个新的key binding来在您选择的列中包装一行!只需在Preferences -> Key Bindings – User文件中添加以下内容:

即可
{ "keys": ["alt+q"], "command": "wrap_lines", "args": {"width": 80} },

删除args对象将改为包裹在第一个标尺上,就像Wrap Paragraph at Ruler命令一样。实际上Wrap Paragraph at Ruler的定义与默认Windows键映射文件中的定义相同:

{ "keys": ["alt+q"], "command": "wrap_lines" },

注意事项

关于wrap_lines命令的最好(在某些情况下,最差)事情之一是它将检测任何序列的非字母数字字符开始该行并在换行时复制它。它非常适合编写注释,因为您的示例所表明的行为确实发生了:

# This is a super long message that has too much information in it. Although inline comments are cool, this sentence should not be this long, because having to scroll to the right to finish reading a comment is really annoying!

变为:

# This is a super long message that has too much information in it.
# Although inline comments are cool, this sentence should not be this
# long, because having to scroll to the right to finish reading a
# comment is really annoying!

但是如果该行恰好以任何其他符号开头,比如引用的开头,那么Sublime Text也不知道如何包装它们。所以这个:

# "This is a super long message that has too much information in it. Although inline comments are cool, this sentence should not be this long, because having to scroll to the right to finish reading a comment is really annoying!"

成为这个,我们可能不想要:

# "This is a super long message that has too much information in it.
# "Although inline comments are cool, this sentence should not be this
# "long, because having to scroll to the right to finish reading a
# "comment is really annoying!"

小心开始原始线路是个好主意。此外,wrap_lines命令定位光标所触及的整个段落,而不仅仅是当前行,也不仅仅是工作选择。这意味着您可以在新包装的一系列行上再次使用该命令将它们重新包装在不同的列中,但如果您正在对齐,您最终可能会包装一些您不想要的行。 Markdown中标题下的一个段落:

# Hey, this header isn't really there!
Be careful with what starts the original line. Also, the `wrap_lines` command **targets the entire paragraph**, not just the current line.

如果该命令在该文本块中的任何位置被激活,您将得到:

# Hey, this header isn't really there! Be careful with what starts the
original line. Also, the `wrap_lines` command **targets the entire
paragraph**, not just the current line.

聪明地使用空格可以避免这种问题;标题和段落之间的另一个空行将修复包装,所以:

# Hey, this header isn't really there!

Be careful with what starts the original line. Also, the `wrap_lines` command **targets the entire paragraph**, not just the current line.

变为:

# Hey, this header isn't really there!

Be careful with what starts the original line. Also, the
`wrap_lines` command **targets the entire paragraph**, not just
the current line.

由于操作速度如此之快,因此您应该在追踪遇到的任何错误的原因,重新开始并创造性地避免它们时遇到太多麻烦。 Sublime Text 通常在将代码中的注释和非注释混合在一起时没有任何问题,所以如果你很幸运,你将不必担心它!

答案 1 :(得分:5)

在SublimeText3

(以及任何具有正则表达式支持的早期版本,即所有版本):

以下是每隔48个字符插入换行符的搜索/替换:

<强>查找

(.{48}){1}

<强>替换

\1\n

<强>解释

  • 括号形成一个组,以便替换可以引用匹配 与\1
  • .匹配任何字符,{n}匹配 确切地说n
  • replace命令接受每个匹配组和 返回 - 用附加的换行符\n替换它。

注意:\1从技术上讲仅指找到的第一个匹配项,但使用“全部替换”处理剩余的正则表达式匹配项。)

真实世界的例子:

假设您正在格式化公钥,这些公钥直接从浏览器复制而没有页眉/页脚为Google Internet Authority提供以下内容:

9c 2a 04 77 5c d8 50 91 3a 06 a3 82 e0 d8 50 48 bc 89 3f f1 19 70 1a 88 46 7e e0 8f c5 f1 89 ce 21 ee 5a fe 61 0d b7 32 44 89 a0 74 0b 53 4f 55 a4 ce 82 62 95 ee eb 59 5f c6 e1 05 80 12 c4 5e 94 3f bc 5b 48 38 f4 53 f7 24 e6 fb 91 e9 15 c4 cf f4 53 0d f4 4a fc 9f 54 de 7d be a0 6b 6f 87 c0 d0 50 1f 28 30 03 40 da 08 73 51 6c 7f ff 3a 3c a7 37 06 8e bd 4b 11 04 eb 7d 24 de e6 f9 fc 31 71 fb 94 d5 60 f3 2e 4a af 42 d2 cb ea c4 6a 1a b2 cc 53 dd 15 4b 8b 1f c8 19 61 1f cd 9d a8 3e 63 2b 84 35 69 65 84 c8 19 c5 46 22 f8 53 95 be e3 80 4a 10 c6 2a ec ba 97 20 11 c7 39 99 10 04 a0 f0 61 7a 95 25 8c 4e 52 75 e2 b6 ed 08 ca 14 fc ce 22 6a b3 4e cf 46 03 97 97 03 7e c0 b1 de 7b af 45 33 cf ba 3e 71 b7 de f4 25 25 c2 0d 35 89 9d 9d fb 0e 11 79 89 1e 37 c5 af 8e 72 69

搜索并替换(全部)后,您将获得:

9c 2a 04 77 5c d8 50 91 3a 06 a3 82 e0 d8 50 48 
bc 89 3f f1 19 70 1a 88 46 7e e0 8f c5 f1 89 ce 
21 ee 5a fe 61 0d b7 32 44 89 a0 74 0b 53 4f 55 
a4 ce 82 62 95 ee eb 59 5f c6 e1 05 80 12 c4 5e 
94 3f bc 5b 48 38 f4 53 f7 24 e6 fb 91 e9 15 c4 
cf f4 53 0d f4 4a fc 9f 54 de 7d be a0 6b 6f 87 
c0 d0 50 1f 28 30 03 40 da 08 73 51 6c 7f ff 3a 
3c a7 37 06 8e bd 4b 11 04 eb 7d 24 de e6 f9 fc 
31 71 fb 94 d5 60 f3 2e 4a af 42 d2 cb ea c4 6a 
1a b2 cc 53 dd 15 4b 8b 1f c8 19 61 1f cd 9d a8 
3e 63 2b 84 35 69 65 84 c8 19 c5 46 22 f8 53 95 
be e3 80 4a 10 c6 2a ec ba 97 20 11 c7 39 99 10 
04 a0 f0 61 7a 95 25 8c 4e 52 75 e2 b6 ed 08 ca 
14 fc ce 22 6a b3 4e cf 46 03 97 97 03 7e c0 b1 
de 7b af 45 33 cf ba 3e 71 b7 de f4 25 25 c2 0d 
35 89 9d 9d fb 0e 11 79 89 1e 37 c5 af 8e 72 69

答案 2 :(得分:2)

您好我遇到了这个解决方案,并使用Sublime Text 3进行了尝试,效果很好。如果我在python docstring上使用通常的Alt + q,那么它几乎可以做到:#/ p>

  • 会将范围限制为docstring
  • 将有开头和结尾&#39;&#39;&#39;或&#34;&#34;&#34;做得好

它涉及从默认包修改一个命令。请看这里。

https://gist.github.com/SmileyChris/4340807

在Sublime Text 3中,默认包位于\ Packages \ Default.sublime-package下。您必须解压缩它,并找到文件paragraph.py。将此文件放在您的用户包目录下,例如。 \ Data \ Packages \ Default \所以此文件现在会覆盖默认包的paragraph.py。

感谢原作者Chris Beaven(SmileyChris)。

相关问题