如何在点击 space 后自动在 Sublime Text 3 的括号之间插入 space
How to automatically insert a space between parentheses in Sublime Text 3 after hitting space
当我键入 (
并点击 Space 时,我希望 Sublime Text 自动添加一个 space 并移动插入符号,以便最终结果是 ( ^ )
,其中 ^
是光标。
如何调整键绑定以允许这样做?
我已经能够使用
调整按键绑定
{ "keys": ["("], "command": "insert_snippet", "args": {"contents": "( [=12=] )"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\)|]|;|\}|$)", "match_all": true }
]
}
但是当键入 (
时,这将始终将其格式化为 ( ^ )
。我宁愿在点击 space
.
后对其进行格式化
我可以在默认绑定中看到在键入 {
后点击 enter
的情况,这将 运行 一个宏,所以我想我可以将它用作模板,但我在我的文件系统上找不到该宏以查看其工作原理。
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\}", "match_all": true }
]
},
你的初始绑定的想法是正确的,但你想要的是将它绑定到按下 space 而不是按下左括号;这将要求您首先手动打开成对的括号,然后按 space 插入您想要的代码段:
{ "keys": [" "], "command": "insert_snippet", "args": {"contents": " [=10=] "},
"context": [
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\($", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\)", "match_all": true }
]
},
现在按 space 将插入两个 space,光标位于它们之间,但前提是光标直接位于两个括号之间。
但是请注意,此绑定将停止默认情况下存在的 Backspace 上的绑定,以便在您返回 space 时删除两个括号,因为该绑定就像这个一样,要求光标被括号包围,但是一旦触发此绑定,就不再是这种情况了。
所以您也可以包含此绑定,它会检测光标何时处于那种情况并运行与默认绑定相同的宏;这将允许您按 Space 插入 spaces,然后决定您不需要它们并点击 返回space 删除它们。
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\( $", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^ \)", "match_all": true }
]
},
I can see in the default bindings a case when hitting enter after typing { which will run a macro so I thought I could use it as a template, but I can't find that macro on my filesystem to see how it works.
Sublime 附带的包存储在 sublime-package
文件中(扩展名已更改的 zip 文件)。从 core sublime 查看它们内部的最简单方法是使用命令面板中的 View Package File
,它会显示当前加载的每个包中的每个文件。然后,您可以输入过滤器文本以缩小您要查看的文件的范围,然后 select 打开文件。
请注意,以这种方式打开的文件是只读的(因为它们来自包文件),提醒您编辑它们没有任何效果。
当我键入 (
并点击 Space 时,我希望 Sublime Text 自动添加一个 space 并移动插入符号,以便最终结果是 ( ^ )
,其中 ^
是光标。
如何调整键绑定以允许这样做?
我已经能够使用
调整按键绑定{ "keys": ["("], "command": "insert_snippet", "args": {"contents": "( [=12=] )"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\)|]|;|\}|$)", "match_all": true }
]
}
但是当键入 (
时,这将始终将其格式化为 ( ^ )
。我宁愿在点击 space
.
我可以在默认绑定中看到在键入 {
后点击 enter
的情况,这将 运行 一个宏,所以我想我可以将它用作模板,但我在我的文件系统上找不到该宏以查看其工作原理。
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\}", "match_all": true }
]
},
你的初始绑定的想法是正确的,但你想要的是将它绑定到按下 space 而不是按下左括号;这将要求您首先手动打开成对的括号,然后按 space 插入您想要的代码段:
{ "keys": [" "], "command": "insert_snippet", "args": {"contents": " [=10=] "},
"context": [
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\($", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\)", "match_all": true }
]
},
现在按 space 将插入两个 space,光标位于它们之间,但前提是光标直接位于两个括号之间。
但是请注意,此绑定将停止默认情况下存在的 Backspace 上的绑定,以便在您返回 space 时删除两个括号,因为该绑定就像这个一样,要求光标被括号包围,但是一旦触发此绑定,就不再是这种情况了。
所以您也可以包含此绑定,它会检测光标何时处于那种情况并运行与默认绑定相同的宏;这将允许您按 Space 插入 spaces,然后决定您不需要它们并点击 返回space 删除它们。
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\( $", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^ \)", "match_all": true }
]
},
I can see in the default bindings a case when hitting enter after typing { which will run a macro so I thought I could use it as a template, but I can't find that macro on my filesystem to see how it works.
Sublime 附带的包存储在 sublime-package
文件中(扩展名已更改的 zip 文件)。从 core sublime 查看它们内部的最简单方法是使用命令面板中的 View Package File
,它会显示当前加载的每个包中的每个文件。然后,您可以输入过滤器文本以缩小您要查看的文件的范围,然后 select 打开文件。
请注意,以这种方式打开的文件是只读的(因为它们来自包文件),提醒您编辑它们没有任何效果。