如何通过Select By扩展实现VS Code指定列跳转及空白补全?
解决方案
1. 设置快捷键跳转到指定列(无需依赖Select By扩展)
VS Code内置命令即可实现精准跳转,无需第三方扩展,配置步骤如下:
- 打开快捷键配置文件:按下
Cmd+Shift+P(Mac)/Ctrl+Shift+P(Win),输入Preferences: Open Keyboard Shortcuts (JSON)回车。 - 在配置文件中添加以下代码(示例为跳转到第50列,快捷键设为
Cmd+M):
{ "key": "cmd+m", "command": "cursorMove", "args": { "to": "column", "value": 50 }, "when": "editorTextFocus" }
- 保存后,在编辑器内按下
Cmd+M,光标会直接跳转到当前行第50列;若当前行字符数不足50,光标停在行尾。
如果坚持使用Select By扩展,可替换为以下配置:
{ "key": "cmd+m", "command": "selectby.regex", "args": { "regex": "^.{49}", "flags": "m", "action": "moveCursor", "moveCursorLocation": "end" }, "when": "editorTextFocus" }
(注:^.{49}匹配行首前49个字符,光标移到匹配内容末尾即为第50列)
2. 一键补全行空白到指定列(C64风格格式规范)
通过自定义命令+正则替换实现一键补全,无需手动复制或编写脚本,提供两种方式:
方式1:批量处理当前文件所有行
- 打开快捷键配置文件,添加以下代码(示例为补到第50列,快捷键设为
Cmd+Shift+M):
{ "key": "cmd+shift+m", "command": "editor.action.replaceInFiles", "args": { "files": "${file}", "replace": "$0${1:/repeat(' ', 50 - length($0))}", "regex": "^.*$", "isRegex": true, "triggerSearch": true }, "when": "editorTextFocus" }
- 按下快捷键后,当前文件所有行都会补全空格到第50列;若行长度已超过50列,则保持原样。
方式2:灵活处理选中行/当前行
先安装Multi Command扩展,再按以下步骤配置:
- 打开
settings.json(按下Cmd+,,点击右上角Open Settings (JSON)),添加宏配置:
"multiCommand.commands": [ { "command": "multiCommand.padToColumn50", "sequence": [ "cursorSelectLine", // 选中当前行,若要处理多行可替换为"editor.action.selectAll" { "command": "editor.action.replaceSelection", "args": { "text": "${TM_SELECTED_TEXT/(.*)/$1${1:/repeat(' ', 50 - strlen($1))}/g}" } }, "cursorMove": { "to": "eol" } // 光标移到行尾,可选 ] } ]
- 打开快捷键配置文件,添加触发快捷键:
{ "key": "cmd+shift+m", "command": "multiCommand.padToColumn50", "when": "editorTextFocus" }
- 按下快捷键后,当前选中行(或所有行)会自动补全空格到第50列。
内容的提问来源于stack exchange,提问作者Vi Et
相关产品推荐
相关产品推荐

