Since this thread popped up as the top result when I was googling for “sublime text bring last window to front”, I figured I would add some things I’ve discovered along the way. Not sure when they were added/supported from, but they seem to work as of Sublime Text 4 build 4189.
CLI help:
I always thought it wasn’t possible to pass args to the commands via the CLI --command
, but today I realised it is by just appending the args as a json object after the --command (ensuring that they are all part of the same string arg):
subl --command 'insert {"characters": "Hello, Sublime Text!"}'
We can also seemingly generate these command strings using sublime.format_command
:
If you have the Chain of Command package installed, you can also use this method to run multiple commands together, eg.
subl --command 'chain {"commands": [{"command": "insert", "args": {"characters": "Placeholder text 1\n"}}, {"command": "insert", "args": {"characters": "Placeholder text 2\n"}}]}'
For my specific use case, I was looking to open a new window using --background --command 'new_window'
and then activate just that new window. If I do it without the --background
, then I end up with both the last used window, and my new window activated.
Today I realised I could just run the first command as --background
, then a ‘noop’ type of command without --background
, eg.
subl --background --command 'new_window'
sleep 0.1
subl
So just using subl
by default in Sublime Text 4, it seems to open the existing file rather than another copy of it (unsure if this is a change in default since the last post was made, or a setting I have configured, etc). But if I wanted to do this more manually…
I’m not sure off the top of my head if this is available with existing commands, but I did stumble over some parts of the plugin API that would potentially allow for this:
But most specifically, these ones looks interesting:
Looking at open_file
, it seems that if the file is already open, then it will be brought to the front. At the very least, a custom plugin could be written to use that; or to make use of find_open_file
to manually check if the file is already open then to focus that view.