如何在 vscode 中粘贴常量代码块

发布于 2025-01-20 04:19:27 字数 299 浏览 4 评论 0原文

要求是创建一个常量并将其保存在 vscode 中并为其分配一个快捷方式,每当使用该快捷方式时,存储的代码块就会自动粘贴。

例如: 下面的代码是我需要在多个文件中添加的内容

if __name__ == "__main__":
    s = [1,2,3,4,5]
    print(fun(s))

,因此,我需要在 vscode 中将其存储在一个地方一次,并为其分配一个快捷键,下次使用快捷键时,代码块应该按原样粘贴。 另外,不希望当前复制到剪贴板上的内容受到影响。

有办法做到这一点吗?

The requirement is to create a constant and save it in vscode and assign a shortcut to it and whenever the shortcut is used the stored block of code gets pasted automatically.

For example:
the below code is something that i need to add in multiple files

if __name__ == "__main__":
    s = [1,2,3,4,5]
    print(fun(s))

So, i need to store this in one place just once in vscode and assign a shortcut key to it, next time using the shortcut key the codeblock should get pasted as it is.
Also, would not want the content currently copied on clipboard to be affected.

Is there a way to do this?

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

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

发布评论

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

评论(1

ぃ弥猫深巷。 2025-01-27 04:19:27

您可以使用代码段。创建您喜欢的任何代码片段,然后为其添加键盘快捷键。它不仅可以通过快捷方式插入代码片段,还可以使用前缀(例如“ifmain”)插入代码片段。

首先,您创建代码片段。导航到文件> 首选项 > 用户片段 > python.json(或 ctrl+shift+p '配置用户片段')。在此处创建任何片段(采用 json 语法),然后保存文件。
您提供的代码片段示例:

{
  "if name main": {
    "prefix": "ifmain",
    "body": [
      "if __name__ == '__main__':",
      "    s = [1, 2, 3, 4, 5]",
      "    print(fun(s))"
    ],
    "description": "my cool snippet"
  }
}

其次,您向片段添加键盘快捷键。 文件> 首选项 > 键盘快捷键(ctrl+k ctrl+s)。然后单击右上角名为“打开键盘快捷键”的小图标,如下所示:

Screenshot

将快捷方式添加到插入snippet 命令然后保存文件。示例:

{
  "key": "ctrl+k 1",
  "command": "editor.action.insertSnippet",
  "when": "editorTextFocus",
  "args": {
    "langId": "python",
    "name": "if name main"
}

ctrl+k 1 >它将插入片段。开始输入“ifmain”,然后按下Tab>它将插入片段。

You can go with the Snippets. Create any code snippet you like, then add a keyboard shortcut to it. It gives the ability to not only insert snippet by a shortcut but also with a prefix (for example 'ifmain').

First, you create code snippet. Navigate to File > Preferences > User snippets > python.json (or ctrl+shift+p 'configure user snippets'). Create any snippet here (in json syntax), then save the file.
Example of snippet for code you provided:

{
  "if name main": {
    "prefix": "ifmain",
    "body": [
      "if __name__ == '__main__':",
      "    s = [1, 2, 3, 4, 5]",
      "    print(fun(s))"
    ],
    "description": "my cool snippet"
  }
}

Secondly, you add a keyboard shortcut to snippet. File > Preferences > Keyboard shortcuts (ctrl+k ctrl+s). Then click small icon located on top right corner named 'Open Keyboard Shortcuts', as shown here:

Screenshot

Add shortcut to the insert snippet command then save the file. Example:

{
  "key": "ctrl+k 1",
  "command": "editor.action.insertSnippet",
  "when": "editorTextFocus",
  "args": {
    "langId": "python",
    "name": "if name main"
}

Press ctrl+k 1 > it will insert snippet. Start typing 'ifmain' then smash Tab > it will insert snippet.

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