Skip to content

Instantly share code, notes, and snippets.

@rinx
Created August 23, 2014 07:45
Show Gist options
  • Save rinx/aa41a0b0bd1d79b78247 to your computer and use it in GitHub Desktop.
Save rinx/aa41a0b0bd1d79b78247 to your computer and use it in GitHub Desktop.
convert csv to tex-table style
"A function to convert csv to tex table
function! s:csv_to_tex_table() range
let lines = getline(a:firstline, a:lastline)
let spacelen = []
let maxrownum = 0
let maxcollen = []
let values = []
for i in range(0, a:lastline - a:firstline)
let linespacelen = []
call add(values, split(substitute(lines[i], '\s*\,\s*', ',', 'g'), ','))
for v in values[i]
call add(linespacelen, len(v))
endfor
if len(values[i]) > maxrownum
let maxrownum = len(values[i])
endif
call add(spacelen, linespacelen)
unlet linespacelen
endfor
for i in range(0, a:lastline - a:firstline)
while len(spacelen[i]) < maxrownum
call add(spacelen[i], 0)
endwhile
endfor
for i in range(0, maxrownum - 1)
call add(maxcollen, 0)
for j in range(0, a:lastline - a:firstline)
if spacelen[j][i] > maxcollen[i]
let maxcollen[i] = spacelen[j][i]
endif
endfor
endfor
for i in range(0, a:lastline - a:firstline)
let aftersbst = values[i][0] . repeat(" ", maxcollen[0] - len(values[i][0])) . " "
for j in range(1, len(values[i]) - 1)
let aftersbst .= "& " . values[i][j] . repeat(" ", maxcollen[j] - len(values[i][j])) . " "
endfor
call setline(i + a:firstline, aftersbst . "\\\\")
endfor
endfunction
command! -range CsvToTexTable <line1>,<line2>call s:csv_to_tex_table()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment