Skip to content

Instantly share code, notes, and snippets.

@dahu
Created September 11, 2015 00:58
Show Gist options
  • Save dahu/cdc0aba6133309f45d24 to your computer and use it in GitHub Desktop.
Save dahu/cdc0aba6133309f45d24 to your computer and use it in GitHub Desktop.
Collapse key\tvalue pairs into key\tvalue1,value2,...
" Barry Arthur Sep 2015
" Convert possibly repeating key\tvalue pairs into key\tvalue1,value2,...
function! CollapsePairs()
let stuff = {}
g/\t/let pair = split(getline('.'), '\t') | let stuff[pair[0]] = add(get(stuff, pair[0], []), pair[1]) | d
call append('$', map(sort(items(stuff)), 'v:val[0] . "\t" . join(v:val[1], ",")'))
endfunction
call CollapsePairs()
finish
one apple
one pear
three moon
two elephant
one lemon
two pig
two tiger
zero chair
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment