【问题标题】:Shortcut to change a line of words to a vertical list in Sublime Text 2在 Sublime Text 2 中将一行单词更改为垂直列表的快捷方式
【发布时间】:2013-04-05 16:28:28
【问题描述】:

是否可以在第 1 行使这个标题成为每个单词或符号的项目列表,并使用键盘快捷键用空格分隔。这样我就可以选择标题,然后点击快捷方式,它将使标题成为如下项目列表:

尝试保存键绑定文件。

【问题讨论】:

  • 如果@skuroda 正确回答了您的问题,您应该接受它。
  • 不用担心,我们只是还没有找到解决方案。到目前为止,skuroda 提供了巨大的帮助!

标签: list sublimetext2 keyboard-shortcuts


【解决方案1】:

没有内置,但您可以使用插件来完成。

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")。这将在切换到控制台之前在您所在的任何视图中运行命令。这样你就知道命令是否真的有效。如果没有,则说明插件有问题。如果是这样,那么键绑定有问题。

【讨论】:

  • 我在哪里保存新插件?
  • 创建插件的最简单方法是转到Tools -> New Plugin... 复制并粘贴此代码(它应该可以工作,但如果遇到问题,请发表评论)。然后保存文件。您需要将其保存在Packages/User 目录中。当您尝试保存插件文件时,它应该默认在那里。将文件另存为<something>.py,将<something> 替换为对您有意义的名称。之后,只需将键绑定添加到您的用户键绑定文件。
  • 好吧,我想我现在只对最后一部分感到困惑。当我把它放在用户键绑定文件中时,我是否必须保存键绑定?我将它保存为什么?请参阅上面的新照片,了解我尝试保存时的体验。 (注意:我没有保存任何东西,因为我不想搞砸任何东西)
  • 通过转到Preferences -> Key Bindings - User 打开用户键绑定文件。键绑定配置是 JSON。键绑定具体是一个包含对象条目的数组。您不想覆盖已经存在的内容,而是扩展它。我列出的将是该数组中的一个条目。
  • 我在“用户”中的键绑定还没有任何内容,可以吗?另一方面,“Defualt”有很多!顺便说一句,我真的很感谢你在这件事上的一贯帮助:)
【解决方案2】:

我修改了上面的代码供我自己使用,所以它现在尊重空白。但是我硬编码制表符而不是空格,所以如果你使用空格,你可能需要进一步改变它。它现在还假设您没有选择文本,而是将光标放在要更改为垂直间距的行中间。我将 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2017-01-28
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    相关资源
    最近更新 更多