Sublime Forum

Automatic line ending shortcut

#1

Hi there,

I spend most of my time writing JavaScript and was one of the feature that really miss me in ST2 available in TM is related to auto pairing.http://manual.macromates.com/en/working_with_text.html#auto-paired_characters_quotes_etc

I want to know if there is a way to have end of line behavior like described above, just having to type

var fct = new Fct (|
// some code

And with the addition of “super + shift + enter” having this :

var fct = new Fct ();
|
// some code

For the moment, the only thing I’ve found is “super + enter” which leads to something worse in most cases

var fct = new Fct ()
|
// some code

Thanks in advance!

0 Likes

#2

This is relatively easy to do with a macro. Add this macro file to your user directory of Sublime’s Packages (Preferences > Browse Packages > User):

	{
		"args":
		{
			"by": "wordends",
			"forward": true
		},
		"command": "move"
	},
	{
		"args":
		{
			"characters": ";"
		},
		"command": "insert"
	},
	{
		"args":
		{
			"characters": "\n"
		},
		"command": "insert"
	}
]

I saved the macro file as auto-line-ending-js.sublime-macro but you can call it whatever you want.

Next, we need to bind that macro to a keyboard shortcut. Open up your “User Keybindings” (Command Palette > type “User Keybindings”). Add the following to your keybinding list:

{ "keys": "super+shift+enter"], "command": "run_macro_file", "args": {"file": "Packages/User/auto-line-ending-js.sublime-macro"}, "context": { "key": "selector", "operator": "equal", "operand": "source.js" } ] }

Hope that helps!

EDIT: BTW, this expects that you have autoclose brackets turned on. Therefore, when you press (, sublime auto-inserts the closing “)”. Macros are powerful and very easy to make, so you can tweak it to your heart’s delight. The easiest way to add them is to record them: “Tools > Record Macro.”

0 Likes

#3

Thanks you so much, this works exactly as I would expect!

I was searching more or less in this direction, but since I am really new to sublime text, I wasn’t sure that something was already built-in.

It helped a LOT.

0 Likes