Permalink
Browse files

Add function to align blocks on the first '='.

Useful to prettify hashes in ruby.
  • Loading branch information...
1 parent ed1ecca commit f2926beb071b630dd1a548aa2151cf478be2d6af @bernd committed Apr 15, 2012
Showing with 28 additions and 0 deletions.
  1. +28 −0 vimrc
View
28 vimrc
@@ -143,3 +143,31 @@ nnoremap <leader>w mz:%s/\s\+$//<cr>:let @/=''<cr>`z
" Highlight VCS conflict marker
match ErrorMsg '^\(<\|=\|>\)\{7\}\([^=].\+\)\?$'
+
+" Align blocks on first '=' (from tenderlove)
+" Usage: ctrl-v + <leader>a
+command! -nargs=? -range Align <line1>,<line2>call AlignSection('<args>')
+vnoremap <silent> <Leader>a :Align<CR>
+function! AlignSection(regex) range
+ let extra = 1
+ let sep = empty(a:regex) ? '=' : a:regex
+ let maxpos = 0
+ let section = getline(a:firstline, a:lastline)
+ for line in section
+ let pos = match(line, ' *'.sep)
+ if maxpos < pos
+ let maxpos = pos
+ endif
+ endfor
+ call map(section, 'AlignLine(v:val, sep, maxpos, extra)')
+ call setline(a:firstline, section)
+endfunction
+
+function! AlignLine(line, sep, maxpos, extra)
+ let m = matchlist(a:line, '\(.\{-}\) \{-}\('.a:sep.'.*\)')
+ if empty(m)
+ return a:line
+ endif
+ let spaces = repeat(' ', a:maxpos - strlen(m[1]) + a:extra)
+ return m[1] . spaces . m[2]
+endfunction

0 comments on commit f2926be

Please sign in to comment.