Xerus Lord
Xerus Lord

Reputation: 63

VS Code snippet date signature thing

I want to input the keystrokes wpli20250305 and have it transformed into wpli(2025-03-05). I've been trying to get into the habit of dating my comments and hoped a snippet would help.

Initially, I have this snippet:

    "date": {
        "prefix": "date",
        "body": "(20${1:$CURRENT_YEAR_SHORT}-${2:$CURRENT_MONTH}-${3:$CURRENT_DATE})",
        "description": "Insert a date in ISO format.",
    },

Since I know each placeholder will, in 99.9% of cases, have only 2 digits. Wouldn't it be great if the cursor jumped to the next placeholder automatically after typing the 2nd character? So I searched around a bit for terms like snippet auto tab and nothing really came up.

I thought of trying a string transformation. After some tinkering, I ended up with something like:

    "overengineered": {
        "prefix": "jj",
        "body": "${TM_CURRENT_WORD/(\\w+)(\\d{4})(\\d{2})(\\d{2})/$1($2-$3-$4)/}",
        "description": "Transform wpli20190305 to wpli(2019-03-05)",
    },

Running this snippet will insert the contents at the position of the cursor, as described by Replace text in a Visual Studio Code Snippet Literal. The top answer basically says to use TM_SELECTED_TEXT, but according to documentation this does not seem to automatically select the current "word".

Is there any way to setup a keyboard combination or snippet, such that if the cursor is in some text of the form \w+\d{8}, replace-transform with word(yyyy-mm-dd)?

The core of the question is to convert the keystroke sequence wpli20250305 into the appropriate form either with a key combination sequence before or after the keystroke sequence, so if there is an alternative to replace-transform, I am open to it.

Upvotes: -1

Views: 51

Answers (0)

Related Questions