2

My goal is to have VSCode insert a timestamp whenever I press Ctrl+Shift+I that looks like this:

[15:03]

Currently the following keybinding gets me almost there:

{
    "key": "ctrl+shift+i",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": {
        "snippet": "[$CURRENT_HOUR:$CURRENT_MINUTE]"
    }
}

The problem is that it is one hour off (it is currently 14:03 on my system clock). That is likely due to some daylight savings time or timezone issue and I don't think that's configurable on VSCode.

I don't really care what happens at midnight, so is there some hacky way of performing some math on these variables? Ideally something like this

{
    "key": "ctrl+shift+i",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": {
        "snippet": "[${$CURRENT_HOUR - 1}:$CURRENT_MINUTE]"
    }
}

Of course, currently this just outputs

[${15-1}:06]
Daniel
  • 678
  • 1
  • 5
  • 20

1 Answers1

1

I just updated an extension, Find and Transform, that can do math on inserted text. With this simple keybinding (in keybindings.json):

{
  "key": "alt+r",                 // choose your keybinding
  "command": "findInCurrentFile",
  "args": {    
    "replace": "[$${ return ${CURRENT_HOUR} - 1 }$$:${CURRENT_MINUTE}]"
}

Since there is no find, it will just insert the replace where your cursor is (as long as that cursor(s) is not in or right next to a word).

This part $${ return ${CURRENT_HOUR} - 1 }$$ indicates a javascript operation $${ <some operation here> }$$ which in this case gets the current hour and subtracts 1 from it.

subtract from hour

You could also use a replace like this:

"replace": [
   "$${",

   "const date = new Date();",
   "return new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'long' }).format(date)",

   "}$$"
]

which would insert this at the cursor:

Tuesday, 22 February 2022 at 00:04:52 GMT-7


Previous answer using a macro extension to run multiple commands:

Generally there is no way to do math on a snippet variable without a truly ugly conditional replacement transform. However, some brilliant guy figured out a way to do simple increment/decrements by 1 (or 10 or 0.1 or multiples and combinations of those!).

See How to increment a variable, like the line number, in a vscode snippet in particular and also Find number and replace + 1. {But no one seems to have noticed.]

In your case, suing the macro extension multi-command put this into your settings.json:

"multiCommand.commands": [

  {
    "command": "multiCommand.decrementHourInSnippet",
    "sequence": [
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "snippet": "[$CURRENT_HOUR"
        }
      },
      "editor.emmet.action.decrementNumberByOne",
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "snippet": ":$CURRENT_MINUTE]"
        }
      }
    ]
  }
]

for the emmet decrementNumberByOne trick to work you can't have two numbers in the line only one of which you want to decremnt. That is why your actual snippet is divided in a half, with the minutes added after the decrement has taken place.

Now, assign your chosen keybinding to this macro, in keybindings.json:

{
  "key": "ctrl+shift+i",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.decrementHourInSnippet" }
}

and it works. Demo:

decrement hour in snippet


Of course, as you said you don't care about going from 0 to -1 which is what I presume it would do after midnight for an hour.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Yes I saw that other stackoverflow thread, but my goal was to do this in a generic way that doesn't involve using an additional extension. Maybe either fix my delay or find a way to do math on the snippets – Daniel Feb 16 '20 at 11:17
  • 1
    No way to do math on snippets other than a very ugly long conditional transform. The only other way would be with an extension so there is no more general way to do what you want. The macro is pretty simple. – Mark Feb 16 '20 at 17:19