Visual Studio Code - delete all blank lines - regex

好久不见. 提交于 2019-12-03 00:16:18

问题


I spent some time trying to figure out how to delete all blank lines in Visual Studio Code and I can't get it working. Anybody knows how to do it please?

If I search for ^$ while typing it in search field VSC does find the blank lines (completely blank lines, means no white spaces) but it doesn't remove them when I hit Replace All. It does nothing:

For blank lines with spaces ^\s+$ Search works, but it does not remove them. What it does is it replaces them with a blank line without spaces :))

It must be I am doing something wrong. I just can't figure out what is it. Anybody knows? Thanks.


回答1:


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




回答2:


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                               |
-----------------------------------------------



回答3:


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!




回答4:


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!!




回答5:


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




回答6:


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.




回答7:


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.




回答8:


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!




回答9:


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,}



回答10:


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.




回答11:


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




回答12:


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

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



回答13:


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




回答14:


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.



来源:https://stackoverflow.com/questions/36350324/visual-studio-code-delete-all-blank-lines-regex

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