自动在“=>”周围添加空格
Automatically add spaces around "=>"
在 SublimeText 3 上,我尝试在“=>”前后自动添加空格
我尝试在用户键绑定中添加:
{ "keys": ["equals,>"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } }
这是我的片段:
<snippet>
<content><![CDATA[ => ]]></content>
</snippet>
但是没用。
控制台显示 Unknown key equals,>
equals
是多余的。所以正确的设置:
{ "keys": [">"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } }
下次请先在控制台中查找错误。
更新
I want it matchs on "=>".
["=",">"]
应在这种情况下使用
{ "keys": ["=",">"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } }
当我读到这个问题时,我意识到我经常在某些东西的两边插入 spaces,所以我敲了下面的 Sublime Text 插件供自己使用,事后,我决定 post 在这里。
此插件在任何选定文本前后添加一个 space,例如"Sel" --> " Sel "
。当然,可以处理多项选择。单个游标将被忽略,否则您只会添加两个 space。它与 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" },
希望对您有所帮助。
在 SublimeText 3 上,我尝试在“=>”前后自动添加空格
我尝试在用户键绑定中添加:
{ "keys": ["equals,>"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } }
这是我的片段:
<snippet>
<content><![CDATA[ => ]]></content>
</snippet>
但是没用。
控制台显示 Unknown key equals,>
equals
是多余的。所以正确的设置:
{ "keys": [">"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } }
下次请先在控制台中查找错误。
更新
I want it matchs on "=>".
["=",">"]
应在这种情况下使用
{ "keys": ["=",">"], "command": "insert_snippet", "args": { "name": "Packages/User/spacer.sublime-snippet" } }
当我读到这个问题时,我意识到我经常在某些东西的两边插入 spaces,所以我敲了下面的 Sublime Text 插件供自己使用,事后,我决定 post 在这里。
此插件在任何选定文本前后添加一个 space,例如"Sel" --> " Sel "
。当然,可以处理多项选择。单个游标将被忽略,否则您只会添加两个 space。它与 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" },
希望对您有所帮助。