You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中创建自定义跳转至下一个指定字符的快捷键?

在VS Code中实现跳转至指定字符的快捷键方案

方案一:无需额外扩展,自定义极简命令

  1. 打开命令面板(Ctrl+Shift+P),执行 Developer: New Command,命名为 extension.jumpToNextDot(可自定义)。
  2. 在生成的 extension.js 文件中替换为以下逻辑(以跳转至下一个句号为例):
const vscode = require('vscode');

function activate(context) {
    let disposable = vscode.commands.registerCommand('extension.jumpToNextDot', function () {
        const editor = vscode.window.activeTextEditor;
        if (!editor) return;
        
        const document = editor.document;
        const position = editor.selection.active;
        const text = document.getText();
        const offset = document.offsetAt(position);
        
        // 查找下一个句号的位置,可替换为其他目标字符
        const nextCharIndex = text.indexOf('.', offset + 1);
        if (nextCharIndex === -1) return;
        
        const nextPosition = document.positionAt(nextCharIndex);
        editor.selection = new vscode.Selection(nextPosition, nextPosition);
    });

    context.subscriptions.push(disposable);
}

function deactivate() {}

module.exports = { activate, deactivate };
  1. 执行命令 Developer: Install Extension from Location,选择该扩展所在文件夹完成安装。
  2. 打开 keybindings.json,添加快捷键绑定:
{
    "key": "ctrl+.", // 可自定义快捷键
    "command": "extension.jumpToNextDot",
    "when": "editorTextFocus"
}

方案二:使用轻量扩展快速实现

  1. 安装 multiCommand 扩展(仅用于组合内置命令,体积小)。
  2. 打开 settings.json,添加宏配置:
"multiCommand.commands": [
    {
        "command": "multiCommand.jumpToNextDot",
        "sequence": [
            {
                "command": "editor.action.findNext",
                "args": {
                    "searchString": "\\.", // 目标字符,正则格式,可替换为其他字符
                    "isRegex": true,
                    "matchWholeWord": false,
                    "caseSensitive": false
                }
            },
            "cancelSelection"
        ]
    }
]
  1. 在 keybindings.json 中绑定快捷键:
{
    "key": "ctrl+.",
    "command": "multiCommand.jumpToNextDot",
    "when": "editorTextFocus"
}

自定义目标字符说明

  • 方案一中,只需修改 text.indexOf('.', offset + 1) 里的 . 为你需要的字符即可。
  • 方案二中,修改 searchString 的值即可,若目标字符是正则特殊字符(如 *、+),需添加转义符 \。

内容的提问来源于stack exchange,提问作者StuSey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 10:02:18