如何在 vscode 中粘贴常量代码块
要求是创建一个常量并将其保存在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用代码段。创建您喜欢的任何代码片段,然后为其添加键盘快捷键。它不仅可以通过快捷方式插入代码片段,还可以使用前缀(例如“ifmain”)插入代码片段。
首先,您创建代码片段。导航到文件> 首选项 > 用户片段 > python.json(或 ctrl+shift+p '配置用户片段')。在此处创建任何片段(采用 json 语法),然后保存文件。
您提供的代码片段示例:
其次,您向片段添加键盘快捷键。 文件> 首选项 > 键盘快捷键(ctrl+k ctrl+s)。然后单击右上角名为“打开键盘快捷键”的小图标,如下所示:
将快捷方式添加到插入snippet 命令然后保存文件。示例:
按 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:
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:
Add shortcut to the insert snippet command then save the file. Example:
Press ctrl+k 1 > it will insert snippet. Start typing 'ifmain' then smash Tab > it will insert snippet.