I know that it is possible to increment/decrement an integer by a repeat count using the <repeat-count><ctrl>-a
and <repeat-count><ctrl>-x
commands.
Now I was wondering whether there are analogous commands for multiplication and division. And if there is no such command, how can I implement this in my own .vimrc?
Here is a quick and dirty attempt:
function! Multiply()
" save count
let cnt = v:count1
" save register v
let old_reg = getreg("v")
" select the number under the cursor
call search('\d\([^0-9\.]\|$\)', 'cW')
normal v
call search('\(^\|[^0-9\.]\d\)', 'becW')
" yank it into register v then reselect
normal "vygv
" change the selection with the yanked number multiplied by the count
execute "normal c" . @v * cnt
" restore register v
call setreg("v", old_reg)
endfunction
nnoremap <F5> :<C-u>call Multiply()<CR>
Now, press 5<F5>
to multiply the number under the cursor by 5
.
If you want to do it without mappings/functions:
v{motion}c<C-r>=<C-r>"*5<CR><Esc>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments