Sublime Forum

Comment each line of a selection in CSS

#1

Hello,

In CSS, you can’t comment juste one line (like “//” or “#”), you’ve got to use /* */.

That’s why in Sublime Text, if you comment by block or not a selection, it’s exactly the same.

This is the result of a non block comment toggle:

However, there’s a trick we can do here is this one (a macro):

[
  {
    "args":
    {
      "to": "line"
    },
    "command": "expand_selection"
  },
	{
		"args": null,
		"command": "split_selection_into_lines"
	},
	{
		"args":
		{
			"block": false
		},
		"command": "toggle_comment"
	},
  {
    "args": null,
    "command": "single_selection"
  }
]

When selecting some lines, it takes all the width of each line, then it splits into multiple selectors, then it comments them, then it comes back to one selection.

And to be compliant with the shortcut already existing, we can do this:

  {
    "keys": ["super+shift+:"],
    "command": "run_macro_file",
    "args": {
      "file": "res://Packages/User/macros/CSS Comment Line By Line.sublime-macro"
    },
    "context":
    [
      { "key": "selector", "operator": "equal", "operand": "source.css", "match_all": true },
    ]
  }

(note: I’m on an azerty keyboard so super+shift+: means super+forward_slash (which doesn’t work when I tried)

The result:

I think it could/should be directly in Sublime Text core. What do you think?

1 Like

#2

The task is simple enough to be performed with just two key bindings. I don’t think we need a built-in shortcut for it.

0 Likes

#3

It’s not about how simple it is. It’s about how do handle correctly the option “block” on toggle-comments on CSS.

In CSS, if you set block to “true” or not, it’s the same behaviour.

0 Likes