Increment or decrement numbers while <C-a> and C-x> bindings are unmapped in vim

Viewed 156

I keep the increment/decrement bindings <C-a> and <C-x> unmapped in my vimrc, because I rarely need them and they can cause a lot of headache if unknowingly pressed during development.

For the rare case where I do need this functionality, is there a way I can invoke the increment/decrement actions without temporarily binding them again?

2 Answers

Not sure if you can "completely" avoid temporarily binding them again, however you could write a function in your config to locally bind, increment, and locally unbind. Something like

function! g:Increment()
    nnoremap <buffer> <C-a> <C-a>
    exe "normal \<C-a>"
    :nunmap <buffer> <C-a>
endfunction

and the analog for Decrement.

You could then position the cursor over the value you want to increment and run :call Increment().

Bindings that have been remapped or unmapped can still be executed via the following ex command form:

:exe "normal! [binding]"

So to answer my original question, I can execute <C-a> or <C-x> while they are unmapped using the following ex commands:

# Increment
:exe "normal! \<C-A>"

# Decrement
:exe "normal! \<C-X>"
Related