9

Is it possible to create a code snippet or a task runner (or anything else) that would allow me to generate a UUID in VSCode? This question can also be interpreted in a more generic way: Can I run an external tool/program and insert the result at the current cursor position.

Thanks.

Gabe Perez
  • 322
  • 1
  • 3
  • 11

3 Answers3

6

The following extension is super easy to use and does exactly what you want: https://github.com/heaths/vscode-guid (or get it here from the VS Marketplace)

Simon Mattes
  • 4,866
  • 2
  • 33
  • 53
  • 1
    For those coming to this answer later, if the extension no longer exists just search on the Extensions panel for "GUID" and you'll find a few there. – Nick Coad Mar 04 '20 at 20:52
6

Regarding code sniper and uuid, consider that VSCode 1.53 (Jan. 2021) introduces an interesting feature:

New Snippet Variables

There are new snippet variables for inserting uuids and for inserting the relative path the current file.

The sample snippet below would print:

let someId = 'foo/test.js/c13d226f-1932-40e2-9fd9-10198c219e33'
// sample snippet using UUID and RELATIVE_FILEPATH
{
 "scope": "javascript",
 "prefix": "newVars",
 "body": "let someId = '${RELATIVE_FILEPATH}/${UUID}'$0"
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Unfortunately you can't do it right now with snippets. I don't think it can work via tasks either.

The only thing I can do for you is to give you a hacky solution that works for C#, TypeScript and Yaml files (tested in VSCode 0.9.0). Let's implement it for TypeScripts to make an example:

  • Go to the folder where VSCode is installed
  • Open the file resources\app\extensions\typescript\out\features\suggestSupport.js
  • Add a method newGuid() to the SuggestSupport class:

    // copied from: https://github.com/Microsoft/ApplicationInsights-JS/blob/master/JavaScript/JavaScriptSDK/Util.ts
    SuggestSupport.prototype.newGuid = function() {
        var hexValues = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
    
        // c.f. rfc4122 (UUID version 4 = xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx)
        var oct = "", tmp;
        for (var a = 0; a < 4; a++) {
            tmp = (4294967296 * Math.random()) | 0;
            oct += hexValues[tmp & 0xF] + hexValues[tmp >> 4 & 0xF] + hexValues[tmp >> 8 & 0xF] + hexValues[tmp >> 12 & 0xF] + hexValues[tmp >> 16 & 0xF] + hexValues[tmp >> 20 & 0xF] + hexValues[tmp >> 24 & 0xF] + hexValues[tmp >> 28 & 0xF];
        }
    
        // "Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively"
        var clockSequenceHi = hexValues[8 + (Math.random() * 4) | 0];
        return oct.substr(0, 8) + "-" + oct.substr(9, 4) + "-4" + oct.substr(13, 3) + "-" + clockSequenceHi + oct.substr(16, 3) + "-" + oct.substr(19, 12);
    }  
    
  • Find the line var suggests = []; [inside the suggest function]

  • Add this suggestion below that line:

            suggests.push({
                label: "Create new UUID",
                codeSnippet: "\"" + _this.newGuid() + "\"",
                type: "keyword"
            });
    

After restarting Code you will always receive in .ts files a suggestion called "Create new UUID" which adds a value like "243BC2A6-1AB5-445B-B086-DBDED67368F5" at the current cursor position. You can force the appearance of the suggestion box by pressing CTRL + Space.

To add this suggestion to C# and YAML you need to do the same in the corresponding suggestSupport.js files.

Wosi
  • 41,986
  • 17
  • 75
  • 82
  • Thanks, that looks like a good approach given how snippets and tasks work in VSCode. I'm trying to get it to work in a json file, I'll let you know if that works too. – Gabe Perez Oct 08 '15 at 13:48
  • @GabePerez were you able to get into json file as it to be generated when the snippet is used ? – Irfan Syed Feb 18 '19 at 10:43