Skip to content

Instantly share code, notes, and snippets.

@MaienM
Forked from tpope/cucumbertables.vim
Created October 2, 2011 21:48
Show Gist options
  • Save MaienM/1258015 to your computer and use it in GitHub Desktop.
Save MaienM/1258015 to your computer and use it in GitHub Desktop.
Auto-align on equal signs (=) using Tabularize.
inoremap <silent> = =<Esc>:call <SID>ealign()<CR>a
function! s:ealign()
let p = '^.*=\s.*$'
if exists(':Tabularize') && getline('.') =~# '^.*=' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
let column = strlen(substitute(getline('.')[0:col('.')],'[^=]','','g'))
let position = strlen(matchstr(getline('.')[0:col('.')],'.*=\s*\zs.*'))
Tabularize/=/l1
normal! 0
call search(repeat('[^=]*=',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
@carsato
Copy link

carsato commented Oct 9, 2011

nice function, it works fine.
For me is a problem when writing asociative arrays in php
'key' => 'value' always results in
'key' = > 'value' ( introduces a space between = and > )

I'm tried to modify it but I can't get it :(
well, nice function anyway.

@bobmaerten
Copy link

I replaced line 3 by :

let p = '^.*=[^>]*$'

to match = characters but not =>

@MaienM
Copy link
Author

MaienM commented Feb 29, 2012 via email

@siwilkins
Copy link

Thanks very much for this - one problem I'm finding is that when it carries out the Tabularize the cursor moves to the beginning of the line. Do you know a way around this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment