VS Code 如何创建自定义快捷方式/代码片段?
为我每次使用的方法创建自定义快捷方式的最简单方法是什么?
喜欢dd()
log :: info()
或console.log()
。
让我确切说明我想要的快捷方式的行为:
- 我想选择我想要的变量的
- 变量键盘shorcut
- brew a line
- 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:
- I want to select my variable
- Press keyboard shorcut
- Break a line
- Passt the code I want with the selected variable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑2024:
我的快捷方式断开,也许是由于更新,这是修复我的快捷方式的新方法,但请考虑以前在同一帖子中使用旧方法来解释Bellow。
这是我的:
Open(Windows)
C:\ USER \ Yourusername \ AppData \ roaming \ code \ code \ user \ keybindings.json
,并使用targetting命令名称添加快捷方式keybinding。请注意,我正在使用选项
时,当
>时,这使您可以具有相同的快捷键,但是根据文件扩展名(如果您执行php或js或...),则使用不同的命令绑定。这是我的:
所以现在每次我按
ctrl + 2
我插入log :: info :: info()
for php文件或console.log()
用于JavaScript和Vue。这确实可以帮助您减少必须记住的快捷方式!PS:我认为我拥有
c:\ users \ yourusername \ appdata \ roaming \ code \ code \ user \ settings.json
不再使用的旧文件。旧方法,
我提出了这个解决方案,这要感谢@mark在评论中,与此线程有关:我如何使用vscode在新行上插入摘要?
console.log()
)然后在VSCODE中转到首选项 - >键盘快捷键,打开键键。

添加路径绑定命令
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.
Here's mine:
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:
So now everytime I press
ctrl + 2
I insert aLog::info()
for PHP files orconsole.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?
Edit in settings.json
console.log()
)Then in VSCode go to Preferences -> Keyboard Shortcuts, open keybindings.json

Add the path binding command
如果我正确理解您的问题,您可能只需使用
$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.
为了简化文件操作,这里是Linux上的文件位置路径
settings.json
和keybindings.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
contentkeybindings.json
contentIn order to ease file manipulation here is the file location path on Linux
settings.json
andkeybindings.json
are in /home/Username/.config/Code/UserI've used "ctrl+alt+p" for print()
and "ctrl+alt+l" for LOG.debug() with LOG declared as
LOG = logging.getLogger(__name__)
as described heresettings.json
contentkeybindings.json
content