Sublime Forum

Is there a way to toggle between the last two files with a single shortcut?


Seems to do what I want but would be better if I could do it with a single shortcut.


  1. To go to the menu Preferences -> Browse Packages...
  2. Create the folder Toggle Last Files
  3. Create the file Toggle Last Files/ and put inside it:
import sublime
import sublime_plugin

isToggled = False

class ToggleBetweenTheLastTwoFilesCommand( sublime_plugin.TextCommand ):

    def run( self, edit ):
        global isToggled
        window = self.view.window()

        if isToggled:
            isToggled = False
            window.run_command( "prev_view_in_stack" )

            isToggled = True
            window.run_command( "next_view_in_stack" )
  1. Create the file Toggle Last Files/Default.sublime-keymap and put inside it:
    { "keys": ["ctrl+shift+o"], "command": "toggle_between_the_last_two_files" }

Then just press ctrl+shift+o and it will toggle between the last two files.

1 Like


Thanks. Though it doesn’t seem to work all the time, sometimes it activates an older window. I guess that’s a problem with the next/prev_view_in_stack. Will investigate some more.

1 Like


Isn’t ctrl+tab what you want? it’s the same as alt+tab on windows, except it’s for tabs.



keyboard: ctrl+pageup & ctrl+pagedown
mouse: back & forward buttons



As pointed, but deleted, it was not working because you need to save the change state per window.

But it is easier just use Ctrl+Tab.



next_view_in_stack (i.e., ctrl+tab) is what you want. Don’t forget to let go of ctrl afterwards though, as it’s only then that the stack gets updated.



It would be nice if there was a preview of the stack while ctrl is hold, just like in windows with alt+tab. I cannot remember the order in which I used my tabs and the lack of a preview makes browsing through the stack feel very random. It wouldn’t need an image, just the tab name (with the folder name appended for files with the same name) would be enough.



I opened a issue for it on the Core Issue tracker:

  1. Core$1789 Preview of the stack while Ctrl on Ctrl+Tab is held down
1 Like


I don’t think it has to do with the state per window as I was using a single window and it jumped to older files instead of toggling only between the last 2 activated files. I’m currently working on a plugin that will keep track of the last activated files manually but I’m currently stuck as I can’t tell whether an active view is the quick panel (and this messes things up) - I don’t have much experience with the Sublime Text API.



Something else, because someone mentioned not holding down the Ctrl key. For the plugin above actually it only worked fine if I held down the Ctrl key; if I released it, the next time I triggered the command it switched me to an older (incorrect) tab.



PS: I figured out how to check if a view is a “standard” view; I can just check whether it’s in self.window.views()

1 Like


I’m not sure if it’s more efficient but I saw a plugin view.settings().('is_widget') If true, it’s not a normal view.

1 Like


You don’t need a plugin, just press ctrl+tab to switch between the two most recently used files



I can’t use Ctrl+Tab, as I’ve reassigned those to:

{ "keys": ["ctrl+tab"], "command": "next_view" },
{ "keys": ["ctrl+shift+tab"], "command": "prev_view" },

because that makes more sense. Now they move to the next/previous tab as seen in the window. Previously the behavior didn’t make much sense to me. Do you know which command was previously assigned to Ctrl+Tab? Just curious if I could do this without the plugin (which by the way is done now and works fine - I will make it public soon).



PS: I’ve removed my ctrl+tab/ctrl+shift+tab keybindings and no, it doesn’t work as I want it. Maybe you meant to use ctrl+tab and then ctrl+shift+tab to move back? This is not what I want, I want a single key combination to toggle between the last two files (which I’ve done with my plugin).


  1. If you press Ctrl+Tab one time, and release the Ctrl and Tab keys, it will switch to the last used tab.
  2. If you press Ctrl+Tab one time, and release the Ctrl and Tab keys, it will switch to the last used tab.

So, you will always be using the some key stroke combination to switch between the last two tabs.

It is not this what you want to?



Indeed seems to work (apparently I didn’t release Ctrl). The command it seems is “next view in stack”. But I will continue to use my plugin as I don’t have to release the Ctrl key for it to work.



OT, but what tool did you use to visualize the pressed keys? I’ve been looking for something like that. Not actively but for quite a while now. (Ideally for linux, too.)



I found it by accident the other day, the programs used are:

  1. (Windows only)
  2. (Windows and Mac only)

For linux you may try:

1 Like