Sublime Forum

Insert a domain specific line-break

#1

In some editors, when you hit ctrl+enter the editor inserts a language domain specific newline character, ie:

<br /> for editing .html/.blade.php
\n for when editing .js/.php/.c/.py/etc.
\r\n for when editing .c/.py/etc. on windows
'n when editing .ps (i can’t escape backtick on this forum markdown, somehow broken, it’s a backtick, not an apostrophe)
&#10; for .xml

maybe i’ve missed some

Can i configure this somehow in sublime? I can’t seem to find something like this in either key bindings or settings.

0 Likes

#2

Most straight forward approach would be to define key bindings…

	{
		"keys": ["ctrl+enter"],
		"command": "insert", "args": { "characters": "\\n" },
	},
	{
		"keys": ["ctrl+enter"],
		"command": "insert", "args": { "characters": "'n" },
		"context": [
			{"key": "selector", "operand": "source.ps - source.ps source - source.ps text"}
		],
	},
	{
		"keys": ["ctrl+enter"],
		"command": "insert", "args": { "characters": "<br />" },
		"context": [
			{"key": "selector", "operand": "text.html - text.html source - text.html html"}
		],
	},
	{
		"keys": ["ctrl+enter"],
		"command": "insert", "args": { "characters": "&#10;" },
		"context": [
			{"key": "selector", "operand": "text.xml - text.xml source - text.xml html"}
		],
	},
1 Like

#3

I guess it gets a bit more complicated for embedded languages, because you’d want the highest scoring selector to win, not the last defined thus highest priority keybinding

0 Likes

#4

You are right.

Selectors have been updated to fix embedded code scenarios.

0 Likes

#5

thank you for the reply! it looks good. although, i am super confused about “selector” and “operand”

i guess “selector” means it only selects this shortcut if the stuff in operand matches.

then, what is text.html - text.html source - text.html html?

if i want to create, let’s say, custom selector for .php, i should write “operand” text.php - text.php source - text.php php?

0 Likes

#6

A “selector” is an expression of scopes to be matched against a point or region in a syntax highlighted file, to return whether it is of given scope. see: https://www.sublimetext.com/docs/selectors.html

You can use ctrl+shift+p in an open file to see, which scopes it uses.

PHP files’ main scope is text.html.php, which is already covered. To address <?php...?> you can use source.php - source.php source - source.php text.

- source.php source - source.php text is required to not apply key bindings in embedded syntaxes whithin PHP source code. This may be relevant for Blade or Twig.

0 Likes