Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically concatenate strings in javascript

Tags:

sublimetext2

I'd really like to have some automatic string concatenation when I line break in the middle of a string on javascript. I commonly have to break when I reach 80 chars and would like an easy way of doing this.

For example:

var string = '<html>Here is some html in a string so it is going to be very long</html>';

I'd like to just line break in the middle of that and Sublime text to automatically concatenate into

var string = '<html>Here is some html in a string so it is going' +  
    'to be very long</html>';

Is this possible? Is there a plugin?

like image 562
eddiec Avatar asked May 01 '26 13:05

eddiec


2 Answers

You can do this with a macro, below is an example I made earlier.

You can change the width argument to the wrap_lines command if you want to break at something other than 80 characters. You can also substitute a different characters sequence e.g. if you're editing Python and you want to insert triple quotes instead.

Save the script below as a Sublime JSON macro file e.g. split-literal.sublime-macro. In Sublime goto "Preferences->Browse Packages" and then copy the macro file into Packages/User. The macro should now appear in the Tools->Macros menu.

To assign a hotkey to this macro, goto "Preferences->Key Bindings - User" and add the following to the keymap file:

[
    { "keys": ["ctrl+shift+w"], "command": "run_macro_file", "args": {"file":"Packages/User/split-literal.sublime-macro"} }
]

Obviously substitute whatever hotkey you prefer in place of ctrl+shift+w.

Example JSON macro in full:

[
    {
        "args":
        {
            "width": 80
        },
        "command": "wrap_lines"
    },
    {
        "args":
        {
            "by": "lines",
            "forward": false
        },
        "command": "move"
    },
    {
        "args":
        {
            "to": "eol"
        },
        "command": "move_to"
    },
    {
        "args":
        {
            "characters": "' +"
        },
        "command": "insert"
    },
    {
        "args":
        {
            "by": "lines",
            "forward": true
        },
        "command": "move"
    },
    {
        "args":
        {
            "to": "bol"
        },
        "command": "move_to"
    },
    {
        "args":
        {
            "characters": "    '"
        },
        "command": "insert"
    },
    {
        "args":
        {
            "to": "eol"
        },
        "command": "move_to"
    }
]
like image 159
snowcrash09 Avatar answered May 03 '26 08:05

snowcrash09


See the plugin script in this answer which breaks or inserts newlines at the wrap point.

This script obviously needs to check if its operating on strings or not but as a quick thing I tried re-purposing the script to add quotes by changing this line:

v.insert(e, textpos, "'+\n'{0}".format(next_line_indent_string))

but that seems to cause ST2 to hang/crash. If you remove the + character, it inserts quotes, newline quotes fine. I'm afraid this is where my python skills run out.

I'm hoping this would point you in the right direction.

like image 22
Mrchief Avatar answered May 03 '26 06:05

Mrchief



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!