Sublime Forum

Request: Don't prompt to save empty "untitled" tabs


Sometimes I use ST as a go-between for modifying some text from one application in order to paste it into another application. When I do this, I create a New “untitled” and empty document, use ST’s handy tools to modify the text, then Ctrl+A and Ctrl+X to clear the buffer and put everything into the clipboard.

After I do this and try to close the “untitled*” tab, ST thinks it has been modified and asks me if I would like to save it. Of course it has been modified, but it is also empty. What use could I possibly have to name it and keep it if I had not already done so?

My previous editor, Metapad, would only prompt to save the untitled file if it contained text, otherwise it would just close the window. I don’t know how other editors behave regarding this, but I grew very fond of this behaviour :smile:



Interesting. makes sense :smile:
Personally I still like it to ask me because I use a lot of untitled windows for notes and stuff like that and if I accidentally close them I would be very sad lol



But he’s talking about empty untitled tabs, so you wouldn’t lose your work anyways. I think it makes sense



It’s a +1 from me for this functionality for empty documents

Notepad++ also exhibits this behaviour




true, I think it could be done with a plugin hooking in presave and check if the view is empty then just close the damn thing…

EDIT: I mean **onClose **oops :stuck_out_tongue:; anyways tinkering around with onClose was total fail because the prompt to save is triggered before onClose hook gets called. So to do what you want I guess we would have to get a lil clever and just hook onModified and check if the view is empty then make it a scratch (which won’t ever ask to save) if it’s not empty then just do normal prompt before closing. So if your view has any non-whitespace character it will ask to save else close that damn thing.

import sublime, sublimeplugin class noEmptyTabs(sublimeplugin.Plugin): def onModified(self, view): #we only check empty tabs... if(view.fileName() == None): view_has_content = view.findAll('\S') if(len(view_has_content) <= 0): view.setScratch(True) else: view.setScratch(False)



Thanks very much! Works great so far :smile: Will let you know if I run into any troubles.



The problem is that if you have a genuine scratch buffer, the plugin will remove its scratch flag.

I think using instead of view.fileName() will fix this, as long as scratch buffers are named.



Here’s another request related to this…

If I don’t have any tabs open in Sublime, and I have text in the clipboard, if I hit CTRL+V, a new untitled tab should open, and the contents of the clipboard should be pasted into it.

I tried seeing if I could make this into a keybinding, but there doesn’t seem to be a context for “noViewsOpen” nor the ability to string two commands (new + paste) into one binding. I guess a plugin could help here? Sorry for being such a bother, but my specialties are Javascript and PHP, not Python. :blush:

As an aside… It would be good if there were a lot more key contexts available to use (such as noViewsOpen), as well as allowing multiple commands in a single keybinding, perhaps separated with a pipe (|). Opera, for instance, has two types of separators for custom keyboard shortcuts, ampersand (&) and pipe (|). Commands separated with ampersand are sent immediately one after the other, while the pipe separator indicates that the program should wait until all actions associated with the first command are done, then execute the second command. It is a useful system, I think :smile:



[quote=“gpfsmurf”]The problem is that if you have a genuine scratch buffer, the plugin will remove its scratch flag.

I think using instead of view.fileName() will fix this, as long as scratch buffers are named.[/quote]

True good find :smiley: didn’t think of that. I personally barely use scratch buffers and if I do I would name them too.

class newPaste(sublimeplugin.WindowCommand): def run(self, window, args): if(len(window.views()) == 0 and sublime.getClipboard() != ''): window.newFile() view = window.activeView() view.insert(view.size(), sublime.getClipboard()) else: window.runCommand('paste')

bind this to ctrl+v it wont interfere with normal paste functionality.

BTW, You can do your own “contexts” with view.options().set() but if you have no views open at all the whole context=“option” in keymap files is useless since from what it seems it only works when there’s actually view(s) open.
Also only way to run multiple commands with one command is to do a custom command (plugin) and in your code to runCommand(“blah”); or call in a function which does that. In your case, runCommand(‘paste’) but we also have sublime.getClipboard() which is more efficient IMO



You can also use the “sequence” command, or create a macro



It’s really annoying. The untitled buffer is always dirty, and when you “save all” you have to get rid of it, so that “save all” can proceed.



I guess it makes sense that save_all would behave differently from closing the empty tab