Skip to content

Instantly share code, notes, and snippets.

@inkredabull
Created August 31, 2012 16:33
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/3555497 to your computer and use it in GitHub Desktop.
Save inkredabull/3555497 to your computer and use it in GitHub Desktop.
Using dynamic val in vim substitution
BACKGROUND
Was using a file with a bunch of named data points [1]. Didn't
want to edit the name of each data point to repurpose it for my
prototyping efforts but needed a command mode substitution to
iterate over a visual block and do the replacement but with a
dynamic expression (line number is adequate.)
Found [2] and then figured out I'd need something like printf()
for use in eval() so stumbled upon [3].
SOLUTION
:'<,'>s/"name":".*"/\=eval("printf('%s','\"name\":\"foo') . printf('%s', line('.')) . '\"'")/
REFERENCES
[1] : http://bost.ocks.org/mike/sankey/energy.json
[2] : http://vim.wikia.com/wiki/Using_an_expression_in_substitute_command
[3] : http://vim.1045645.n5.nabble.com/what-is-vim-s-printf-as-it-is-in-c-td4973892.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment