Search code examples
vimpowershellguid

How can I generate a GUID in Vim?


Vim doesn't have a built-in GUID generator.

For the project I'm working on, I can rely on Powershell being available, so the following gives me a GUID string:

[guid]::NewGuid().ToString()

I call this within a substitution, as follows:

%s/foo/\=system('[guid]::NewGuid().ToString()')[:2]/

While this works, it flashes up a window for each substitution and it's quite slow.

What I really want is a way to generate GUIDs within Vim, portably and quickly.


Solution

  • If you can rely on Vim's Python scripting support being available

    :pyx import uuid
    :%s/foo/\=pyxeval('str(uuid.uuid4())')/
    

    (If your Vim and Python are very old, use :py and pyeval() instead of :pyx and pyxeval().)