键盘快捷键,用于启用/禁用代码格式切换(VSCode)

时间:2020-08-19 16:20:40

标签: visual-studio-code format key-bindings

我经常需要打开项目,并对使用与我使用的代码格式选项不同的代码格式的项目进行小的更改。为了不重新格式化整个文件,我通常打开用户设置,搜索包含“格式”的设置,然后禁用选项Editor: Format On PasteEditor: Format On SaveEditor: Format On Type。返回项目后,我将重新启用这些选项。

我希望这更简单,例如,绑定键盘快捷键以快速切换所有这三个选项。但是,我找不到可以绑定到这些快捷方式的动作。

有人知道我想要实现的目标吗?

1 个答案:

答案 0 :(得分:1)

您可以使用扩展名Toggle来执行此操作,该扩展名使您可以同时切换许多设置。

在您的keybindings.json中:

void delete_dir_content(const fs::path& dir_path) {
    for (auto& path: fs::directory_iterator(dir_path)) {
        std::error_code err;
        std::uintmax_t n = fs::remove_all(path, err);
        if (static_cast<std::uintmax_t>(-1) == n) {
            std::cout << "Failed to remove_all(" << path << ") with error: " << err.message() << std::endl;
        }
    }
}

我还没有测试过,但是应该可以。

唯一的问题是没有可视指示器指示您处于哪种状态-也许您可以找到其他设置来进行切换,从而做到“无害”。这就是为什么我将{ "key": "alt+capslock", // whatever keybinding you wish "command": "toggle", "when": "editorTextFocus", "args": { "id": "toggleFormats", "value": [ { "editor.formatOnType": true, "editor.formatOnPaste": true, "editor.formatOnSave": true, "window.enableMenuBarMnemonics": false }, { "editor.formatOnType": false, "editor.formatOnPaste": false, "editor.formatOnSave": false, "window.enableMenuBarMnemonics": true } ] } }, 扔在那里的原因,enableMenuBarMnemonics设置为false时,键盘上的capslock键会亮起,您也可以通过 alt + < kbd> F 来查看是否打开了主菜单format选项。您可能不需要视觉提醒来指示状态或提出更好的状态。

相关问题