VS Code 如何创建自定义快捷方式/代码片段?

问题描述 投票:0回答:3

为我日常使用的方法创建自定义快捷方式的最简单方法是什么?
就像

dd()
Log::info()
console.log()

让我准确解释一下我想要的快捷方式的行为:

  1. 我想选择我的变量
  2. 按键盘快捷键
  3. 断线
  4. 通过所选变量传递我想要的代码
javascript php visual-studio-code shortcut code-snippets
3个回答
1
投票

我想出了这个解决方案,感谢评论中的@Mark,与此线程相关:How can I insert a snippet on a new line with vscode?

  1. 安装多命令 VSCode 扩展

  2. 打开扩展程序的设置,然后单击

    Edit in settings.json
    enter image description here

  3. 实施您的快捷方式代码(例如

    console.log()

     "multiCommand.commands": [
    
         {
             "command": "multiCommand.console.log",
    
             "sequence": [
                 "editor.action.clipboardCopyAction",
                 "editor.action.insertLineAfter",
                 {
                 "command": "editor.action.insertSnippet",
                 "args": {
                     "snippet": "console.log(\"$CLIPBOARD: \", $$CLIPBOARD)\n$0"
                 }
                 },
             ]
         },
    
  4. 然后在 VSCode 中转到 Preferences -> Keyboard Shortcuts,打开 keybindings.json keybindings.json location

  5. 添加路径绑定命令

    // (new version)
    {
        "key": "ctrl+1", 
        "command": "multiCommand.console.log" 
    }
    
    // (old version)
    {
        "key": "ctrl+1",
        "command": "extension.multiCommand.execute",
        "args": { "command": "multiCommand.console.log" }
    }
    

0
投票

如果我正确理解您的问题,您可能只需使用

$0
显示光标的结束位置,然后使用
\n
插入换行符。

但是,我不完全确定这在从键盘快捷键文件创建片段时是否有效,但它可以从片段文件中起作用,所以我假设它在这里可以工作。


0
投票

为了方便文件操作这里是Linux上的文件位置路径

settings.json
keybindings.json
位于 /home/Username/.config/Code/User

我使用“ctrl+alt+p”进行 print() 和 LOG.debug() 的“ctrl+alt+l”,其中 LOG 声明为

LOG = logging.getLogger(__name__)
,如 此处所述

settings.json
内容

{
    "workbench.colorTheme": "Visual Studio Light",
    "git.enableSmartCommit": true,
    "git.autofetch": true,
    "git.confirmSync": false,
    "diffEditor.ignoreTrimWhitespace": true,
    "files.trimTrailingWhitespace": true,
    "editor.minimap.enabled": false,
    "[html]": {
        "editor.defaultFormatter": "vscode.html-language-features"
    },
    "terminal.integrated.defaultProfile.osx": "bash",
    "terminal.integrated.profiles.osx": {
      "bash": {
        "path": "bash",
        "args": ["--login"]
      }
    },
    "git.openRepositoryInParentFolders": "never",
    "javascript.updateImportsOnFileMove.enabled": "always",
    "workbench.tree.enableStickyScroll": false,
    "editor.stickyScroll.scrollWithEditor": false,
    "editor.stickyScroll.enabled": false,
    "diffEditor.hideUnchangedRegions.enabled": true,
    "multiCommand.commands": [
      {
        "command": "multiCommand.printVariable",

        "sequence": [
          "editor.action.clipboardCopyAction",
          "editor.action.insertLineAfter",
          {
            "command": "type",
            "args": {
              "text": "print(f'"
            }
          },
          "editor.action.clipboardPasteAction",
          {
            "command": "type",
            "args": {
              "text": ": {"
            }
          },
          "editor.action.clipboardPasteAction",
          {
            "command": "type",
            "args": {
              "text": "}')"
            }
          },
        ]
      },
      {
        "command": "multiCommand.logVariable",

        "sequence": [
          "editor.action.clipboardCopyAction",
          "editor.action.insertLineAfter",
          {
            "command": "type",
            "args": {
              "text": "LOG.debug(f'"
            }
          },
          "editor.action.clipboardPasteAction",
          {
            "command": "type",
            "args": {
              "text": ": {"
            }
          },
          "editor.action.clipboardPasteAction",
          {
            "command": "type",
            "args": {
              "text": "}')"
            }
          },
        ]
      }
    ]
}

keybindings.json
内容

// Place your key bindings in this file to override the defaults
[
    {
        "key": "ctrl+alt+p",
        "command": "multiCommand.printVariable",
    },
    {
        "key": "ctrl+alt+l",
        "command": "multiCommand.logVariable",
    }
]

© www.soinside.com 2019 - 2024. All rights reserved.