Skip to content

Instantly share code, notes, and snippets.

@roman
Forked from tpope/cucumbertables.vim
Created February 23, 2011 22:22
Show Gist options
  • Save roman/841327 to your computer and use it in GitHub Desktop.
Save roman/841327 to your computer and use it in GitHub Desktop.
inoremap <silent> <Bar> <Bar><Esc>:call <SID>align()<CR>a
function! s:align()
let p = '^\s*|\s.*\s|\s*$'
let lineStartsWithPipe = getline('.') =~# '^\s*|'
let pipedContentOnPrevLine = getline(line('.')-1) =~# p
let pipedContentOnNextLine = getline(line('.')+1) =~# p
if exists(':Tabularize') && lineStartsWithPipe && (pipedContentOnPrevLine || pipedContentOnNextLine)
" Getting the number of pipes that are on the line
" (the column you are standing on)
let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
" Get the length of the lastest word we are standing on
let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
" What is the l1 for?
Tabularize/|/l1
" What happens when we don't make this a normal command?
normal! 0
" move the cursor to the column and position infered the first time
" respecting new whitespaces
call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment