Sublime
1
Datetime plugin
In sublime, I need to print current date and time with a custom shortcut key.
To set up a custom shortcut key in Sublime Text to print the current date and time, you'll need to create a new command and then bind that command to a key combination. Here's a step-by-step guide:
Create a new plugin:
Open Sublime Text.
Go to
Tools>Developer>New Plugin....Replace the default content with the following script:
import sublime import sublime_plugin from datetime import datetime class InsertDatetimeCommand(sublime_plugin.TextCommand): def run(self, edit): current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.view.run_command("insert", {"characters": current_datetime})Save this file in the
Packages/Userdirectory (Sublime Text will suggest the correct directory) with a name likeinsert_datetime.py.
Add a key binding:
Go to
Preferences>Key Bindings.In the right-hand user key bindings file, add a new key binding for the custom command. For example:
[ { "keys": ["ctrl+shift+q"], // Choose your preferred key combination "command": "insert_datetime" } ]Make sure to place this entry within the array brackets.
Test the new shortcut:
Open a new file or an existing one.
Press the key combination you set (e.g.,
Ctrl+Shift+Q).The current date and time should be inserted at the cursor position.
With these steps, you will have created a custom command in Sublime Text that inserts the current date and time when you press your chosen key combination.
Last updated
Was this helpful?