设置/使用键盘快捷键来替换Visual Studio Code中的字符 [英] Set/use a keyboard shortcut to replace a character in Visual Studio Code
问题描述
- 如何设置键盘快捷键,以将
"
替换为''
. - 或者是否有默认快捷方式将
"
替换为''
?
- How to set a keyboard shortcut to replace
""
with''
. - Or is there a default shortcut to replace
""
with''
?
我在Mac上使用Visual Studio Code.我经常用''
替换""
.
I use Visual Studio Code on a Mac. I often replace ""
with ''
.
例如,
const foo = "foo";
const bar = "bar";
⬇️
const foo = 'foo';
const bar = 'bar';
我通常将Visual Studio Code的replace函数与这样的正则表达式一起使用.
I usually use Visual Studio Code's replace function with a regular expression like this.
我想为此操作注册一个键盘快捷键.
I want to register a keyboard shortcut for this operation because I do it frequently.
我打开了 keybindings.json
,并尝试在其中设置键盘快捷键.但是我不知道该写些什么.
I opened keybindings.json
and tried to set a keyboard shortcut in it. But I have no idea what to write.
[
{
"key": "shift+cmd+'",
"command": "actions.find" // ?????????
}
]
我应该在那里写什么?还是有默认的快捷方式将"
替换为''
?
What should I write in there? Or is there a default shortcut to replace ""
with ''
?
根据Jeff Brower的回答(谢谢!),"args"
对于我的目的可能会有所帮助.我正在尝试更改他/她的密码.
According to Jeff Brower's answer (thank you!), "args"
can be helpful for my purpose. I'm trying to change his/her code.
我在 keybindings.json
中这样写,但不起作用:
I wrote in keybindings.json
like this but it doesn't work:
{
"key": "shift+cmd+'",
"command": "editor.action.startFindReplaceAction",
"args": {
"query": "\"(.+)\"",
"replace": "'$1'",
"triggerSearch": true,
"isRegex": true,
"matchWholeWord": true,
"isCaseSensitive": false
}
}
推荐答案
可以通过扩展来完成,我将显示其代码.扩展名允许您设置所需的键绑定.
It can be done with an extension, I'll show the code for that. An extension allows you to set a keybinding if that is what you want.
但是通过这些步骤很容易完成您想要的事情.
But it is easy to accomplish what you want with these steps.
- 选择任何
"
. - Cmd + Shift + L 选择所有出现的
"
. - 输入
'
.
- Select any
"
. - Cmd+Shift+L to select all occurrences of
"
. - Type
'
.
扩展代码的工作部分:
The working part of the extension code:
let disposable = vscode.commands.registerCommand('yourExtensionName.someCommandName', async function () {
const fileName = vscode.workspace.asRelativePath(vscode.window.activeTextEditor.document.uri);
vscode.commands.executeCommand('workbench.action.findInFiles',
{
query: "\"(.+)\"",
replace: "'$1'",
triggerSearch: true,
isRegex: true,
filesToInclude: fileName
// preserveCase: true,
// useExcludeSettingsAndIgnoreFiles: true,
// isCaseSensitive: true,
// matchWholeWord: true,
// filesToExclude: "./*.css"
}).then(() => {
setTimeout(() => {
vscode.commands.executeCommand('search.action.replaceAll');
}, 1000);
});
});
context.subscriptions.push(disposable);
演示:
该扩展名与 findInFiles
命令一起使用,因为 actions.find
将不接受参数.但是它仅限于当前文件.它会找到当前的文件名:
The extension works with the findInFiles
command, since actions.find
will not take arguments. But it is restricted to the current file. It finds the current fileName:
const fileName = vscode.workspace.asRelativePath(vscode.window.activeTextEditor.document.uri);
,并在 filesToInclude
参数中使用它.
这篇关于设置/使用键盘快捷键来替换Visual Studio Code中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!