Sublime Forum

Is there a default file name plugin?

#1

When I create a new file, I want it to automatically name the file with a default filename, say, “Untitled”. If there is already an “Untitled” than I would like ST to append ‘n+1’ to it.

I have been looking but I havnt been able to find a plugin that does this.

Does anyone know of a plugin that does this, or perhaps does something similar???

Thanks for your help.

0 Likes

#2

I couldn’t find something either, but here you go:

import sublime_plugin

class UntitledRenamer(sublime_plugin.EventListener):
    def on_new(self, view):
        if not view.window():
            return
        views = view.window().views()
        i = ''
        while True:
            for v in views:
                if v.name() == "Untitled{}".format(i):
                    if not i:
                        i = 1
                    else:
                        i += 1
                    break
            else:
                break

        view.set_name("Untitled{}".format(i))
0 Likes

#3

Thank you very much for this.

I have no previous experience of creating ST plugins, but I attempted to create a plugin and this doesn’t work for me.

This is an event listener so apparently does not need any command settings. Is there something I need to do?

0 Likes

#4

No, you just save this as a .py file in your User package and you’re ready to go.

0 Likes

#5

I have done this, but unfortunately it hasn’t worked.

Do I change something within the file itself? For example, I presume I should change format to md if i want it to output to a markdown file?

Or could it be that I need to close down all open tabs?

0 Likes

#6

I do not of anything else that could go wrong here, except wrongly saving the file. Or having the User package disabled, which would be stupid.

Actually, I do have an idea. Are you perhaps running ST2? In that case, they code should yield an exception in the console and you should **always **check that if something isn’t working.

0 Likes

#7

I am pretty sure User package is not disabled as it has, in the user preferences, the theme that I am currently using.

I am using ST2.

thanks for suggesting using the console, it has returned this:

Is this any help to what might be wrong?

0 Likes

#8

I gather there is a problem with the encoding, and not declaring the correct encoding?

0 Likes

#9

Not exactly, that error looks like it’s from a different file or something, because I only used ascii characters in the code - unless you did something to it.

Since you are using ST2, the problem should be the formatting syntax. Use this instead:

[code]import sublime_plugin

class UntitledRenamer(sublime_plugin.EventListener):
def on_new(self, view):
if not view.window():
return
views = view.window().views()
i = ‘’
while True:
for v in views:
if v.name() == “Untitled{1}”.format(i):
if not i:
i = 1
else:
i += 1
break
else:
break

    view.set_name("Untitled{1}".format(i))

[/code]

0 Likes

#10

This hasnt worked either.

Are you sure i dont need to put something in the preferences file??

This is literally the same thing you had before, but with more lines after ‘import sublime text’

When I save the file ‘untitled.py’ a duplicate file ‘untitled.pyc’ appears in the same directory, is this normal??

Thanks for you perseverance, there must be a reason why its not working.

0 Likes

#11

Or perhpas there is a misinterpretation of what this plugin does??

I press ‘Command + N’ and a new tab pops up and automatically is named and saved as ‘Untitled.txt’ or ‘Untitled.md’ or ‘Untitled_2.md’

0 Likes

#12

[quote=“edison”]
Are you sure i dont need to put something in the preferences file??

This is literally the same thing you had before, but with more lines after ‘import sublime text’

When I save the file ‘untitled.py’ a duplicate file ‘untitled.pyc’ appears in the same directory, is this normal??

Thanks for you perseverance, there must be a reason why its not working.[/quote]

Yes, I am sure.

The main thing I changed was {} in the strings to {1}, but I realize now that this is wrong and should have been {0}. Please make this change.

Yes, the .pyc file is normal for ST2.

You should check the console for exceptions/tracebacks from the Python code. Both errors I should have been recorded there and easy to debug if you included them in your posts.

The way I wrote it (which you can easily detect if have only a little bit of programming experience) does not automatically save the file, because I don’t know where. It only sets the tab name, which is equivalent to the default name that the file will have when you press ctrl+s for the first time and open the save as dialog.

0 Likes

#13

[quote=“FichteFoll”]
The way I wrote it (which you can easily detect if have only a little bit of programming experience) does not automatically save the file, because I don’t know where. It only sets the tab name, which is equivalent to the default name that the file will have when you press ctrl+s for the first time and open the save as dialog.[/quote]

Just so I am clear, the default setting of ST is to automatically use the first line of the document as the name of the file. Thats currently what it does on my ST editor.

But the plugin that you have written would automatically name the document “Untitled” ?

0 Likes

#14

Yes

And “Untitled2” if “Untitled” already exists.

0 Likes

#15

Ok thanks. Your plugin has not worked, and so i don’t know what more can be done.

0 Likes

#16

What about this:

import sublime
import sublime_plugin

class UntitledRenamer(sublime_plugin.EventListener):
    def on_new(self, view):
        views = sublime.active_window().views()
        i = ''
        while True:
            for v in views:
                if v.name() == "Untitled{0}".format(i):
                    if not i:
                        i = 1
                    else:
                        i += 1
                    break
            else:
                break
        view.set_name("Untitled{0}".format(i))

(When I briefly tested the other code, if not view.window() always evaluated to True…)

0 Likes

#17

Good thought, QED1224. I didn’t actually test this on ST2 because I am lazy and it is my sincere opinion that** nobody should be using it anymore**, but I do remember now view.window() being very annoying to deal with back in the old days. There’s even an issue for it: github.com/SublimeTextIssues/Core/issues/8

So yeah, this was most likely the problem. Thank you.

0 Likes