Using a vimscript to run through a list of substitutions with dictionary pairs

I wanted to modify a test suite to call a method using ruby 2’s keyword arguments. There were several calls to the same initialization function that followed a pattern that made it a good candidate for a series of `%s` calls using key-value pairs in a dictionary.

Interesting how vimscript uses a leading slash as line continuation instead of a trailing slash at the end of the continued line.

Also, it took me a while to figure out that interpolating a variable in an Ex command required “execute” and string concatenation with periods.

let fields = { 'public_key' : 'public_pem_key',
              \'private_key' : 'private_pem_key',
              \'username' : 'username',
              \'password' : 'password',
              \'url' : 'url',
              \'ssl_verify_host' : 'ssl_verify_host'

for key in keys(fields)
  execute '%s/\([ \t]*\)\(.*TestCommon::' . fields[key] . '\)/\1' . key . ': \2/'

%d bloggers like this: