vscode 添加新建文件夹快捷键 (配置 when 属性)

实现目标

在获取左侧项目栏文件夹焦点情况下 添加新建文件夹的快捷键 Shift + A

我本来是个atom忠实粉丝, 但是atom使用window10自带输入法在书写汉字时总是会出现首字母缺失的情况, 查了好久没法解决, 直接放弃了atom, 转而使用Vs code书写markdown,
但是使用Vs code时发现在左侧导航栏配置新建文件和文件夹没有快捷方式, 于是就想配置一个, 但是在百度上搜了好长时间, 却总是解决不了KeyBoard Shortcuts上面的when 属性, 最终访问的 Vs code 官方文档 解决的.

下面是官方文档配置快捷键的地址
https://code.visualstudio.com/docs/getstarted/keybindings

相应步骤

  1. 点击左下角设置图标, 点击其中的选项KeyBoard Shortcuts.

    image

  2. 输入 “explorer.new” 找到相应命令, 点击 explorer.newFolder 行配置上 “Shift + A”, 快捷键. 但是这里还有个 When 属性, 如果放任不管的话, 软件会默认全局快捷键, 试想一下, 在你编辑的时候突然按了个 “Shift + A”, 结果新建了个文件夹, 那真是太糟糕了, 所以 When 属性一定要解决.

    image

  3. 在KeyBoard Shortcuts视图里, 有一个打开keybindings.json文件的链接, 点击可以打开该文件,
    image
    此时我们可以看到右侧文件的相应配置, 这就是我们刚刚配置的东西. 我们可以参照左边的格式配置 when 属性, 可是我们该怎么选择属性呢.
    image

  4. 找到符合条件的When属性

    现在我们可以打开 Vs code 的官方文档 Key Bindings for Visual Studio Code,这个是国外的网站, 纯英文. 可能打不开, 下面贴上文档里的关于 When 的介绍, 里面也是英文的, 不过作为使用Vs code的开发人员, 这点应该能看懂的, 实在不行也可以google翻译.

    Context nameTrue when
    Editor contexts
    editorFocusAn editor has focus, either the text or a widget.
    editorTextFocusThe text in an editor has focus (cursor is blinking).
    textInputFocusAny editor has focus (regular editor, debug REPL, etc.).
    inputFocusAny text input area has focus (editors or text boxes).
    editorHasSelectionText is selected in the editor.
    editorHasMultipleSelectionsMultiple regions of text are selected (multiple cursors).
    editorReadonlyThe editor is read only.
    editorLangIdTrue when the editor’s associated language ID matches. Example: "editorLangId == typescript".
    isInDiffEditorThe active editor is a difference editor.
    isInEmbeddedEditorTrue when the focus is inside an embedded editor.
    Operating system contexts
    isLinuxTrue when the OS is Linux
    isMacTrue when the OS is macOS
    isWindowsTrue when the OS is Windows
    isWebTrue when accessing the editor from the Web
    List contexts
    listFocusA list has focus.
    listSupportsMultiselectA list supports multi select.
    listHasSelectionOrFocusA list has selection or focus.
    listDoubleSelectionA list has a selection of 2 elements.
    listMultiSelectionA list has a selection of multiple elements.
    Mode contexts
    inSnippetModeThe editor is in snippet mode.
    inQuickOpenThe Quick Open dropdown has focus.
    Resource contexts
    resourceSchemeTrue when the resource Uri scheme matches. Example: "resourceScheme == file"
    resourceFilenameTrue when the Explorer or editor filename matches. Example: "resourceFilename == gulpfile.js"
    resourceExtnameTrue when the Explorer or editor filename extension matches. Example: "resourceExtname == .js"
    resourceDirnameTrue when the Explorer or editor’s resource absolute folder path matches. Example: "resourceDirname == /users/alice/project/src"
    resourcePathTrue when the Explorer or editor’s resource absolute path matches. Example: "resourcePath == /users/alice/project/gulpfile.js"
    resourceLangIdTrue when the Explorer or editor title language ID matches. Example: "resourceLangId == markdown"
    isFileSystemResourceTrue when the Explorer or editor file is a file system resource that can be handled from a file system provider
    resourceSetTrue when an Explorer or editor file is set
    resourceThe full Uri of the Explorer or editor file
    Explorer contexts
    explorerViewletVisibleTrue if Explorer view is visible.
    explorerViewletFocusTrue if Explorer view has keyboard focus.
    filesExplorerFocusTrue if File Explorer section has keyboard focus.
    openEditorsFocusTrue if OPEN EDITORS section has keyboard focus.
    explorerResourceIsFolderTrue if a folder is selected in the Explorer.
    Editor widget contexts
    findWidgetVisibleEditor Find widget is visible.
    suggestWidgetVisibleSuggestion widget (IntelliSense) is visible.
    suggestWidgetMultipleSuggestionsMultiple suggestions are displayed.
    renameInputVisibleRename input text box is visible.
    referenceSearchVisiblePeek References peek window is open.
    inReferenceSearchEditorThe Peek References peek window editor has focus.
    config.editor.stablePeekKeep peek editors open (controlled by editor.stablePeek setting).
    quickFixWidgetVisibleQuick Fix widget is visible.
    parameterHintsVisibleParameter hints are visible (controlled by editor.parameterHints.enabled setting).
    parameterHintsMultipleSignaturesMultiple parameter hints are displayed.
    Debugger contexts
    debuggersAvailableAn appropriate debugger extension is available
    inDebugModeA debug session is running.
    debugStateActive debugger state. Possible values are inactive, initializing, stopped, running.
    debugTypeTrue when debug type matches. Example: "debugType == 'node'".
    inDebugReplFocus is in the Debug Console REPL.
    Integrated terminal contexts
    terminalFocusAn integrated terminal has focus.
    terminalIsOpenAn integrated terminal is opened.
    Timeline view contexts
    timelineFollowActiveEditorTrue if the Timeline view is following the active editor.
    Timeline view item contexts
    timelineItemTrue when the timeline item’s context value matches. Example: "timelineItem =~ /git:file:commit\\b/".
    Extension contexts
    extensionTrue when the extension’s ID matches. Example: "extension == eamodio.gitlens".
    extensionStatusTrue when the extension is installed. Example: "extensionStatus == installed".
    extensionHasConfigurationTrue if the extension has configuration.
    Global UI contexts
    notificationFocusNotification has keyboard focus.
    notificationCenterVisibleNotification Center is visible at the bottom right of VS Code.
    notificationToastsVisibleNotification toast is visible at the bottom right of VS Code.
    searchViewletVisibleSearch view is open.
    sideBarVisibleSide Bar is displayed.
    sideBarFocusSide Bar has focus.
    panelFocusPanel has focus.
    inZenModeWindow is in Zen Mode.
    isCenteredLayoutEditor is in centered layout mode.
    workbenchStateCan be empty, folder (1 folder), or workspace.
    workspaceFolderCountCount of workspace folders.
    replaceActiveSearch view Replace text box is open.
    viewTrue when view identifier matches. Example: "view == myViewsExplorerID".
    viewItemTrue when viewItem context matches. Example: "viewItem == someContextValue".
    isFullscreenTrue when window is in fullscreen.
    focusedViewThe identifier of the currently focused view.
    canNavigateBackTrue if it is possible to navigate back.
    canNavigateForwardTrue if it is possible to navigate forward.
    canNavigateToLastEditLocationTrue if it is possible to navigate to the last edit location.
    Global Editor UI contexts
    textCompareEditorVisibleAt least one diff (compare) editor is visible.
    textCompareEditorActiveA diff (compare) editor is active.
    editorIsOpenTrue if one editor is open.
    groupEditorsCountNumber of editors in a group.
    activeEditorGroupEmptyTrue if the active editor group has no editors.
    activeEditorGroupIndexA number starting from 1 reflecting the position of an editor group in the editor grid. The group with index 1 will be the first in the top-left corner.
    activeEditorGroupLastWill be true for the last editor group in the editor grid.
    multipleEditorGroupsTrue when multiple editor groups are present.
    activeEditorThe identifier of the active editor in a group.
    activeEditorIsDirtyTrue when the active editor in a group is dirty.
    activeEditorIsNotPreviewTrue when the active editor in a group is not in preview mode.
    activeEditorIsPinnedTrue when the active editor in a group is pinned.
    inSearchEditorTrue when focus is inside a search editor.
    Configuration settings contexts
    config.editor.minimap.enabledTrue when the setting editor.minimap.enabled is true.
  5. 鉴于我们要添加访问左侧 Explorer 时有效的快捷键, 因此可以选择Explorer contexts 中的 explorerResourceIsFolder 属性.,最终 keybindings.json 文件如下

    // Place your key bindings in this file to override the defaults
    [
       {
          "key": "a",
          "command": "explorer.newFile",
          "when": "explorerResourceIsFolder"
       },
       {
          "key": "shift+a",
          "command": "explorer.newFolder",
          "when": "explorerResourceIsFolder"
       }
    ]
    

    image


新版的 vscode 已经没有必要直接去修改 keybindings.json 文件,直接在 键盘快捷方式 面板右键更改when表达式就可以了在这里插入图片描述

  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逆光影者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值