Skip to content

Instantly share code, notes, and snippets.

@inkredabull
Created September 10, 2012 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inkredabull/3693833 to your computer and use it in GitHub Desktop.
Save inkredabull/3693833 to your computer and use it in GitHub Desktop.
Using random number value generation in vim substitution
BACKGROUND
Given a JSON data file for one Use Case, I wanted to repurpose it
for two other, similar Use Cases while prototyping. Actual values
in the copied files didn't matter; they just needed to be different
to make the prototype appear different between Use Cases.
RESEARCH
I knew I wanted to do something like [1] so I did a Google search
for "vim random number substitute" and found [2]. That didn't work
for me (something about my syntax of 'eval' and 'system' in command
mode) so I opted to leverage definitions as found in [3] which I
modified and put in my .vimrc file. The regex to the left-facing
curly bracket could have been \d+ but I opted to be lazy.
SOLUTION
'<,'>s/"foo":.*}/\=eval("printf('%s', '\"foo\":') . Random() . '}'")/
REFERENCES
[1] : https://gist.github.com/3555497
[2] : http://www.commandlinefu.com/commands/view/6880/map-r-do-insert-random-number-in-vim
[3] : http://vim.1045645.n5.nabble.com/simple-16-bit-random-number-generator-td2843842.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment