Skip to content

Instantly share code, notes, and snippets.

@romainl
Last active September 17, 2023 10:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainl/97d79dfd522bd1c33c7fafb9ec08dc5d to your computer and use it in GitHub Desktop.
Save romainl/97d79dfd522bd1c33c7fafb9ec08dc5d to your computer and use it in GitHub Desktop.
Multiply/divide by [count]
function! Multivide(divide)
let cnt = v:count1
let old_reg = getreg("v")
call search('\d\([^0-9\.]\|$\)', 'cW')
normal v
call search('\(^\|[^0-9\.]\d\)', 'becW')
normal "vygv
if a:divide == 1
execute 'normal "_c' . @v / cnt
else
execute 'normal "_c' . @v * cnt
endif
call setreg("v", old_reg)
endfunction
nnoremap <key> :<C-u>call Multivide(0)<CR>
nnoremap <otherkey> :<C-u>call Multivide(1)<CR>
@bagohart
Copy link

Hey romainl, this is great!
I never knew I needed this, but now I use it all the time.
Therefore, I decided to polish it a little and publish it as a plugin:
https://github.com/bagohart/multiply-divide.vim
Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment