自动在" =>"周围添加空格

时间:2015-03-23 09:33:29

标签: sublimetext sublimetext3

在SublimeText 3上,我尝试在“=>”

之前和之后自动添加空格

我尝试在用户密钥绑定中添加:

{ "keys": ["equals,>"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } } 

这是我的片段:

<snippet>
<content><![CDATA[ => ]]></content>
</snippet>

但它不起作用。

2 个答案:

答案 0 :(得分:1)

控制台说Unknown key equals,>

equals是多余的。所以正确的设置:

{ "keys": [">"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } } 

下次请先在控制台中查找错误。

更新

  

我希望它匹配“=&gt;”。

在这种情况下应该使用

["=",">"]

{ "keys": ["=",">"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } } 

答案 1 :(得分:1)

当我读到这个问题时,我意识到我经常在某些东西的两边插入空格,所以我将下面的Sublime Text插件用于我自己的使用,并且作为事后的想法,决定将它发布在这里。

此插件在任何选定文本之前和之后添加单个空格,例如"Sel" --> " Sel "。当然,处理多个选择。单个游标被忽略,否则你只需添加两个空格。它与Sublime Text v.2和v.3兼容。

将以下代码保存在名为AddSpacesAroundSelection.py的文件中,并将文件放在Sublime Text Packages文件夹中的某个位置。例如~/.config/sublime-text-3/Packages/User/

# File:     AddSpacesAroundSelection.py
# Command:  add_spaces_around_selection
# Keys:     { "keys": ["ctrl+space"], "command": "add_spaces_around_selection" }

import sublime, sublime_plugin

class AddSpacesAroundSelectionCommand(sublime_plugin.TextCommand):
    """
    The AddSpacesAroundSelectionCommand class is a Sublime Text plugin which
    adds a single space on both sides of each selection. e.g. "Sel" -> " Sel "
    """

    def run(self, edit):
        """ run() is called when the command is run. """

        space_char = " "

        # Loop through all the selections.
        for sel in self.view.sel():

            # If something is actually selected (i.e. not just a cursor) then
            # insert a space on both sides of the selected text.
            if sel.size() > 0:

                # Insert the space at the end of the selection before the
                # beginning of it or the insertion position will be wrong.
                self.view.insert(edit, sel.end(), space_char)
                self.view.insert(edit, sel.begin(), space_char)

    # End of def run()

# End of class AddSpacesAroundSelectionCommand()

为您的用户.sublime-keymap文件添加密钥绑定。在我的系统上,ctrl+space密钥绑定未被使用,它们似乎适合使用。

{ "keys": ["ctrl+space"], "command": "add_spaces_around_selection" },

希望这有帮助。