将字词行更改为Sublime Text 2中的垂直列表的快捷方式 [英] Shortcut to change a line of words to a vertical list in Sublime Text 2

查看:130
本文介绍了将字词行更改为Sublime Text 2中的垂直列表的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to make this title on line 1 a list of items from each word or symbol seperated by a space with a keyboard shortcut. So that I can select the title and then hit a shortcut and it will make the title a list of items like below:

试图保存密钥绑定文件.

Tried saving the Key Binding file.

推荐答案

没有内置功能,但是您可以使用插件来实现.

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": " "}}
]

当然可以将密钥更改为所需的内容.如果仅使用空格,则实际上不需要args参数.它默认为该值.我只是为了完整起见将其包括在内.

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.

修改: 我已经更新了插件,因此它现在可以处理选择,尽管此时它不能处理多个游标.

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

编辑2 如果它不起作用,请尝试打开控制台并输入view.run_command("split_line").这将在切换到控制台之前以您所处的任何视图运行该命令.通过这种方式,您可以知道该命令是否真正起作用.如果不是,则说明插件存在问题.如果是这样,则说明键绑定存在问题.

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.

这篇关于将字词行更改为Sublime Text 2中的垂直列表的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆