문제

I want to generate 10 random numbers in the buffer.

In Emacs I would do:

<start macro>(random limit) <eval lisp><newline><end macro> to define the macro,

and 9 <execute macro> to generate it 10 times.

Is there a way I can do this in vim?

도움이 되었습니까?

해결책

is this ok for you?

:r! echo $RANDOM

then

9@:

if you have certain programming language env available on your OS, you can eval those statement too.

다른 팁

Vim doesn't offer native random generator, however if you have vim compiled with Python, the following method will append a random digit at the end of your line:

:py import vim, random; vim.current.line += str(random.randint(0, 9))

Note: To check if your vim supports Python, try: :echo has('python') (1 for yes).

You can also use shell which offers $RANDOM variable as Kent suggested (works with bash/ksh/zsh) which returns a pseudorandom (0-32767), in example:

:put =system('echo $RANDOM')

or:

:r! od -An -td -N1 /dev/urandom

On Windows, you've to have Cygwin/MSYS/SUA installed, or use %RANDOM% variable as Carpetsmoker suggested.

If you don't have access to shell and Python, as for workaround, you use last few digits from the current timestamp, in example:

:put =reltimestr(reltime())[-2:]

Note: If you're using it quite often, write a simple function which will return reltimestr(reltime())[-4:].

Note: Above methods returns only a pseudorandom integer which should not be used to generate an encryption key.


To add more random numbers please press @: to repeat the command again. Or prefix with number (like 10@:) to add much more of random numbers separated by new lines.


Related:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top