Question

I am trying to set up a snippet in Sublime Text 2 that will expand to the following:

/**
* @version   $Id: ${1:current_file_name.extension} ${2:random_4_digit_number} ${3:YYYY-MM-DD} ${4:time_in_UTC_24} ${5:current_logged-in_user} $
* @author    Company http://example.com
* @copyright Copyright (C) 2007 - ${6:current_year} Company
* @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
*/

The above snippet has cursor stops. If all the data can be automated, then I wouldn't need any cursor stops.

The stops map as follows:

${1:current_file_name.extension}

Automatically pastes the name of the current file being edited.

${2:random_4_digit_number}

A randomly-generated number from 0000 to 9999

${3:YYYY-MM-DD}

The current date using - separator.

${4:time_in_UTC_24}

The current time in UTC 24-hour format including seconds using : separator.

${5:current_logged-in_user}

The currently logged in user

${6:current_year}

The current year

Any advice or help would be greatly appreciated.

Was it helpful?

Solution

It is probably not possible with a snippet, however I wrote a plugin to accomplish what you want. In Sublime, click Tools > New Plugin. Replace the example code with the following. Name it "add_license_stamp.py" and save it in your Packages folder (not in Packages/User). Also, add a keybinding in your keymap file. To run the command, place your cursor where you want it and press the keybinding:

Keybinding:

{ "keys": ["ctrl+shift+9"], "command": "add_license_stamp" }

Plugin:

import sublime, sublime_plugin
import os
import datetime
import random
import getpass

''' Add license stamp
/**
* @version   $Id: ${1:current_file_name.extension} ${2:random_4_digit_number} ${3:YYYY-MM-DD} ${4:time_in_UTC_24} ${5:current_logged-in_user} $
* @author    Company http://example.com
* @copyright Copyright (C) 2007 - ${6:current_year} Company
* @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
*/
'''


class AddLicenseStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        company_name = "BobCo"
        company_site = "http://bobco.com"

        file_path = self.view.file_name()
        file_name = os.path.basename(file_path)
        year = datetime.datetime.utcnow().strftime("%Y")
        date_time = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
        random_number = str(random.randrange(0000, 9999)).zfill(4)
        user = getpass.getuser()

        license = "/**\n"
        license += "* @version   $Id: " + file_name + " " + random_number + " " + date_time + " " + user + " $\n"
        license += "* @author    " + company_name + " " + company_site + "\n"
        license += "* @copyright Copyright (C) 2007 - " + year + " " + company_name + "\n"
        license += "* @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only\n"
        license += "*/\n"

        self.view.replace(edit, self.view.sel()[0], license)

(note: python requires a blank line after your code)

Replace "BobCo" with your company name. I'm not sure of the best way to get the current user name, I used this question: Is there a portable way to get the current username in Python? . They say it is compatible with the major OSes. If not do something similar to how I did the company name. And manually set it per user. Also, I don't know what UTC 24-hour format is. But I just used the time in 24 hour format.

Edit

I changed now() to utcnow() to get the utc date/time. I added date/time formatting. I added zfill(4) to the random number to pad it with zeros if under 4 digits. You can highlight the current stamp and hit the key binding to update it. You could also get fancy and automatically replace on save, but beyond the current scope. You would have to use a regex to find the current stamp. Then activate the script upon save instead of run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top