Welcome, guest | Sign In | My Account | Store | Cart

Put your cursor on a CSS line that ends with a value in px (do not select any text). Run the script and it will insert a new line there the px value is converted to a rem value.

You need to edit the code to modify your rem base matching the one you use in your own CSS. The default value is 16.

Example: font-size: 12px; (run the script with the cursor in this line) font-size: 0.75rem (new line added by script)

JavaScript, 24 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }
ko.commands.doCommand('cmd_lineDuplicate')
ko.commands.doCommand('cmd_end')
ko.commands.doCommand('cmd_lineNext')
ko.commands.doCommand('cmd_left')
ko.commands.doCommand('cmd_selectWordLeft')

// Skip the code above if you dont whant the script to insert a new line. It will then simply convert the selected value to rem. Select 12px and it will be converted to 0,75rem. 

var koedit = ko.views.manager.currentView.scimoz;

    if ( !!koedit.selText ) {
        var newText = koedit.selText;
        
        var rem_base = 16; //Replace the value here to the rem base you have in your own CSS file!
        
        newText = newText.replace("px", ""); // Removes the px prefix
        newText = parseInt(newText); //Convert the string to a int 
        newText = newText / rem_base; //Convert to rem value
        newText = newText.toFixed(9); //Shorten the rem value to 9 decimals 
        
        koedit.replaceSel( newText + "rem" ); //Return the new value with a rem prefix
    }
Created by Magnus Axelsson on Wed, 20 Nov 2013 (MIT)
JavaScript recipes (69)
Magnus Axelsson's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks