Sublime Forum

CSS Prettify, Specifying Single Line Rules

#1

I really love the magic the CSS Prettify tools.maxcdn.com/process does it.

For example:

[code]footer .bottom ul.bugs {
    text-align: left;
  }[/code]

Should re-format to:

[code]footer .bottom ul.bugs { text-align: left }[/code]

Thank you!

0 Likes

#2

Quick solution:

import sublime
import sublime_plugin
import re

class UniteInOneLineCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sel = self.view.sel()
        if not sel:
            return
        for region in sel:
            text = self.view.substr(region)
            text = re.sub(r'\n|\t', '', text)
            text = re.sub(r'\{\s*', '{ ', re.sub(r'\s*\}$', ' }', text))
            self.view.replace(edit, region, text)

Bind the command “unite_in_one_line” to a hotkey or insert this to an menu.
Select the related lines and run the command.

Edit: Now I’ve changed the example to work also with multiple selections.

0 Likes

#3

I’ve never created a custom hotkey or menu item, where do I enter the code you’ve given me?

Thanks

0 Likes

#4

Open a new file, put the code into this file and store it as
portable version:
“…\Data\Packages\MyPlugin\MyPlugin.py”
installed version:
“%USER%\AppData\Roaming\Sublime Text 3\Packages\User\MyPlugin\MyPlugin.py”
(any other name can used, but folder and file should has the same title)
After a restart the command “unite_in_one_line” is available.
In your user keybindings you can create a new hotkey:
{ “keys”: “YOUR_SEQUENCE”], “command”: “unite_in_one_line” }

Have a look to the system keybindings first, to avoid doubles.

Add a menu item:


    {
        "caption": "MyTool",
        "mnemonic": "Y",
        "id": "my_tool",
        "children":
        
            {"caption": "Unite in one line", "command": "unite_in_one_line"}
        ]
    }
]

Save this code as “Main.sublime-menu” in the same folder like your PlugIn above.
All menu-files in ST will merged by starting ST and will shown in the related menu. This is a first level menu and will shown in the menu bar.
If you like to insert your command in an existing menu, use the id of this without caption and mnemonic.

0 Likes