if !exists('g:Align_StartIncrement') let g:Align_StartIncrement = 4 endif if !exists('g:Align_DefaultSeq') let g:Align_DefaultSeq = {} endif let g:Align_DefaultSeq['c'] = '=' let g:Align_DefaultSeq['cpp'] = '=' let g:Align_DefaultSeq['python'] = '=' let g:Align_DefaultSeq['vhdl'] = '<=\|:\|:=\|=>' let g:Align_DefaultSeq['vim'] = '=' function! s:Align(...) range " If pattern is not set, find the default pattern for the current filetype if !exists("a:1") if !exists("g:Align_DefaultSeq[&filetype]") echoerr "No Pattern was given and no default pattern could be found. Doing nothing." return endif let l:pattern = g:Align_DefaultSeq[&filetype] else let l:pattern = a:1 endif let l:nextstart = 0 while l:nextstart >= 0 let l:nextstart = s:AlignOnce(l:nextstart, l:pattern, a:firstline, a:lastline) if l:nextstart != -1 let l:nextstart = l:nextstart + g:Align_StartIncrement endif endwhile endfunction function! s:AlignOnce(startpos, pattern, startline, endline) " First, find the rightmost occurence of pattern in all lines let l:index = -1 for i in range(a:startline, a:endline) let l:line = getline(i) let l:pos = match(l:line, a:pattern, a:startpos) if l:pos > l:index let l:index = l:pos endif endfor " Now, for each line, find the first occurrence of pattern, and insert " spaces until pattern was shifted to l:index for i in range(a:startline, a:endline) let l:line = getline(i) let l:pos = match(l:line, a:pattern, a:startpos) if l:pos == -1 continue endif " Build string of spaces let l:spaces = "" for j in range(l:pos, l:index-1) let l:spaces = l:spaces . ' ' endfor " Insert spaces right before the pattern if l:pos > 0 let l:line = l:line[: l:pos-1] . l:spaces . l:line[l:pos :] else let l:line = l:spaces . l:line endif call setline(i, l:line) endfor return l:index endfunction command! -nargs=? -range Align ,call s:Align()