Shortcut to change a line of words to a vertical list in Sublime Text 2

后端 未结 2 1455
臣服心动
臣服心动 2021-01-07 08:09

\"enterIs it possible to make this title on line 1 a list of items from each word or symbol se

相关标签:
2条回答
  • 2021-01-07 08:28

    Nothing built in, but you can do it with a plugin.

    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")
    

    You can then add the following in your key binding file.

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

    Of course changing the key to something that you want. You don't actually need the args argument if you are just using a space. It defaults to that. I just included it for completeness.

    Edit: I've updated the plugin so it now handles selections, though it does not handle multiple cursors at this point.

    Edit 2 If it is not working, try opening the console and entering view.run_command("split_line"). This will run the command in whatever view you were in prior to switching to the console. This way you know if the command actually works. If it doesn't then there is a problem with the plugin. If it does, then there is a problem with the key binding.

    0 讨论(0)
  • 2021-01-07 08:28

    I adapted the above code for my own use, so that it now respects whitespace. But I hard-coded tabs instead of spaces, so if you use spaces you might have to change it further. It also now assumes you have no text selected and instead have the cursor in the middle of the line to be changed to vertical spacing. I left intro/outro as arguments so you can also use it for [] or (), although maybe some more escaping is needed in that case for the regex.

    Before:

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

    After:

            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")
    
    0 讨论(0)
提交回复
热议问题