Sublime Forum

Is there a way to comment out multiple lines?

#1

For instance, if I wanted to take this:

[code]x = 1
print(“x is {}.”).format(x)

y = 2
print(“y is {}.”).format(y)[/code]
Then highlight the bottom 2 lines and turn it to this:

[code]x = 1
print(“x is {}.”).format(x)

y = 2

print(“y is {}.”).format(y)[/code]

I’m sure there is an easy way to do this (as ST has thought of everything), but I can’t seem to find it.

0 Likes

#2

Nevermind. I’m an idiot. Just found it under “Edit > Comments”.

For those interested, the shortcut is Ctrl + \ and it works perfect.

Edit: I am actually kinda curious what “Toggle Block Comment” does though.

0 Likes

#3

Both of those key bindings are set to the same command: toggle_comment.
Unless you’ve overriden the key binding, they should both do exactly the same thing: toggle that region’s comment delimiters.

0 Likes

#4

Toggle Comment - Ctrl + /
Toggle Block Comment - Ctrl + Shift + /

0 Likes

#5

Not exactly true.
Ctrl+/ It comments lines like this:

// line 1
// line 2

On the other hand, ctrl+shift+/ (block comment) will comment lines like this:

/* 
line 1
line 2
*/

(which is… block!)

0 Likes

#6

[quote=“iamntz”]

Not exactly true.
Ctrl+/ It comments lines like this:

// line 1
// line 2

On the other hand, ctrl+shift+/ (block comment) will comment lines like this:

/* 
line 1
line 2
*/

(which is… block!)[/quote]

Ah okay, it’s the argument passed that is different.

	{ "keys": "ctrl+/"], "command": "toggle_comment", "args": { "block": false } },
	{ "keys": "ctrl+shift+/"], "command": "toggle_comment", "args": { "block": true } },

I guess the follow up question would be, if the block comment is set in the Comments.tmPreferences file where are the comment line preferences set?

0 Likes

#7

Just a tip, in case anyone ends up here from the search engines, because the comment short cut doesn’t work:

With a non-US keyboard layout the default shortcut Ctrl+/ (Win/Linux) does not work.

I managed to change it into Ctrl+1 as per Robert’s comment by writing

[
  {
    "keys": ["ctrl+1"],
    "command": "toggle_comment",
    "args": { "block": false }
  },
  {
    "keys": ["ctrl+shift+1"],
    "command": "toggle_comment",
    "args": { "block": true }
  }
]

to Preferences -> Key Bindings (on the right half, the user keymap).

From https://stackoverflow.com/a/40663089

0 Likes