Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code Does Not Comment-out Empty Lines

Visual Studio Code Does Not Comment-out Empty Lines


I've searched everywhere for a solution to this issue but couldn't find anything, and it's been bugging me for months now.

Basically what happens is that VS Code ignores empty lines when you tell it to comment out multiple lines of code.

So for example, let's say I highlighted all the code below and told VS Code to comment it out:

package com.mycompany.app;

public class MyApp {
    public static void main(String[] args) {
        SayHello();
    }

    static void SayHello() {
        System.out.println("Hello!");
    }
}

What I expected to get:

// package com.mycompany.app;
// 
// public class MyApp {
//     public static void main(String[] args) {
//         SayHello();
//     }
// 
//     static void SayHello() {
//         System.out.println("Hello!");
//     }
// }

What I got instead:

// package com.mycompany.app;

// public class MyApp {
//     public static void main(String[] args) {
//         SayHello();
//     }

//     static void SayHello() {
//         System.out.println("Hello!");
//     }
// }

I've only experienced this with Java and Golang so far, but I assume this happens for all other languages inside VS Code too.

The reason I want my comments to stay connected to each other is so I know which lines I commented out together in case I need to uncomment them back.

I am also aware of the Shift + Alt + A shortcut, but that typically uses Block Comments (which I don't like using), and I only want Line Comments.

Is there a setting I'm missing? Because I tried searching within VS Code and couldn't find anything either.

Any help would be greatly appreciated.

like image 646
Floating Sunfish Avatar asked Dec 13 '18 02:12

Floating Sunfish


People also ask

Why comment is not working in VS code?

Change the Keyboard layout to US-Keyboard if US keyboard is not selected. If it is US keyboard then, The shortcut key might have changed. You can edit your preferences on VS code.

How do I comment all lines in Visual Studio Code?

Windows: Ctrl + K + U. Mac: Command + K + U.

How do you comment out sections in code Vscode?

Fortunately, if you use Visual Studio Code, commenting out a block of code is really quick and easy. All you need to do is select that code block with your mouse, then press the following key combination: Ctrl + K then press Ctrl + C if you're using Windows.


2 Answers

An option to have empty lines commented out is coming to v1.48. See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_48.md#thank-you and https://github.com/microsoft/vscode/pull/93160

New setting:

Editor > Comments: Ignore Empty Lines default is true (will ignore)

[editor.comments.includeEmptyLines]

In Insiders' Build already v1.48.

like image 88
Mark Avatar answered Oct 18 '22 20:10

Mark


A new VS Code setting has been added that resolves this issue:

"editor.comments.ignoreEmptyLines": false

Unfortunately, it is only tagged as "insiders-released" despite being included in Stable versions of VS Code.

Hopefully, this issue gets addressed in future versions.

like image 38
Floating Sunfish Avatar answered Oct 18 '22 20:10

Floating Sunfish