简体   繁体   English

防止在 VSCode 中输入多个空格,但行首除外

[英]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:演示:

awk 在保存演示时去除空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VSCode:package 可从多个模块访问 - VSCode: The package is accessible from more than one module 防止 VSCode 在键入 Golang 导入时要求提供 GitHub 凭据? - Prevent VSCode from asking for GitHub credentials when typing Golang imports? 有什么方法可以在同一连接中连接到多个数据库? (在 VScode 中使用 SQLtools 扩展) - Any way to connect to more than one db in the same connection? (using SQLtools extension in VScode) VsCode:如何避免为每个编辑器组打开多个文件? - VsCode: how to avoid to open more than one file for each editor group? package 仅在 VSCode 下可从多个模块问题中访问 - The package is accessible from more than one module problem under VSCode only 无法通过未捕获的异常启动vscode - Can't start vscode with Uncaught Exception 将多个光标添加到VSCode(Windows)中的行首? - Add multiple cursors to the start of line in VSCode (Windows)? 防止vscode在python文件中输入后自动添加两个空格 - Prevent vscode automatic add two space after enter in python file 有没有办法让 VSCode 移动到代码的开头(而不是行本身的开头)? - Is there a way to make VSCode move to the start of the code (not the start of the line itself)? 在 VSCode 扩展中再添加一个 cursor - Add one more cursor in VSCode extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM