As you’ve outlined it in your message, this should work for you. Here I’ve redacted the commented out lines just to make the image as small as possible:

The code as you presented it above is not properly formatted python code (python is sensitive to indentation), so if that’s what the code you have currently looks like, that is the reason why it doesn’t work for you. In that case, you want to format it more the way you see here.
You may also want to verify that you’ve stored the code in the correct directory (i.e. User
sounds more correct than ..\User
, although it’s hard to tell from the way you’ve described it). Your User
package is available by selecting Preferences > Browse Packages...
from the menu and then going inside of the User
folder that you see there.
When in doubt, if you select Tools > Developer > New Plugin...
from the menu, Sublime will create an empty plugin for you, and will automatically save the file into the correct location for you when you save it.
As mentioned, the code that you have posted (except for the formatting) should work properly. There are some problems with the commented out lines of code, though:
now = datetime.datetime.now()
self.view.replace(edit, s, now.strftime("%Y%m%dT%H%M%S"))
self.view.replace(edit, s, str(datetime.date.today())
These lines won’t work as written because datetime
isn’t known; for these to work, you need to add datetime
to the list of items being imported at the top of the plugin, so that it knows what a datetime
is.
Also, the last line has more open parentheses than it does close parentheses, which makes the compiler mad. To fix that you need to add another one to the end of the line:
import sublime, sublime_plugin, time, datetime
self.view.replace(edit, s, str(datetime.date.today()))
When working with creating your own plugin code, it’s a good idea to check out the Sublime console when things don’t seem to be working the way you expect them to. If anything is wrong with the code, it will show up there. You can view the console by pressing Ctrl+` or by selecting View > Show Console
from the menu.
Sublime will reload the file as soon as you save it (this is also mentioned in the console when it happens), so you can tell right away if there is anything structurally wrong with the code (if you don’t see it saying it is reloading the plugin, you have it saved in the wrong location).
If that doesn’t trigger an error, you may still see one when you actually press the key, because code that is syntactically correct is not necessarily logically correct.