Last active
August 29, 2015 14:27
-
-
Save dahu/9946b83850d5992332d6 to your computer and use it in GitHub Desktop.
Vim: Moving habits into maps
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
" Barry Arthur, August 2015 | |
" Moving habits into maps | |
" | |
" A regular editing sequence I caught myself | |
" doing was: <c-v>}kwI<some-text><esc> | |
" to prepend a common string to all of | |
" the lines within the current indent. | |
" These maps make that much simpler: | |
" <leader>K<some-text><esc> | |
" | |
" And subsequently aligning on something | |
" within the same range of text was so | |
" common that I have mapped that now to: | |
" <leader>t | |
" | |
" The actual maps might change in future, | |
" but the process of identifying and | |
" crystalising habits into maps is the | |
" important point here. | |
" direction: | |
" <0 : up | |
" 0 : up and down | |
" >0 : down | |
function! MarkIndent(direction) | |
let d = a:direction | |
let start = line('.') | |
let indent = indent(start) | |
if d < 1 | |
while indent(start) == indent | |
let start -= 1 | |
endwhile | |
let start += 1 | |
endif | |
let end = start | |
if d >= 0 | |
while indent(end) == indent | |
let end += 1 | |
endwhile | |
endif | |
let end -= 1 | |
exe 'normal! ' . start . 'Gm`' | |
exe 'normal! ' . end . 'G' | |
endfunction | |
nnoremap <silent> <leader>j :call MarkIndent(1)<cr> | |
nnoremap <silent> <leader>J :call MarkIndent(0)<cr> | |
nnoremap <silent> <leader>k :call MarkIndent(-1)<cr> | |
nnoremap <silent> <leader>v <c-v>`` | |
nmap <silent> <leader>K <leader>J<leader>vI | |
nmap <silent> <leader>t <leader>v:Tabular / |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment