是的!它比这个答案的长度可能暗示的要简单!
背景
有一个名为的命令wrap_lines
可以完全满足您的需求。在第 112 行wrap_lines
中定义,并且在非官方文档的命令列表中很少记录:Packages/Default/paragraph.py
wrap_lines 换
行。默认情况下,它在第一个标尺的列处换行。
wrap_lines
已经可以通过Edit -> Wrap
菜单中的项目访问。有选项可以将光标位于第 70、78、80、100 和 120 列的段落以及第一个标尺换行。默认情况下,Wrap Paragraph at Ruler
映射到Alt+Q
Windows 和Super+Alt+Q
OSX。
与统治者合作
我说的第一任统治者是什么意思?您可以通过 召唤一个标尺View -> Ruler
,但如果您想要多个屏幕上的标尺(或者希望您的标尺以书面形式出现),您可以将一个 JSON 整数数组(每个整数都定义一个标尺)添加到任何.sublime-settings
文件中。例如,我添加到用户首选项文件中的数组如下所示:
"rulers":
[
79,
80,
72
],
由于第一个标尺规则,Alt+Q
将在 79 列标记处换行超过 79 个字符,即使在第 72 列“之前”有一个标尺。“第一个标尺”不是指最左边的标尺,而是首先定义标尺。如果我80,
像下面那样移动到索引 0,那么这些行将在 80 列处换行。同样对于72
.
"rulers":
[
80,
79,
72
],
使用键绑定
统治者是为弱者服务的,你说?您还可以编写一个新的键绑定以在您选择的列处换行!只需将这样的内容添加到您的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在代码中混合注释和非注释通常不会有任何问题,所以如果你幸运的话,你永远不必担心它!