如果您选择几行文本并按Ctrl-Shilf-L(或菜单>选择>拆分为行),您会发现每行的末尾都有多光标。
这里是试图完成类似任务的插件,但是现在光标应该位于行的开头。
此插件有效,但我想对其进行一些改进-它应该保留选择,就像默认的Ctrl-Shift-L行为一样。
import sublime_plugin
class SplitIntoLinesReverseCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command("split_selection_into_lines")
# Seems to be redundant:
# self.view.run_command("expand_selection", {"to": "line"})
self.view.run_command("move", {"by": "characters", "forward": False})
# Already tried this:
# self.view.run_command("move", {"by": "characters", "forward": False, "extend": True})
要测试的文字:
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar
baz baz baz baz baz baz baz baz baz baz baz baz baz baz baz baz
答案 0 :(得分:1)
听起来您想要这样的东西:
import sublime_plugin
class SplitIntoLinesReverseCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command("split_selection_into_lines")
self.view.run_command("move", {"by": "characters", "forward": True})
self.view.run_command("move_to", {"to": "bol", "extend": True})
这里的步骤是:
还有其他方法可以通过直接操纵选择来完成相同的操作,但是通常可以像在这里那样在现有命令上进行构建。
如果启用了自动换行并且选择中的任何行都被换行,则上述解决方案将无法正常工作。这是因为在包装线时,用于跳转到该行的开头和结尾的移动命令将跳转到插入记号所在的行的逻辑(可视)结尾,而不是该行的物理开头和结尾。
为了在自动换行情况下工作,您需要考虑到这一点。同样,可以通过手动摆弄选择内容来实现,但是更简单的方法是在命令执行期间暂时关闭自动换行,以使移动按预期进行:
import sublime_plugin
class SplitIntoLinesReverseCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Save the state of word wrap and ensure that it's turned off
word_wrap = self.view.settings().get("word_wrap", None)
self.view.settings().set("word_wrap", False)
self.view.run_command("split_selection_into_lines")
self.view.run_command("move", {"by": "characters", "forward": True})
self.view.run_command("move_to", {"to": "bol", "extend": True})
# Replace the setting as long as it existed.
if word_wrap is not None:
self.view.settings().set("word_wrap", word_wrap)