CSS Grouping in VS Code with CSS Organizer: Streamline Your Workflow
Tags: css, vscode, cssgroup, automation
Published: 2023-06-09 16:37:58

When working on CSS, it can be challenging to keep track of numerous properties within a single rule. It becomes even more complicated when dealing with complex styles that require many properties.


![CSS Meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1c2t0g55j2ttkw7vu4qq.png)

One popular method that some people use is organizing properties alphabetically. This approach involves arranging the properties in alphabetical order, making it easier to locate specific properties and identify conflicting ones. However, this method may not be suitable for everyone.

One alternative approach, which [Kevin Powell](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjZmY2VsLb_AhXnR2wGHY9OCaoQwqsBegQICRAF&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D3Y03OSNw6zo&usg=AOvVaw0DzD6XmfVO3W_gNAljpMq2) recommends, involves grouping properties based on their functionality and importance. We can group the properties into different categories, such as display, positioning, box model, typography, manipulation, and miscellaneous.

1. Display:
    
    * Property 1
        
    * Property 2
        
2. Positioning:
    
    * Property 1
        
    * Property 2
        
3. Box Model:
    
    * Property 1
        
    * Property 2
        
4. Typography:
    
    * Property 1
        
    * Property 2
        
5. Manipulation:
    
    * Property 1
        
    * Property 2
        
6. Miscellaneous:
    
    * Property 1
        
    * Property 2
        

By grouping the properties this way, it becomes easier to locate and work with specific sets of properties. For example, if you need to modify the box model properties, you can quickly find them in the designated section. This approach reduces cognitive load and streamlines the development process.


![CSS Meme 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfebpey6d1vxr56vz5kx.png)

## How To Automate Grouping of CSS in Visual Studio?

### Step 1: Install CSS Organizer

To begin, you need to install the CSS Organizer extension from the ucsoftware publisher.


![CSS organizer plugin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bc6ob775t6qiyz1b1uhl.png)

### Step 2: Change the settings of CssOrganizer

Once the extension is installed, open the VSCode settings by navigating to File &gt; Preferences &gt; Settings or using the shortcut `Ctrl + ,` (comma). In the search bar of the Settings tab, type "CSS-organizer" to filter the options.

Inside the CSS Organizer settings, you can configure various options. For this automation, we'll go ahead with the following preferences:

* Display Option: Nothing (to avoid any unnecessary visual clutter)
    
* User Grouping File Use: Checked (to utilize the custom grouping file).
    
    
![CSS organizer settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7gzhsaag26tb8enilvr.jpg)

### Step 3: Edit the grouping configuration (optional).

If you want to customize the way CSS properties are grouped, you can modify the grouping configuration. To do this, navigate to the user's extensions directory. The path is typically `%USERPROFILE%\.vscode\extensions\`. Look for a folder named `ucsoftware.css-organizer-version` (the version may vary), and open it. Inside the `resources` folder, you'll find a file named `user-grouping.json`. Open this file in a text editor and make the desired changes to the property orders or add/remove properties as needed. Save the file after making the modifications.

### Step 4: Automatically call the extension's commands on save

To automate the grouping process when saving a CSS file, we'll define a keybinding that triggers the necessary commands. Press Ctrl + Shift + P to open the Command Palette, then type "Open Keyboard Shortcuts (JSON)" and select it. This will open the `keybindings.json` file, where you can define custom keybindings.

Inside the `keybindings.json` file, add the following object to the array:

```json
[...
{
  "command": "runCommands",
  "key": "ctrl+shift+alt+s", // You can choose a different keybinding if desired
  "args": {
    "commands": [
      "editor.action.selectAll",      // Select all the code
      "css-organizer.grouped",        // Call the group command from Css Organizer
      "workbench.action.files.save",  // Save the file
      "cancelSelection"               // Cancel the whole selection
    ]
  },
  "when": "editorTextFocus"
}
]
```

Save the `keybindings.json` file after adding the object.

By following these steps, you have automated the process of grouping CSS in VSCode using the CSS Organizer extension. Now, whenever you save a CSS file, the extension will automatically group the CSS properties based on the defined configuration.


![gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2i0lf6ew8n2d6mcflt8y.gif)

Remember, if you encounter any issues or want to explore more features of the Css Organizer extension, you can refer to the official documentation or the extension's [GitHub repository](https://github.com/ucsw/css-organizer) for additional information.