Sublime Forum

Auto-open .css file when opening .scss file


Hey guys,

I’m looking for a package/plugin that will automagically open up the corresponding css file when I open up an scss file.

I use CodeKit to compile and save the /scss into the /css folder.

On my old Mac, I had it somehow set up that every time I opened say: scss/home.scss, my css/home.css file would also automatically open into another tab and monitoring would be turned on by default.

All I’d have to do is open my scss file and make changes and save and then codekit and sftp monitoring would do the rest of the magic.

I have looked over my old Mac set up but cannot determine how it was achieving this automatic opening of css files for me.

Any help is greatly appreciated.




Assuming you have a directory structure like the following :-

│       main.css

Then the simplest way to solve your problem would be to use the following keybinding.

	"keys": ["ctrl+alt+shift+t"],
	"command": "open_file",
	"args": {
		"file": "../css/${file_base_name}.css",

This assumes that you already have a css/main.css. If not it will create, the key binding will create it then.
Pressing this key bindings when the current file is main.scss, should open the corresponding css/main.css file.

Of course, this is not automagic (you would require a plugin for that), but it is simple.



Thanks for the reply mate. I’ve never played with keybindings before, but I’ll give this a go. I appreciate the tip.

However I would kill to know how my old setup was able to do what it was doing. I cannot find anything in the Package Control list of packages that different to my new setup. Go figure?



I am not aware of a package/plugin that does this to be honest, but writing a plugin for this should be trivial. Let me see if I can whip one up.



Wow OK.

Well if that’s the case, the desired/ideal functionality is:

  1. When I open xxx.scss, sublime will open …/css/xxx.css,

  2. turns on sftp monitoring for the css file,

  3. Makes the scss file tab the currently active tab rather than the css file which isn’t going to be manually edited since CodeKit will compile this. (this last part wasn’t something it did before but I really wished it did!)

If you come up with something, you most definitely should share it as this workflow saves numerous extra manual openings and monitoring of files.




Here is something I quickly came up with.

import sublime
import sublime_plugin
import os

class OpenCSSAutoMagicListener(sublime_plugin.EventListener):

	def on_load_async(self, view):
		scss_file = view.file_name()
		if scss_file.endswith(".scss"):
			target_css_file = os.path.join(os.path.dirname(os.path.dirname(scss_file)), "css", os.path.splitext(os.path.basename(scss_file))[0] + ".css")
			if not os.path.exists(target_css_file):

To use this, just copy the code and put it in a python file (The name can be anything you want, e.g. and then put the file in the User directory (Preferences -> Browse Packages from main menu to get to User).

Now, whenever you open a scss file (e.g. /my_website/scss/main.scss), it will open the corresponding css file (e.g. /my_website/css/main.css) if it exists. And the focus is kept on the scss file.

I am not sure what sftp monitoring means, sorry.

Again, this will only work with the directory structure I posted previously.



Wow! Thank you. I will give this a go now.

SFTP is a plugin/package that provides FTP and remote server features. “Monitoring” listens for external changes/saves to a file (the css file), and automatically uploads them to the FTP.

So when you save changes to the SCSS file, CodeKit compiles it and overwrites/saves the CSS file, which triggers SFTP to upload the CSS file.

To manually tell SFTP to monitor a file and listen for changes you have to press Command+Control+U,M while in the CSS file.



I have only ever used node-sass before from the cmd line to compile sass files so I am not sure how Codekit works. But if it has a command line option to compile sass files, then compiling sass -> css on save should be easy.



I believe this should do the trick:


… run on the css file.