Sublime Forum

Cursor position on insert date snippet

#1

I would like to bind a command to get the following string:

-  ] |	{2012-10-05}

(The | represents the position of the caret.)

This can’t be done with a snippet because the date has to be dynamic. I’m no programmer, but I’ve been messing around with the itodo plugin to arrive at what I want:

line_contents = '-  ] \t{%s}' % datetime.now().strftime("%Y-%m-%d")
self.view.insert(edit, line.begin(), line_contents)

This is quite close but I would like the caret to be after the “checkbox” (the square bracekts). Any pointers?

P.S. Sorry if this has been answered before. I recall reading similar questions, but I couldn’t find an answer to the specific issue I’m having.

Edit: added a parenthetical explanation.

0 Likes

#2

I’d probably just insert a dynamic snippet:

view.run_command("insert_snippet", {"contents":'-  ] ${1:something}\t{%s}' % datetime.now().strftime("%Y-%m-%d")})
0 Likes

#3

@quarnster Thank you! That’s even better than what I asked for, because it allows me setup a couple of placeholders as well:

self.view.run_command("insert_snippet", { "contents": "-  ] ${1:Next action} +${2:Project}\t{%s}" %  datetime.date.today().isoformat() } )
0 Likes