megapode
megapode

Reputation: 75

How do I create a text list of open files in Sublime Text 2

Hi there and thanks in advance for any help on this.

I have tried hunting here and other places on the web but have not found anything to help. If this is obvious or has been asked before then I am sorry.

I am using Sublime Text 2 and I often end up with a load of open files because they meet a criteria that I have been working on.

I would like to list all those files out a new text file, within Sublime text.

Something like: for each open file write filename (or possibly full filepath) Next

I know I can get there from the open files panel but that only lists the files, there is no interaction with it that does what I expected. To thought I might be able to highlight files and use copy and paste to get a list.

Is this a built-in function that I have missed? Is there already a package to do this? Will I have to work out how to write a plug-in to do this?

Once again, many thanks for any and all assistance on this.

(If it makes any difference this is Sublime text 2, mostly on Windows but I do also switch across to Mac regularly for different jobs)

Upvotes: 6

Views: 1467

Answers (2)

Hiren Raiyani
Hiren Raiyani

Reputation: 744

import sublime
import sublime_plugin


class RunningfileCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.window().new_file().insert(edit, 0, "\n".join([view.file_name() for view in sublime.active_window().views() if view and view.file_name()]))
  1. Create file with above code and saved as filelist.py
  2. Open sublime, Goto Preferences -> Select Browse Pacakages
  3. Goto User folder and move above file(filelist.py)
  4. Now goto Preferences -> Keybinding
  5. Add below content on it:

    [{ "keys": ["f12"], "command": "runningfile" }]

  6. Now press f12, you got your all opened files path in new window

Thanks

Upvotes: 2

Iulius Curt
Iulius Curt

Reputation: 5114

Here's a hacky and relatively quick way to get something like that:

  1. Ctrl+Shift+F (This brings up the Find in files dialog)
  2. Make sure the Regex mode is on (.* button) and search after $, for example
  3. You now have a large file with all the content. Hope not too large to slow Sublime down. Now hit Ctrl+F for normal Search dialog
  4. Enter ^_ (caret+space, not underscore) and hit Find All
  5. Ctrl+Shift+K to remove all those content lines

There! Now you have a list of all open files with full path.

More concise:

  • Ctrl+Shift+F
  • .* button is on, search after $
  • Ctrl+F
  • .* button still on, search after ^[space] -> Find All
  • Ctrl+Shift+K

The other way is to make yourself a small plugin to do that for you at a keystroke.

From the SublimeText2 API, you can get some useful methods:

Class sublime.Window.views()    [View]    Returns all open views in the window.

Then

Class sublime.View.file_name()    String    The full name file the file
                                            associated with the buffer, or
                                            None if it doesn't exist on disk.

You can start from an existing plugin. This involves some Python programming skills, though.

Upvotes: 0

Related Questions