Tidy CSS for Notepad++
Asked Answered
E

4

14

Is there some kind of CSS tidy available for Notepad++. I have tried what seems to be every menu item in TEXTFX but none seem to do it. I am used to using PSPAD where you just select the CSS and clicked "Reformat Into Structured CSS"

EDIT made a discovery a long time after asking this question which may help others ... there is a big difference between tidy css and format css. It was actually the latter I was looking for and there are several online systems, for instance http://procssor.com/

Ealing answered 6/8, 2012 at 21:4 Comment(0)
E
1

I have scoured the net and as far as I'm concerned the answer to this question is NO, or at least if there is I certainly cant find it; but if anyone comes up with a better answer I will accept it. For me I will continue to use PsPad for this operation.

Ealing answered 16/8, 2012 at 14:38 Comment(0)
A
30

I was looking for this functionality myself, so I wrote this pair of regex search-and-replace based macros for Notepad++. Format CSS formats a CSS document while Minify CSS makes it smaller.

To install them, locate your shortcuts.xml file, usually at

C:\Users\%username%\AppData\Roaming\Notepad++\shortcuts.xml

and paste the whole block into the <NotepadPlus><Macros> block.

You need to use another editor than Notepad++, OR close Notepad++ by killing the process after you edit this file. Otherwise, Notepad++ will overwrite the file on close.

After installing, you can run those macros from the Macros menu in Notepad++, as well as rename them or assign hotkeys using Edit shortcut/delete macro.

In case someone finds a css for which this script doesn't work, please make a paste and post the link in the comments so I can look at it.

Macro source XML

<Macro name="Format CSS" Ctrl="no" Alt="no" Shift="no" Key="0">
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="([^\r\n;])\}(?![\r\n])" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="\1;\r\n}\r\n" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="([^\r\n])\}(?![\r\n])" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="\1\r\n}\r\n" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="([^\r\n])\{(?![\r\n])" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="\1\r\n{\r\n" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam=";(?![\r\n])" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam=";\r\n" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="(^(?![ \t]).+;)" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="    \1" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="\}(?!(\r?\n\r?\n))" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="}\r\n" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam=":(?![ ])(?=.+;)" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam=": " />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
</Macro>
<Macro name="Minify CSS" Ctrl="no" Alt="no" Shift="no" Key="0">
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="[\r\n\t ]+" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam=" " />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="[ ]*\}[ ]*" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="}" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="[ ]*\{[ ]*" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="{" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="[ ]*;[ ]*" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam=";" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam=";\}" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="}" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="[ ]*:[ ]*" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam=":" />
    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
</Macro>
Allhallows answered 28/5, 2015 at 16:26 Comment(7)
Brilliant. There are a few errors though and I don't know enough about how this works to fix it. Mostly has to do with spacing. It also inserted an extra semicolon somehow. I wish I StackOverflow had a better commenting system so I could show you.Dyspeptic
@DerekZiemba you can make a paste and post the link here. How this works is: the message="1601" fields are the "find" regex, the 1602 fields are the "replace" expression. Each search and replace operation is one whole block 17001701, and the blocks are executed top to bottom, each doing a global search and replace on the file.Allhallows
Great! Using the macro it took 10 seconds to reformat 50K lines of css code.Weighbridge
Very fast, even Aptana Studio failed to format the source, this macro did, Thank you!Anastigmat
Here is a test file for some problems with @-rules, comments (missing linebreak after comment) and url(data) terms.Inutility
@Inutility Thanks. Unfortunately, as soon as nesting scopes are introduced, things get extremely difficult to handle with regexes. Will probably not fix that.Allhallows
Just here to say how great this macro is.Garrott
A
4

What about JSFormat function of the JSTool plugin? I know it is unexpected, the name refers to Javascript. It has a format and a minification functionality that work on css too.

Amenra answered 3/8, 2016 at 6:50 Comment(1)
works, but you need to do some cleaning after. described here: Notepad++ plugin for auto format Css - Stack OverflowAmey
E
1

I have scoured the net and as far as I'm concerned the answer to this question is NO, or at least if there is I certainly cant find it; but if anyone comes up with a better answer I will accept it. For me I will continue to use PsPad for this operation.

Ealing answered 16/8, 2012 at 14:38 Comment(0)
S
-1

There's a list of plugins for Notepad++ out here: http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Plugin_Central

Just do a search for CSS and try out those plugins. I don't use such a plugin, so no recommendation here.

Maybe CSS-eXplorer or jN?

Skiplane answered 7/8, 2012 at 13:54 Comment(1)
Thanks for the effort but I don't think either of those or anything else on that page will work TBHEaling

© 2022 - 2024 — McMap. All rights reserved.