Skip to content

Instantly share code, notes, and snippets.

@dahu
Created February 11, 2015 02:18
Show Gist options
  • Save dahu/c38092e54ec7c6ef7132 to your computer and use it in GitHub Desktop.
Save dahu/c38092e54ec7c6ef7132 to your computer and use it in GitHub Desktop.
Expand bash brace forms in Vim
" Barry Arthur, Feb 2015
" Expand a bash brace expression
let x = 'xorg-{,xinit,xfontsel,xset,xrdb,font-util{,s},server{,-utils,},utils,xmodmap,xwininfo}'
function! BashBracesToVimTree(string)
return eval('[' .
\ tr(substitute(substitute(substitute(substitute(a:string, "'", "''", 'g')
\ , '[^,{}]\+', "'&'", 'g')
\ , "'{", "',{", 'g')
\ , "{,", "{", 'g')
\ , '{}', '[]')
\ . ']')
endfunction
function! ExpandTree(tree, ...)
let parent = a:0 ? a:1 : ''
let s = []
for elem in a:tree
if type(elem) == type([])
let prior = s[-1]
if parent == ''
call remove(s, -1)
endif
call add(s, ExpandTree(elem, prior))
else
call add(s, parent . elem)
endif
unlet elem
endfor
return join(s)
endfunction
echo ExpandTree(BashBracesToVimTree(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment