Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
" This is a test answer for https://vi.stackexchange.com/q/17423/1841
" <C-a>
nnoremap <Leader>a :call CoherentIncrement(1)<CR>
" <C-x>
nnoremap <Leader>b :call CoherentIncrement(0)<CR>
function! CoherentIncrement(inc)
let save_cursor = getcurpos()
" Set the operation to increment or decrement according to the argument
let operation = "norm! " . ( a:inc ? "" : "" )
" Search for the next number
if search('\d', 'cW') != 0
" If the next number is 0 and the operation is decrement do nothing
if !a:inc && getline('.')[getcurpos()[2]-1] == '0'
call setpos('.', save_cursor)
return
endif
" If the character before the next number is '-' remove it temporarily
" then add it again
if getline('.')[getcurpos()[2]-2] == '-'
norm! "_X
execute operation
norm! i-
else
execute operation
endif
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.