Search code examples
c#visual-studio-codeformat

How to automatically delete empty lines in VS code when formatting on C#?


I have applied C# formatter on vs code that helps me formatting on save automatically, but I don't know how to set it up so that it deletes empty lines like code maid does on visual studio. I want to be able to set up rules when formatting. But cant figure it out how to set up the json of that formatteer.

Is there any extension that helps out with this? I tried installing Beautify but is isn't compatible with C#.

For example, I want to be able to delete lines 39 and 40 in the following code, but automatically as I type or at least as I save: enter image description here


Solution

  • There is an vs code extension that does this: https://marketplace.visualstudio.com/items?itemName=usernamehw.remove-empty-lines

    or you can do it manually using regular expressions:

    • Open your code in Visual Studio Code

    • From Edit Menu, select Replace or use a short cut key (command + Option + F on Mac or Ctrl + H on Windows)

    • In the find box type ^(\s)*$\n

    • Leave the replace box empty

    • Make sure the 'Use Regular Expression' is selected

    • Select the 'Replace All' button

    For more info on this see this answer.