Visual Studio Code - delete all blank lines - regex

后端 未结 15 1220
心在旅途
心在旅途 2021-01-29 18:36

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 fo

相关标签:
15条回答
  • 2021-01-29 19:02

    install extention "Remove Blank Lines" in vscode

    0 讨论(0)
  • 2021-01-29 19:05

    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

    0 讨论(0)
  • 2021-01-29 19:05

    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!

    0 讨论(0)
  • 2021-01-29 19:05

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

    0 讨论(0)
  • 2021-01-29 19:05

    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.

    0 讨论(0)
  • 2021-01-29 19:06

    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.

    0 讨论(0)
提交回复
热议问题