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

发布于 2025-01-20 17:08:35 字数 269 浏览 2 评论 0原文

为我每次使用的方法创建自定义快捷方式的最简单方法是什么?
喜欢dd() log :: info()console.log()

让我确切说明我想要的快捷方式的行为:

  1. 我想选择我想要的变量的
  2. 变量键盘shorcut
  3. brew a line
  4. passt passt passt the Spteding aptical变量

What is the simpliest way to create custom shortcuts for methods I'm using everydays?
Like dd() Log::info() or console.log().

Let me explain exactly what behavior I want for my shortcut:

  1. I want to select my variable
  2. Press keyboard shorcut
  3. Break a line
  4. Passt the code I want with the selected variable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余罪 2025-01-27 17:08:35

编辑2024:

我的快捷方式断开,也许是由于更新,这是修复我的快捷方式的新方法,但请考虑以前在同一帖子中使用旧方法来解释Bellow。

  1. (先决条件)安装多命令VSCODE扩展。
  2. 在项目的根部创建“ /.vscode/settings.json”。它将包含您的命令。

这是我的:


        {
            "multiCommand.commands": [
    
                // (ctrl+2) Only prints methode, usefull if you want to write something
                {
                    "command": "multiCommand.console.log.1",
                    "sequence": [
                        "editor.action.insertLineAfter",
                        {
                            "command": "editor.action.insertSnippet",
                            "args": {
                                "snippet": "console.log(\"-> $0\")"
                            }
                        },
                    ]
                },
        
                {
                    "command": "multiCommand.log.info.1",
                    "sequence": [
                        "editor.action.insertLineAfter",
                        {
                            "command": "editor.action.insertSnippet",
                            "args": {
                                "snippet": "Log::info(\"-> $0\");"
                            }
                        },
                    ]
                },
    
            // (ctrl+3) Select your variable before activating the shortcut, it will place the selection in the right spot automatically
            {
                "command": "multiCommand.console.log.2",
                "sequence": [
                    "editor.action.clipboardCopyAction",
                    "editor.action.insertLineAfter",
                    {
                        "command": "editor.action.insertSnippet",
                        "args": {
                            "snippet": "console.log(\"$CLIPBOARD$0 : \", $CLIPBOARD$0)\n"
                        }
                    },
                ]
            },
    
            {
                "command": "multiCommand.log.info.2",
                "sequence": [
                    "editor.action.clipboardCopyAction",
                    "editor.action.insertLineAfter",
                    {
                        "command": "editor.action.insertSnippet",
                        "args": {
                            "snippet": "Log::info(\"$CLIPBOARD$0 : \" . $CLIPBOARD$0);"
                        }
                    },
                ]
            },
    
            // (ctrl+4) PHP Log::info(array)
            {
                "command": "multiCommand.log.info.3",
                "sequence": [
                    "editor.action.clipboardCopyAction",
                    "editor.action.insertLineAfter",
                    {
                        "command": "editor.action.insertSnippet",
                        "args": {
                            "snippet": "Log::info(\"$CLIPBOARD$0 : \" . json_encode($CLIPBOARD$0));"
                        }
                    },
                ]
            },

            // (cltr+5) classic dd() with variable in php
            {
                "command": "multiCommand.dd.2",
                "sequence": [
                    "editor.action.clipboardCopyAction",
                    "editor.action.insertLineAfter",
                    {
                        "command": "editor.action.insertSnippet",
                        "args": {
                            "snippet": "dd($CLIPBOARD$0);"
                        }
                    },
                ]
            },
            ],
            "makefile.configureOnOpen": false
        }

  1. 现在您必须将这些命令绑定到VSCODE快捷方式。

Open(Windows)C:\ USER \ Yourusername \ AppData \ roaming \ code \ code \ user \ keybindings.json ,并使用targetting命令名称添加快捷方式keybinding。

请注意,我正在使用选项时,当>时,这使您可以具有相同的快捷键,但是根据文件扩展名(如果您执行php或js或...),则使用不同的命令绑定。

这是我的:

[
    {
        "key": "ctrl+2",
        "command": "multiCommand.console.log.1",
        "when": "editorLangId == javascript || editorLangId == vue"
    },
    {
        "key": "ctrl+2",
        "command": "multiCommand.log.info.1",
        "when": "editorLangId == php"
    },
    {
        "key": "ctrl+3",
        "command": "multiCommand.console.log.2",
        "when": "editorLangId == javascript || editorLangId == vue"
    },
    {
        "key": "ctrl+3",
        "command": "multiCommand.log.info.2",
        "when": "editorLangId == php"
    },

    {
        "key": "ctrl+4",
        "command": "multiCommand.console.log.3",
        "when": "editorLangId == javascript || editorLangId == vue"
    },
    {
        "key": "ctrl+4",
        "command": "multiCommand.log.info.3",
        "when": "editorLangId == php"
    },

    {
        "key": "ctrl+5",
        "command": "multiCommand.dd.2",
        "when": "editorLangId == php"
    },

所以现在每次我按ctrl + 2我插入log :: info :: info() for php文件或console.log()用于JavaScript和Vue。这确实可以帮助您减少必须记住的快捷方式!

PS:我认为我拥有c:\ users \ yourusername \ appdata \ roaming \ code \ code \ user \ settings.json不再使用的旧文件。

旧方法,

我提出了这个解决方案,这要感谢@mark在评论中,与此线程有关:我如何使用vscode在新行上插入摘要?

  1. 安装多核vscomand vscode扩展名
  2. 打开扩展的设置,然后单击<<<<代码>在设置中编辑
  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"
                }
                },
            ]
        },
  1. 然后在VSCODE中转到首选项 - &gt;键盘快捷键,打开键键。
    “

  2. 添加路径绑定命令

// (new version 2024)
{
    "key": "ctrl+1", 
    "command": "multiCommand.console.log" 
}

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

Edit 2024:

My shortcuts breaks, maybe because of an update, here's the new method to fix my shortcuts, but consider doing the old method explained bellow in the same post before.

  1. (Precondition) Install Multi-command VSCode extension.
  2. Create "/.vscode/settings.json" at the root of your project. It will contain your commands.

Here's mine:


        {
            "multiCommand.commands": [
    
                // (ctrl+2) Only prints methode, usefull if you want to write something
                {
                    "command": "multiCommand.console.log.1",
                    "sequence": [
                        "editor.action.insertLineAfter",
                        {
                            "command": "editor.action.insertSnippet",
                            "args": {
                                "snippet": "console.log(\"-> $0\")"
                            }
                        },
                    ]
                },
        
                {
                    "command": "multiCommand.log.info.1",
                    "sequence": [
                        "editor.action.insertLineAfter",
                        {
                            "command": "editor.action.insertSnippet",
                            "args": {
                                "snippet": "Log::info(\"-> $0\");"
                            }
                        },
                    ]
                },
    
            // (ctrl+3) Select your variable before activating the shortcut, it will place the selection in the right spot automatically
            {
                "command": "multiCommand.console.log.2",
                "sequence": [
                    "editor.action.clipboardCopyAction",
                    "editor.action.insertLineAfter",
                    {
                        "command": "editor.action.insertSnippet",
                        "args": {
                            "snippet": "console.log(\"$CLIPBOARD$0 : \", $CLIPBOARD$0)\n"
                        }
                    },
                ]
            },
    
            {
                "command": "multiCommand.log.info.2",
                "sequence": [
                    "editor.action.clipboardCopyAction",
                    "editor.action.insertLineAfter",
                    {
                        "command": "editor.action.insertSnippet",
                        "args": {
                            "snippet": "Log::info(\"$CLIPBOARD$0 : \" . $CLIPBOARD$0);"
                        }
                    },
                ]
            },
    
            // (ctrl+4) PHP Log::info(array)
            {
                "command": "multiCommand.log.info.3",
                "sequence": [
                    "editor.action.clipboardCopyAction",
                    "editor.action.insertLineAfter",
                    {
                        "command": "editor.action.insertSnippet",
                        "args": {
                            "snippet": "Log::info(\"$CLIPBOARD$0 : \" . json_encode($CLIPBOARD$0));"
                        }
                    },
                ]
            },

            // (cltr+5) classic dd() with variable in php
            {
                "command": "multiCommand.dd.2",
                "sequence": [
                    "editor.action.clipboardCopyAction",
                    "editor.action.insertLineAfter",
                    {
                        "command": "editor.action.insertSnippet",
                        "args": {
                            "snippet": "dd($CLIPBOARD$0);"
                        }
                    },
                ]
            },
            ],
            "makefile.configureOnOpen": false
        }

  1. Now you have to bind those commands to VSCode shortcuts.

Open (windows) C:\Users\YourUsername\AppData\Roaming\Code\User\keybindings.json and add your shortcuts keybinding with the targetting command name.

Notice that I'm using the option when, this enables you to have the same shortcut key but binding with a different command depending on the file extension (if you do PHP or JS or ...).

Here's mine:

[
    {
        "key": "ctrl+2",
        "command": "multiCommand.console.log.1",
        "when": "editorLangId == javascript || editorLangId == vue"
    },
    {
        "key": "ctrl+2",
        "command": "multiCommand.log.info.1",
        "when": "editorLangId == php"
    },
    {
        "key": "ctrl+3",
        "command": "multiCommand.console.log.2",
        "when": "editorLangId == javascript || editorLangId == vue"
    },
    {
        "key": "ctrl+3",
        "command": "multiCommand.log.info.2",
        "when": "editorLangId == php"
    },

    {
        "key": "ctrl+4",
        "command": "multiCommand.console.log.3",
        "when": "editorLangId == javascript || editorLangId == vue"
    },
    {
        "key": "ctrl+4",
        "command": "multiCommand.log.info.3",
        "when": "editorLangId == php"
    },

    {
        "key": "ctrl+5",
        "command": "multiCommand.dd.2",
        "when": "editorLangId == php"
    },

So now everytime I press ctrl + 2 I insert a Log::info() for PHP files or console.log() for Javascript and Vue. This really helps you to reduce the number of shortcut you have to remember!

PS: I think the old file I have C:\Users\YourUsername\AppData\Roaming\Code\User\settings.json is not used anymore.

Old Method

I came up with this solution, thanks to @Mark in comments, related to this thread : How can I insert a snippet on a new line with vscode?

  1. Install Multi-command VSCode extension
  2. Open the settings of the extension and click on Edit in settings.json
    enter image description here
  3. Implement your shortcut code (e.g 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"
                }
                },
            ]
        },
  1. Then in VSCode go to Preferences -> Keyboard Shortcuts, open keybindings.json
    keybindings.json location

  2. Add the path binding command

// (new version 2024)
{
    "key": "ctrl+1", 
    "command": "multiCommand.console.log" 
}

// (old version)
{
    "key": "ctrl+1",
    "command": "extension.multiCommand.execute",
    "args": { "command": "multiCommand.console.log" }
}
单挑你×的.吻 2025-01-27 17:08:35

如果我正确理解您的问题,您可能只需使用 $0 来显示光标的结束位置,并使用 \n 插入换行符。

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

If I'm understanding your question correctly, you can probably just use $0 to show where your cursor will end and use \n to insert a line break.

However, I'm not entirely sure if this works when creating a snippet from the Keyboard Shortcut file, but it works from the snippet file so I'm assuming it'll work here.

白色秋天 2025-01-27 17:08:35

为了简化文件操作,这里是Linux上的文件位置路径
settings.jsonkeybindings.json in/home/username/.config/code/user

我使用过的“ ctrl+alt+alt+p” for print()
and log.debug()的“ ctrl+alt+l”,log声明为log = logging.getLogger(__ name __) -logging-right-way/“ rel =“ nofollow noreferrer”>在此处描述

settings.json content

{
    "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 content

// 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",
    }
]

In order to ease file manipulation here is the file location path on Linux
settings.json and keybindings.json are in /home/Username/.config/Code/User

I've used "ctrl+alt+p" for print()
and "ctrl+alt+l" for LOG.debug() with LOG declared as LOG = logging.getLogger(__name__) as described here

settings.json content

{
    "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 content

// 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",
    }
]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文