Sublime Forum

Possible to select a CSS block?

#1

Is it possible to set a keyboard shortcut that will select a CSS block regardless of cursor position as long as it is somewhere within that block?

Ex.:

to

0 Likes

#2

given your cursor is inside propery list you can achieve it with a macro invoked by keybinding
keybinding:

{
    "keys": "ctrl+super+/"],
    "command": "run_macro_file",
    "args": {"file": "Packages/User/select_css_block.sublime-macro"},
    "context": 
      { "key": "selector", "operator": "equal", "operand": "meta.property-list.css" }
    ]
  }

macro:


	{"args": { "to": "line" }, "command": "expand_selection", "args": {"to": "brackets"} },
	{"args": { "to": "line" }, "command": "expand_selection" }
]

it’s not perfect. it relies on the fact you put your braces on line with the selectors.
you can make this smarter by writing TextCommand and again trigger it with key shortcut.

0 Likes

#3

Thanks.

I see that it is essentially doing this: Ctrl+Shift+M (Expand Selection to Brackets) followed by Cmd+L (Expand Selection to Line).

As you mentioned, here are its current limitations:

1. Cursor should be placed in property value pair lines. This won’t work if the cursor is somewhere in the selector.

2. If selectors are in multiple lines, the ones at the top won’t get selected.

It will be great if these can be addressed.

1 Like

#4

yes, macros are not that smart, but they can save time.
if i find time to write a custom command to address your request i’ll post it here.

0 Likes

#5

If your CSS is nicely formatted with empty lines between each block but no empty lines inside brackets, you could just use Expand Selection to Paragraph.
That would solve both issues.

0 Likes