Sublime Forum

Is there a way to navigate your open files using the keyboard?

#1

What I’m looking for is a keyboard shortcut that opens a list (alphabetical would be great) of the files you have open. Then I could bring that file’s tab into focus by either 1) using the up/down arrows to select the file and hitting enter, or 2) typing a portion of the file name until it is highlighted, then hitting enter.

So, for example,I have three files open: carrot.txt, apple.text, and banana.txt.

Hitting this magic keyboard shortcut brings up the three files in an alpabetized list:

apple.text
banana.txt
carrot.txt

I want to open carrot.txt, so I type “ar” (which selects carrot.txt) and hit enter. Or, alternatively, I hit the down arrow twice and hit enter.

I don’t think this functionality exists natively in Sublime. Is there a plugin that can do it?

1 Like

#2

I believe you’re looking for ctrl+p which opens a listing of files in your project overall, but it lists the currently open files at the very top allowing you to use the arrow keys to select which one you would like to view/edit.

in any case, you can just type in the name of the file you’re looking for and it will come up.

2 Likes

#3

Thanks! I use ctrl+p all the time, and until you said it I never noticed that it showed the open files near the top!

That’s a pretty good solution. I’d still like something that just shows the opened files, though. Visually it would be a cleaner interface for the workflow I’d like.

Does the command behind ctrl-p take arguments?

0 Likes

#4

I did a bit of googling and cobbled together what I want in a simple plugin. Here it is in case anyone is interested:

import sublime
import sublime_plugin


class NavigateOpenFilesCommand(sublime_plugin.TextCommand):

    def on_done(self, index):
        # If user cancels, index returned is -1. Do nothing
        if index == -1:
            return

        # If user picks from list, open the view associated with that file
        id = sublime.active_window().find_open_file(self.file_names[index])
        sublime.active_window().focus_view(id)

    def run(self, edit):
        views = sublime.active_window().views()

        self.file_names = []
        for view in views:
            self.file_names.append(view.file_name())

        self.file_names = sorted(self.file_names)

        self.view.window().show_quick_panel(sorted(self.file_names), self.on_done, 1)

To give credit, this was put together using code from:

and

0 Likes

#5

That’s a nice example that reminds me I don’t have to always find an existing plugin and avoid just writing one the will do what I need right then and there.

I’ve been using https://packagecontrol.io/packages/ExtendedTabSwitcher for this sort of tab listing.

1 Like

#6

Creating your own plugin is certainly great, but I’d like to bring up another option.

I usually hide the sidebar (ctrl+k, ctrl+b) and don’t see this, but if you display the sidebar the top portion of it can show open files before showing your projet or folder.

Just activate the feature by going to View -> Side Bar -> Show Open Files

0 Likes

#7

ahh, cool trick. thanks!

Is there a way to navigate the sidebar with the keyboard?

0 Likes

#8

Ctrl+0 gives the keyboard focus to the side bar if it’s open (use Ctrl+KCtrl+B to toggle the state if it’s not open, and use Command in place of Ctrl for the sidebar toggle keys on a Mac).

Once in the side bar, Up/Down/PageUp/PageDown move through the files and folders in the side bar, including the open files section, if you have that turned on. Home and End jump to the extremes of the list.

The Left and Right keys fold and unfold folders when the selection is on them, while Left on a file will jump the selection to the folder that it’s contained in and Enter will open the file or focus the already open copy.

3 Likes

#9

There is an existing package which does this, it is not maintained anymore. But the fork below from evandrocoan has all the updates. This works great, I have been using this over the last 2 years and fixed some edge cases.

0 Likes