Home Download Buy Blog Forum Support

Automatic line ending shortcut

Automatic line ending shortcut

Postby Thithi on Sat Dec 17, 2011 4:14 pm

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
Code: Select all
var fct = new Fct (|
// some code

And with the addition of "super + shift + enter" having this :
Code: Select all
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
Code: Select all
var fct = new Fct ()
|
// some code


Thanks in advance!
Thithi
 
Posts: 3
Joined: Sat Dec 17, 2011 4:00 pm

Re: Automatic line ending shortcut

Postby C0D312 on Sat Dec 17, 2011 4:45 pm

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):

Code: Select all
[
   {
      "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:
Code: Select all
{
      "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."
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Automatic line ending shortcut

Postby Thithi on Sat Dec 17, 2011 5:01 pm

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.
Thithi
 
Posts: 3
Joined: Sat Dec 17, 2011 4:00 pm


Return to Technical Support

Who is online

Users browsing this forum: Yahoo [Bot] and 18 guests