Visual Studio Code - delete all blank lines - regex

半城伤御伤魂 提交于 2019-12-02 13:58:20

For those who might be interested - what worked for me in version 1.3.1 (and still works in 1.33.1) to delete blank lines I used ctrl+h (find and replace) alt+r (Use regular expression)

In find box then:

\n\n 

In replace box:

\n 

This should make two consecutive end of line signs into one.

edited:

If you need to replace more empty lines (more than two) at once, you can use following regular expression in find box:

\n+ 

If you need to replace also empty lines with whitespaces, then you need to use following regular expression in find box:

\n+\s*\n 

VS code is using javascript regular expressions

Dzumret

What also works is this regex pattern:

^\s*$\n 

Then CTRL+Enter to replace all lines.

Explanation of the above pattern:

----------------------------------------------- |  ^ | beginning of string anchor             | ----------------------------------------------- | \s | any whitespace character               | ----------------------------------------------- | '*'| zero or more repetitions               | ----------------------------------------------- |  $ | end of string anchor                   | ----------------------------------------------- | \n | new line                               | ----------------------------------------------- 
Antonio Delgado

Visual Studio Code 1.13.0 Linux Lite:

  • Hit CTRL+H
  • Select "Use Regular Expression"
  • Find box: ^(\s)*$\n (enter many ending \n as you need)
  • Replace box: empty
  • Click replace all

Empty lines gone!

Here's my regex, it catches all extra new lines and empty lines that contain only space,tabs etc

\n\s*\n 

And I replace all matches with \n

Explanation

\n       : New Line \s*      : Zero or more consecutive white space characters or new lines \n       : Another New Line 

P.S :Remember to choose the regex option in the search window!!

NEO

Try using ^\s*\n in the Replace dialog of VS Code -

no, you're doing it right.

I get the same behaviour here.

I also tried another regex: (\r?\n){2,} but it seems that it only works for single lines.

maybe there is a preference to change the default regexp behaviour, or maybe VS is just built in such a way (line based)

ofcourse it's not a big deal to cut-paste and back from another text editor.

Alan

I found the following works best for me in Visual Studio:

Replace: ^\n$ With: <no value here>

This will find all empty lines and clear them out.

I don't know about yout, but memorize a lot of commands for me seams a waste of time!

Use the extension "Blank Line Organizer", here's the description:

This extension will help you organize blank lines in the code by removing multiple blank lines. The extension removes blank lines only from the selected lines if any, otherwise from the entire file

How to use it: check the description of extension, but it really seams nice!

blankLine.triggerOnSave boolean true    If set to true, the command will be triggered on save. 

In other words, after save the file, it automatically cleans up!

At My case. kobi7 solution (\r?\n){2,} only worked for me, I had to run it again with small modification to make it work for single lines (just changed 2 to 1)

^(\r?\n){1,} 

Code Maid Extension is all you need. You can use shortcut Ctrl M + Space bar to Cleanup your file, It will remove empty lines and format your code. You also can configure about format and cleanup rule. Hope this useful.

One or more line breaks (\n)+ and replace by \n

There is my version for cleaning empty lines with white space:

find:    (?:\s*$(\r?\n)){2,} replace: $1 

Replace: ^\n$ With: "blank space"

Windows 10,Visual Studio 2015

Ctrl + H

Find... -> ^\s*

Replace all

Ctrl + A

Ctrl + K + F

Thank you for your question, I learned something new.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!