Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 496
Rik Hemsley Avatar asked Sep 10 '12 10:09

Rik Hemsley


3 Answers

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

:py import uuid
:%s/foo/\=pyeval('str(uuid.uuid4())')/
like image 121
Marius Gedminas Avatar answered Nov 15 '22 07:11

Marius Gedminas


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.

like image 43
Ingo Karkat Avatar answered Nov 15 '22 08:11

Ingo Karkat


Replace

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

with

'powershell.exe -command "[guid]::NewGuid().ToString()"'

Does that help?

like image 37
ravikanth Avatar answered Nov 15 '22 09:11

ravikanth