[英]Prevent typing more than one space in VSCode with the exception of the start of a line
I want to restrict more than one space to be typed in VS Code unless it is before the first non-space in a line.我想限制在 VS Code 中输入多个空格,除非它在一行中的第一个非空格之前。
Additionally, if you open a file that was like that already I want those multi-spaces to be removed when saved.此外,如果您打开一个类似的文件,我希望在保存时删除这些多空格。
Here is Trailing Spaces - it looks like a workaround for what you're looking for.这是尾随空格- 它看起来像是您正在寻找的解决方法。
You are really looking for a formatter for these two functionalities: format on type and format on save.您确实在为这两个功能寻找格式化程序:类型格式和保存格式。 However, you can get the first function - "restrict more than one space to be typed in Visual Studio Code unless it is before the first non-space in a line" with the help of an extension HyperSnips .但是,您可以借助扩展HyperSnips获得第一个 function -“限制在 Visual Studio Code 中键入多个空格,除非它位于一行中的第一个非空格之前”。
For setting up that extension, see https://stackoverflow.com/a/62562886/836330要设置该扩展,请参阅https://stackoverflow.com/a/62562886/836330
Then say in your all.hsnips
file put this:然后在你的all.hsnips
文件中说:
snippet `(\S)( ){2,}` "found 2 consecutive spaces with no non-whitespace characters preceding" A
``rv = m[1] + " "``
endsnippet
That will work in all language files, you can restrict it to certain languages as mentioned in that answer linked to.这将适用于所有语言文件,您可以将其限制为链接到的答案中提到的某些语言。
Run the command HyperSnips: Reload Snippets
.运行命令HyperSnips: Reload Snippets
。
Basically you are running a snippet that uses (\S)( ){2,}
as the prefix.基本上,您正在运行一个使用(\S)( ){2,}
作为前缀的片段。 It will be triggered whenever it detects two or more spaces in a row and will replace them with just one!每当它连续检测到两个或多个空格时都会触发它,并将它们替换为一个!
The snippet will only be triggered if those two spaces are preceded by a non-whitespace (so some character) and thus your second requirement that multiple spaces be allowed before the first character in a line.只有当这两个空格前面有一个非空格(所以是某个字符)时才会触发该片段,因此您的第二个要求是在一行中的第一个字符之前允许多个空格。
It also works between existing words if you go there to edit - you will not be able to add a space to an existing space!!如果您在 go 那里进行编辑,它也适用于现有单词- 您将无法在现有空间中添加空格!
Demo:演示:
The gif doesn't really capture it well but every time I am trying to write a second consecutive space it deletes it leaving only the one. gif 并不能很好地捕捉到它,但每次我试图写第二个连续的空间时,它都会删除它,只留下一个。
Your second question was "if you open a file that was like that already I want those multispaces to be removed when saved".您的第二个问题是“如果您打开一个类似的文件,我希望在保存时删除这些多空间”。
I suggest running a script on save.我建议在保存时运行一个脚本。 Using, for example, Trigger on Task Save .例如,使用Trigger on Task Save 。 In your settings:在您的设置中:
"triggerTaskOnSave.tasks": {
"stripSpaces": [
"**/*.txt" // here restricting it to .txt files
],
},
This runs the stripSpaces
task whener you save a .txt
file.这会在您保存.txt
文件时运行stripSpaces
任务。 That task (in tasks.json) could look like:该任务(在 tasks.json 中)可能如下所示:
{
"label": "stripSpaces",
"type": "shell",
"command": "awk -f stripSpaces.awk ${relativeFile} > temp; mv temp ${relativeFile}"
}
which runs this stripSpaces.awk
script:运行此stripSpaces.awk
脚本:
# strip multiple consecutive spaces from a file
# unless they are the beginning of the line
BEGIN {
regex="\\S( ){2,}";
}
{
if (match($0,regex)) {
gsub(/( ){2,}/, " ")
print
}
else print
}
Demo:演示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.