Sublime Forum

Unsaved TabName Bug?

#1

I just came across this StackOverflow question asking how to name an unsaved tab.

I had previously noticed that sometimes my unsaved tabs would assume a name from the first line of text, while other times it would not.

 
Looking into it further to provide an answer for the question, I found that the first line tabname is unset when altering a document after changing the syntax:

 
Is there any reason for this?  It seems like a bug…

1 Like

Autoset tab name for new (unsaved) files for custom syntax
#2

Similar issue: https://github.com/SublimeTextIssues/Core/issues/1180

1 Like

#3

From FichteFoll on the previously linked issue:

that is the “expected” behavior. Unsaved view names are set in Default/set_unsaved_view_name.py

From set_unsaved_view_name.py:

# Only set the name for plain text files
syntax = view.settings().get('syntax')
if syntax != 'Packages/Text/Plain text.tmLanguage':
    if cur_name:
        # Undo any previous name that was set
        view.settings().erase('auto_name')
        if cur_name == view_name:
            view.set_name("")
    return

So it seems that it is intentional to only work on Plain Text files. But I think it would be better if ST3 unset the name immediately upon the syntax being changed, otherwise it does indeed look like a bug.

1 Like

#4

 
That’s such a weird & specific behavior, I don’t see the benefit…

I know some languages have a mandatory first line, so you might have random code as your unsaved tab title in those scenarios, but I think in most cases it would be useful to maintain the first-line tab naming.  Having any number of untitled tabs is a bad UI move when they could just as easily use an existing feature for differentiation.

It’s easy enough to work around, but it would be nice to just be able to type it in without wondering why it works in some cases and not others ( for people who aren’t digging into the source code ).

###WorkAround:

import sublime, sublime_plugin

class NameUnsavedTabCommand( sublime_plugin.TextCommand ):
	def run( self, edit ):

		window = sublime.active_window()
		file   = self.view.file_name()

		if file == None:
			window.show_input_panel ( "Enter Tab Name", "", get_TabName, None, None )

def get_TabName( userInput ):

	view = sublime.active_window().active_view()

	unsavedTab_Prefix = "* "
	unsavedTab_Suffix = " *"

	view.set_name( unsavedTab_Prefix + userInput + unsavedTab_Suffix )
1 Like

How can I start a new find_in_files tab with the old one exist?
#5

It’s not a bug, it’s a feature. To disable, set_unsaved_view_name to false.
Undocumented.

0 Likes

#6

 
That just disables it completely.

The “bug-like” nature of it is that it only works on plain-text syntax and it requires the document to be altered before the tab name updates in some cases.

1 Like

#7

Works for me.

0 Likes

#8

 
In what context?

Did you try the steps to reproduce as shown in the GIF in the first post?

 

Edit:

Actually, judging by the code in @kingkeith’s post, it shouldn’t work in any case.

0 Likes

#9

This one’s easy. It’s because the tab name is only updated in a on_modified_asnyc callback. That’s why changing syntax does not automatically change the tab name. Adding on_activated_async should help.

0 Likes

#10

I added the code from post 4 to my personal devtools plugin.

Pretty much solves both the on_modified & plaintext issues.

 
The on_modified update seems reasonable, but I don’t get why the default functionality is limited to plaintext.

0 Likes

#11

Oh, by the way, I suggest including the syntax name to be used in the unsaved view name for non-text files. And then some counter, like other editors already do (think untitled1 (Python)).

3 Likes

#12

I was able to resolve this by altering a single line in the set_unsaved_view_name.py file:

if syntax != 'Packages/Text/Plain text.tmLanguage':

changed to:

if syntax != 'Packages/Text/Plain text.tmLanguage' and syntax != 'Packages/User/myCustom.sublime-syntax':`

Adding other syntaxes the same way (using the and operator) will add the auto_name functionality to them as well.

0 Likes

#13

Sorry are you saying your workaround makes the tab title automatically update for non Plain Text syntaxes?

If so, how do I use it?

0 Likes