Skip to content

Instantly share code, notes, and snippets.

@abatkin
Created May 29, 2012 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abatkin/2830780 to your computer and use it in GitHub Desktop.
Save abatkin/2830780 to your computer and use it in GitHub Desktop.
.vim/after/plugin/tabular.vim
function! FormatTableLine(line)
let curr_line = a:line
" Get rid of all leading or trailing spaces
let curr_line = substitute(curr_line, '^[ ]\{1,\}\|[ ]\{1,\}$', '', 'g')
" Replace all tabs with " | "
let curr_line = substitute(curr_line, '\t', ' | ', 'g')
" Possibly insert a leading | (if there isn't already one)
let curr_line = substitute(curr_line, '^\(|\)\@!', '| ', '')
" Possibly insert a trailing | (if there isn't already one)
let curr_line = substitute(curr_line, '\(|\)\@<!$', ' |', '')
return curr_line
endfunction
function! FormatPrettyTable() range
for linenum in range(a:firstline, a:lastline)
let curr_line = getline(linenum)
let curr_line = FormatTableLine(curr_line)
call setline(linenum, curr_line)
endfor
call Tabularize("/\|/")
endfunction
AddTabularPipeline! pretty_table /\t\|\(-\)\@<!|\(-\)\@!/ map(a:lines, "FormatTableLine(v:val)") | tabular#TabularizeStrings(a:lines, '|')
nmap <silent> <leader>pt :Tabularize pretty_table<cr>
nmap <silent> <leader>pp "+P:Tabularize pretty_table<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment