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.
If you can rely on Vim's Python scripting support being available
:py import uuid
:%s/foo/\=pyeval('str(uuid.uuid4())')/
If you don't want / cannot use a Vim language wrapping (e.g. to Python or Perl), you have to write a DLL wrapper for the Win32 UuidCreate()
function and invoke that from Vim via libcall()
. (The help says that you cannot directly invoke Windows system DLLs because the calling convention doesn't match.)
The wrapper is probably simple and easy to write, but you still need to compile a DLL and install that on each system.
Replace
'[guid]::NewGuid().ToString()'
with
'powershell.exe -command "[guid]::NewGuid().ToString()"'
Does that help?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With