Visual Studio Code 中自定义单词片段分隔符的方法
自定义VS Code中“单词片段”的方法
目前VS Code并没有内置的editor.wordPartSeparators这类设置来直接自定义单词片段的分隔规则,但有两种可行的解决办法:
借助第三方扩展
可以在VS Code扩展商店搜索类似「Custom Word Separators」的扩展,这类扩展专门支持自定义单词片段的分隔逻辑。安装完成后,你就能在设置面板里配置需要的单词片段分隔符,让deleteWordPartLeft/deleteWordPartRight命令按照你定义的规则识别单词片段。自定义宏实现
如果不想依赖第三方扩展,可以借助「multi-command」这类宏扩展来实现自定义逻辑。比如创建一个宏,模拟deleteWordPartLeft的行为:逐个向左判断字符,直到遇到你定义的分隔符就停止删除。具体步骤如下:- 安装「multi-command」扩展;
- 在
settings.json中添加宏配置:
"multiCommand.commands": [ { "command": "multiCommand.customDeleteWordPartLeft", "sequence": [ "cursorLeftSelect", { "command": "runCommands", "args": { "commands": [ "cursorLeftSelect", "multiCommand.customDeleteWordPartLeft" ], "when": "editorTextFocus && !inString && !inComment && !editorHasSelection && !isInDiffEditor && textSelectedCharacter =~ /[^a-zA-Z0-9_]/i" } }, "deleteLeft" ] } ]- 在
keybindings.json中绑定快捷键(以Alt+Backspace为例):
{ "key": "alt+backspace", "command": "multiCommand.customDeleteWordPartLeft", "when": "editorTextFocus && !editorHasSelection" }上面配置里的
textSelectedCharacter =~ /[^a-zA-Z0-9_]/i是自定义的分隔符判断逻辑,你可以根据需求修改正则表达式,比如添加-、.等字符到排除集合中,以此定义哪些字符属于单词片段的分隔边界。
内容的提问来源于stack exchange,提问作者mgarort
相关产品推荐
相关产品推荐

