How can I create a GUID in Vim?

Vim does not have a built-in GUID generator.

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

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

I call this in the substitution as follows:

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

While this works, it blinks a window for each lookup, and it is pretty slow.

I really want to create a GUID in Vim, portable and fast.

+5
source share
6 answers

If you can rely on Vim Python scripting support available

:py import uuid
:%s/foo/\=pyeval('str(uuid.uuid4())')/
+8
source

/ Vim (, Python Perl), DLL- Win32 UuidCreate() Vim libcall(). ( , DLL- Windows, .)

, , , DLL .

+3

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

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

?

+2

Windows system() ( ). - , Vim, . Python:

:python import uuid, vim; vim.command("let g:uuid = '" + str(uuid.uuid1()) + "'

, Python Vim Python; , , Perl Ruby, - . , PowerShell.

+1

, foo . , :

M $Windows:

%s/foo/\=printf("%04X",libcallnr("msvcrt.dll","rand",localtime()))[:2]/

Unix:

%s/foo/\=printf("%04X",libcallnr("libc.so.6","rand",localtime()))[:2]/

, .

+1

, - , UUID, - UUID.

Marius Gedminas , - UUID, :

function! Guid()
python << EOF
import uuid, vim
vim.command("normal i" + str(uuid.uuid4()) )
EOF
endfunction

<alt+G>: map <m-g> :call Guid() <cr>

( Gvim Python )

0

All Articles