I create multiple Views view.set_scratch(True)
for a single file
Difficult to close them all manually.
Is it possible to close a View
by name/id/etc?
Is it possible to close a tab?
You can close a view by calling view.close()
, but to do so you need to already have the view
that you want to close.
If you want to close all views that have a specific name, you would do something like:
for view in window.views():
if view.name() == "something":
view.close()
You can also use view.id() == someInteger
if you happen to know the ID values of the views that you want to close.
If you’re going to have a lot of temporary views associated with the same thing that you’d like to close all at once, presumably you don’t want them to all have the same name()
or you won’t be able to tell them apart when you’re working with them.
In such a case, when you create the views you could also do something like the following to tag a view as one that you created:
view.settings().set("magic_setting", True)
Here "magic_setting"
is some string that you would create to uniquely identify views you’re creating (so namespace it like "pluginname.magic_setting"
or something), and the value can be either a simple bool
as here or some other value that also describes the view.
Later on you can use view.settings().has("magic_setting")
to see if a view has the setting, or view.settings().get("magic_setting")
if you want to get at the value and do something specific.
Somehow I get an error trying to execute this code, especially window.views
For window.views
:
AttributeError: ‘module’ object has no attribute ‘window’
or
For sublime.window.views
:
NameError: global name ‘window’ is not defined
I used it in EventListener Class
in on_pre_close
routine. I dont quite understand Python architecture, but I tried sublime.window.views()
and self.window.views()
but both didnt work.
Any thoughts on this?
sublime.windows()
gives you a list of all the windows. sublime.active_window()
gives you the active window. sublime.window
is nothing.
So you probably want sublime.active_window().active_view()
maybe? Or sublime.active_window().views()
and you can find the one you want.
Take a look at the API docs for more info: https://www.sublimetext.com/docs/3/api_reference.html.