Last active
September 26, 2015 22:48
-
-
Save AndrewRadev/1171605 to your computer and use it in GitHub Desktop.
Tabularize mappings for specific use cases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Tabularize mappings | |
" | |
" sa= -- align by equals | |
" sa> -- align by "=>" | |
" | |
" and so on. Note that any character can be entered and the mappings will | |
" attempt to align by that, in the simplest way possible. | |
" | |
" sa| -- equivalent to ":Tab/|" | |
" | |
" The custom Tabularize definitions (in the other file) need to be placed in | |
" after/plugin/tabularize.vim, so that they can be defined after the plugin is | |
" loaded. | |
" | |
" To compress multiple spaces, effectively un-tabularizing, use `s=` | |
" | |
nnoremap sa :call <SID>TabularizeMapping(0)<cr> | |
xnoremap sa :<c-u>call <SID>TabularizeMapping(1)<cr> | |
nnoremap s= :call <SID>TabularizeReset()<cr> | |
xnoremap s= :call <SID>TabularizeReset()<cr> | |
function! s:TabularizeMapping(visual) | |
echohl ModeMsg | echo "-- ALIGN -- " | echohl None | |
let align_type = nr2char(getchar()) | |
if align_type == '=' | |
call s:Tabularize('equals', a:visual) | |
elseif align_type == '>' | |
call s:Tabularize('ruby_hash', a:visual) | |
elseif align_type == ',' | |
call s:Tabularize('commas', a:visual) | |
elseif align_type == ':' | |
call s:Tabularize('colons', a:visual) | |
elseif align_type == ' ' | |
call s:Tabularize('space', a:visual) | |
else " just try aligning by the character | |
call s:Tabularize('/'.align_type, a:visual) | |
end | |
endfunction | |
function! s:Tabularize(command, visual) | |
normal! mz | |
let cmd = "Tabularize ".a:command | |
if a:visual | |
let cmd = "'<,'>" . cmd | |
endif | |
exec cmd | |
echo | |
normal! `z | |
endfunction | |
function! s:TabularizeReset() | |
let original_cursor = getpos('.') | |
s/\S\zs \+/ /g | |
" Don't leave an entry in the history | |
call histdel('search', -1) | |
let @/ = histget('search', -1) | |
call setpos('.', original_cursor) | |
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" This file goes in after/plugin/tabularize.vim | |
AddTabularPattern! equals /^[^=]*\zs=/ | |
AddTabularPattern! ruby_hash /^[^=>]*\zs=>/ | |
AddTabularPattern! commas /,\s*\zs\s/l0 | |
AddTabularPattern! colons /^[^:]*:\s*\zs\s/l0 | |
" Not perfect, causes problems sometimes | |
AddTabularPipeline space / \+/ | |
\ map(a:lines, "substitute(v:val, ' \+', ' ', 'g')") | |
\ | tabular#TabularizeStrings(a:lines, ' ', 'l0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment