sponsor Vim development Vim logo Vim Book Ad

basic Tip #73: Using vim as calculator

 tip karma   Rating 79/49, Viewed by 4313 

created:   June 5, 2001 11:14      complexity:   basic
author:   Volker Duetsch      as of Vim:   5.7

Basic calculations can done within vim easily by typing (insert-mode):
STRG (=CTRL) + R followed by =
then for example 2+2 and hit RETURN
the result 4 will be printed in the document.

Some other tipps you can find under www.linuxclass.de/vim.phtml

 rate this tip  Life Changing Helpful Unfulfilling 

<<Remove unwanted empty lines | Using Vim as an outline processor >>

Additional Notes

[email protected], July 25, 2001 16:20


Adding the following map to your vimrc
ino <C-A> <C-O>yiW<End>=<C-R>=<C-R>0<CR>

Then, just type 8*8<C-A>
you will get 8*8=64
[email protected], December 18, 2001 11:03
For simple arithmitic, you can often get away with just using repeats of the Ctrl-A (increment) and Ctrl-X (decrement) commands.

For instance, to calculate 453-244, insert the text "453" and then, in Command mode, type 244 and press Ctrl-X (answer: 209).

To calculate 4024+3322, insert the text "4024" and then, in Command mode, type 3322 and press Ctrl-A (answer: 7346).
[email protected], January 2, 2002 10:53
Is there any way that I can calculate with real numbers (as opposed to integers only)?
antti@_NOSPAM_kuntsi.com, January 26, 2002 7:52
I have a mapping for passing a line to bc:

map gbc yypkA =<ESC>jOscale=2<ESC>:.,+1!bc<CR>kJ

I guess it could be made a bit better. Another 'useful' mapping I once needed was summing numbers on multiple lines:

vmap gs y'>p:'[,']-1s/$/+/\|'[,']+1j!<CR>'[0"wy$:.s§.*§\=w§<CR>'[yyP:.s/./=/g<CR>_j

So, having

10
20
30

in a file would result in two new lines:

==
60

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo