Skip to content

Instantly share code, notes, and snippets.

@pabloasanchez
Last active December 21, 2020 23:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabloasanchez/f6eb5755b55ed5f4fbe4b43b1805c0ac to your computer and use it in GitHub Desktop.
Save pabloasanchez/f6eb5755b55ed5f4fbe4b43b1805c0ac to your computer and use it in GitHub Desktop.
Sublime Text 3 Plugin: Close all saved tabs
# Place inside Data\Packages\User
# Run: view.run_command("close_saved")
import sublime
import sublime_plugin
class CloseSavedCommand(sublime_plugin.TextCommand):
def run(self, edit):
activeWindow = sublime.active_window()
views = sublime.Window.views(activeWindow)
for each in views:
if each.is_dirty() == False:
print("Dirty: " + str(each.is_dirty()))
print("Closing " + each.file_name())
each.close()
[
{ "caption": "-" },
{ "command": "close_saved", "caption": "Close Saved Files" }
]
@aravind-r-ranganathan
Copy link

Thank you for this handy script. I had to fix a minor bug in the code to make it work.

#  Place the two files - closesaved.py and Tab Context.sublime-menu files inside Data\Packages\User.
# Data\Packages\User folder can be open from Preferences -> Browse packages
# To view the output of the script press Ctrl + `
# To execute the script, right click on any tab and select "Close Saved Files" in the context menu.
# Run: view.run_command("close_saved") 

import sublime
import sublime_plugin


class CloseSavedCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        activeWindow = sublime.active_window()
        views = sublime.Window.views(activeWindow)
        for each in views:
            if each.is_dirty() == False:     
                # print("Dirty: " + str(each.is_dirty())) - DOn't need this as we know that file is not dirty.
                if each.file_name() != None:
                    print("Closing " + each.file_name())
                    each.close()
                	

@pabloasanchez
Copy link
Author

Thank you for this handy script. I had to fix a minor bug in the code to make it work.

You're welcome, friend. I'm glad you found it useful. Also thank you for the bug fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment