I'm used to press ctrl+shift+t to get a closed tab back in my browser that's why I did the same thing in sublime text and since there was no such keybinding available I implemented my own plugin and added a keymap for it.

This plugin allows you to reopen closed files.You can open more then one closed file by repeating the command.

Changelog v1.0.2:Fixed not using package folder to save tmp file when --data commandline option is used

I'm being really dumb, but how do you setup this plugin? I've yet to add any plugins to Sublime so all I did was select "New Plugin" from the menu and pasted the code in, saved the file then setup the key binding as mentioned in the example but it does not seem to run.

Your user key bindings are probably the older style. Throw this in there:

{ "keys": "ctrl+shift+t"], "command": "open_last_closed_file" }

...and I had to create a file in User called "last_closed_file.path" before it started to work.

I noticed something though, at least with dresende's latest version-- once a closed file is re-opened, it will start to walk the directory up each time you invoke the plugin. That is, say I close this file:

Inspired by dresende's code, but not quite content, I implemented a slightly enhanced version of this plugin. This plugin keeps track of both closed files as well as the files that have been opened. The default behavior of the plugin is to open the most recently closed file, but it can also provide access to a searchable quick panel with the recently closed files followed by a history of the files that have been opened. This can be helpful when using multiple projects or when wanting to reopen a file that you closed a few hours or days ago.

The implementation uses the sublime.Settings API methods to store and cache the file open/close history. The history file is saved in the User sub-directory of the Packages directory (as History.sublime-settings). The history is formatted in JSON like the rest of the sublime settings.

import sublime, sublime_plugin
import os
HISTORY_SETTINGS_FILE = "History.sublime-settings"
HISTORY_MAX_ENTRIES=500
def get_history(setting_name):
"""load the history using sublime's built-in functionality for accessing settings"""
history = sublime.load_settings(HISTORY_SETTINGS_FILE)
if history.has(setting_name):
return history.get(setting_name)
else:
return ]
def set_history(setting_name, setting_values):
"""save the history using sublime's built-in functionality for accessing settings"""
history = sublime.load_settings(HISTORY_SETTINGS_FILE)
history.set(setting_name, setting_values)
sublime.save_settings(HISTORY_SETTINGS_FILE)
class OpenRecentlyClosedFileEvent(sublime_plugin.EventListener):
"""class to keep a history of the files that have been opened and closed"""
def on_close(self, view):
self.add_to_history(view, "closed", "opened")
def on_load(self, view):
self.add_to_history(view, "opened", "closed")
def add_to_history(self, view, add_to_setting, remove_from_setting):
filename = os.path.normpath(view.file_name())
if filename != None:
add_to_list = get_history(add_to_setting)
remove_from_list = get_history(remove_from_setting)
# remove this file from both of the lists
while filename in remove_from_list:
remove_from_list.remove(filename)
while filename in add_to_list:
add_to_list.remove(filename)
# add this file to the top of the "add_to_list" (but only if the file actually exists)
if os.path.exists(filename):
add_to_list.insert(0, filename)
# write the history back (making sure to limit the length of the histories)
set_history(add_to_setting, add_to_list[0:HISTORY_MAX_ENTRIES])
set_history(remove_from_setting, remove_from_list[0:HISTORY_MAX_ENTRIES])
class OpenRecentlyClosedFileCommand(sublime_plugin.WindowCommand):
"""class to either open the last closed file or show a quick panel with the file access history (closed files first)"""
def run(self, show_quick_panel=False):
self.reload_history()
if show_quick_panel:
self.window.show_quick_panel(self.file_list, self.open_file)
else:
self.open_file(0)
def reload_history(self):
history = sublime.load_settings(HISTORY_SETTINGS_FILE)
self.file_list = get_history("closed") + get_history("opened")
def open_file(self, index):
if index >= 0 and len(self.file_list) > index:
self.window.open_file(self.file_list[index])

I added an updated version and uploaded it to github. Unfortunately commiting to github is blocked from my work so I had to add it as a gist.

The updated version now keeps both a global and a per-project history. This allows you to access the file history specific to the active project or the file history across all projects. I find this extremely helpful when opening a project after a few days and forgetting the name of a file that I closed when I last accessed that project.