Skip to content

Instantly share code, notes, and snippets.

@kamichidu
Last active December 29, 2015 04:09
Show Gist options
  • Save kamichidu/7612734 to your computer and use it in GitHub Desktop.
Save kamichidu/7612734 to your computer and use it in GitHub Desktop.
replace keyword
let s:save_cpo= &cpo
set cpo&vim
function! Sprintf(expr, args)
let l:result= a:expr
while match(l:result, '\${\w\+}') != -1
let l:keyword= matchstr(l:result, '\${\zs\w\+\ze}')
let l:replacement= has_key(a:args, l:keyword) ? a:args[l:keyword] : ''
let l:result= substitute(l:result, '\${' . l:keyword . '}', l:replacement, 'g')
endwhile
return l:result
endfunction
" output => A is B
echo Sprintf('${hoge} is ${fuga}', {
\ 'hoge': 'A',
\ 'fuga': 'B',
\})
let &cpo= s:save_cpo
unlet s:save_cpo
" vim: foldenable: foldmethod=marker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment