sublime

for mac key :

In Sublime Text, you can create a custom key binding to insert a template text when you press a specific key combination like Cmd+Shift+D. Here’s how you can achieve that:

Step-by-Step Plugin Creation

  1. Go to Tools -> Developer -> New Plugin.

  2. Replace the content with the following code:

import sublime
import sublime_plugin
from datetime import datetime

class InsertCustomTextCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # Generate the current date information
        now = datetime.now()
        date_str = now.strftime("%B %d %Y")
        day_name = now.strftime("%A")
        day_num = (now - datetime(now.year, 1, 1)).days + 1

        # Create the template text
        template = f"""
-------------------------------------------------------------------------------------------------------------------
Day # {day_num} {date_str} - {day_name}
---------------------------------------








-------------/
"""
        # Insert the text at the current cursor position
        self.view.insert(edit, self.view.sel()[0].begin(), template)
  1. Save the file with a .py extension, for example, insert_custom_text.py.

  2. Bind this command to the key combination:

    • Go back to Preferences -> Key Bindings.

    • Add the following to your custom key bindings file:

Now, when you press Cmd+Shift+D, the dynamic date will be inserted into your file.


2

date and time

To achieve this, you can create a custom Sublime Text plugin that inserts the current date and time when you press Cmd+K. Here's how to do it:

Step-by-Step Plugin Creation

  1. Open Sublime Text.

  2. Go to Tools -> Developer -> New Plugin.

  3. Replace the content with the following code:

  1. Save the file with a .py extension, for example, insert_date_time.py.

Key Binding

  1. Go to Preferences -> Key Bindings. This will open two files: the default key bindings on the left and your custom key bindings on the right.

  2. Add a new key binding to the right file. Here’s an example configuration:

Now, when you press Cmd+K, the current date and time will be inserted at your cursor's position in the format (2024-07-25 17:56:28).


3

To create a Sublime Text plugin that inserts the exact date and time format you provided, follow these steps:

Plugin Code

  1. Create the Plugin:

    • Go to Tools > Developer > New Plugin… in Sublime Text.

    • Replace the default content with the following Python code:

  2. Save the Plugin:

    • Save this file in your Sublime Text Packages/User directory with a name like insert_formatted_date.py.

  3. Create a Key Binding:

    • Go to Preferences > Key Bindings.

    • Add a new key binding entry to call this plugin, like this:

How It Works

  • Day of Year Calculation: now.strftime('%j') is used to get the day number of the year.

  • Date and Time Formatting: The strftime function is used to format the date and time as specified in your example.

This setup will insert the exact format you need when you press cmd+shift+d in Sublime Text.

Last updated