在Sublime Text 2中将一行单词更改为垂直列表的快捷方式

时间:2013-04-05 16:28:28

标签: list sublimetext2 keyboard-shortcuts

enter image description here是否可以在第1行为此标题创建一个项目列表,每个单词或符号由带有键盘快捷键的空格分隔。这样我就可以选择标题然后点击一个快捷方式,它会使标题成为下面的项目列表:

enter image description here

尝试保存密钥绑定文件。

2 个答案:

答案 0 :(得分:3)

内置任何内容,但您可以使用插件执行此操作。

import sublime
import sublime_plugin
import re


class SplitLineCommand(sublime_plugin.TextCommand):
    def run(self, edit, split_pattern=" "):
        view = self.view
        cursors = view.sel()
        if len(cursors) == 1:
            cursor = cursors[0]
            begin_offset = 0
            end_offset = 0
            if cursor.empty():
                region = view.line(cursor)
                content = view.substr(region)
                new_content = re.sub(split_pattern, "\n", content)

                view.replace(edit, region, new_content)
            else:
                region = cursor
                content = view.substr(region)
                new_content = ""
                if view.line(region).begin() != region.begin():
                    new_content = "\n"
                    begin_offset = 1
                new_content += re.sub(split_pattern, "\n", content)

                if view.line(region).end() != region.end():
                    new_content += "\n"
                    end_offset = - 1

            view.replace(edit, region, new_content)
            cursors.clear()
            cursors.add(sublime.Region(region.begin() + begin_offset, region.begin() + len(new_content) + end_offset))
            view.run_command("split_selection_into_lines")

然后,您可以在密钥绑定文件中添加以下内容。

[
    { "keys": ["f8"], "command": "split_line", "args": {"split_pattern": " "}}
]

当然,将密钥更改为您想要的内容。如果您只是使用空格,则实际上不需要args参数。它默认为。我把它包括在内是为了完整性。

修改 我已经更新了插件,因此它现在可以处理选择,但此时它不会处理多个游标。

编辑2 如果它不起作用,请尝试打开控制台并输入view.run_command("split_line")。这将在切换到控制台之前以任何视图运行命令。这样你就可以知道命令是否真的有效。如果没有,则插件存在问题。如果是,则密钥绑定存在问题。

答案 1 :(得分:0)

我修改了上面的代码以供自己使用,以便现在可以使用空格。但是我对制表符(而不是空格)进行了硬编码,因此,如果使用空格,则可能必须进一步更改它。现在还假定您没有选择文本,而是将光标置于要更改为垂直间距的行中间。我保留了intro / outro作为参数,因此您也可以将其用于[]或(),尽管在这种情况下可能需要对正则表达式进行更多的转义。

之前:

        fields = { 'Team1', 'Team2', 'Player1', 'Player2', 'Tab=Round', 'DateTime_UTC=DateTime', 'HasTime=TimeEntered', 'OverviewPage=Tournament', 'ShownName', 'Winner', 'Stream' },

之后:

        fields = {
            'Team1',
            'Team2',
            'Player1',
            'Player2',
            'Tab=Round',
            'DateTime_UTC=DateTime',
            'HasTime=TimeEntered',
            'OverviewPage=Tournament',
            'ShownName',
            'Winner',
            'Stream',
        },
import sublime
import sublime_plugin
import re

class SplitLineCommand(sublime_plugin.TextCommand):
    def run(self, edit, sep=",", repl= "\n", intro="{", outro="}"):
        view = self.view
        find = re.escape(sep + ' ') + '*(?! *$| *\n)'
        intro_repl = intro + repl
        intro = intro + ' *'
        outro_repl_start = sep + repl
        outro_repl_end = outro
        outro = ',? *' + outro
        repl = sep + repl
        cursors = view.sel()
        if len(cursors) == 1:
            cursor = cursors[0]
            begin_offset = 0
            end_offset = 0
            if cursor.empty():
                region = view.line(cursor)
                content = view.substr(region)
                line_str = view.substr(view.line(view.sel()[0]))
                tabs = len(line_str) - len(line_str.lstrip())
                intro_repl = intro_repl + '\t' * (tabs + 1)
                repl = repl + '\t' * (tabs + 1)
                outro_repl = outro_repl_start + ('\t' * tabs) + outro_repl_end
                content = re.sub(outro, outro_repl, content)
                content = re.sub(find, repl, content)
                content = re.sub(intro, intro_repl, content)
                view.replace(edit, region, content)
            cursors.clear()
            cursors.add(sublime.Region(region.begin() + begin_offset, region.begin() + len(content) + end_offset))
            view.run_command("split_selection_into_lines")