Vim Tips Wiki
Register
Advertisement
Tip 987 Printable Monobook Previous Next

created 2005 · complexity basic · author Brent Rice · version 6.0


I have often found myself wanting to do quick floating point arithmetic within Vim. Here are the maps that I put together to handle such. They do require access to Perl, but they are a simple solution to simple mathematical needs. Of course the syntax used for the mathematical expression should follow that of Perl itself.

Evaluate an expression contained on the full current line and place answer in a new line below the current line:

nnoremap <Leader>ma yyp^y$V:!perl -e '$x = <C-R>"; print $x'<CR>-y0j0P

Evaluate an expression contained in a visual selection and place the answer in a new line below the current line:

vnoremap <Leader>ma yo<Esc>p^y$V:!perl -e '$x = <C-R>"; print $x'<CR>-y0j0P

Evaluate an expression contained on the full current line and replace the current line with the answer:

nnoremap <Leader>mr ^"gy0^y$V:!perl -e '$x = <C-R>"; print $x'<CR>^"gP

Evaluate an expression contained in a visual selection and replace the visual selection with the answer:

vnoremap <Leader>mr "aygvrXgv"by:r !perl -e '$x = <C-R>a; print $x'<CR>0"cyWddk:s/<C-R>b/<C-R>c/<CR>

Comments[]

Or you could use :perldo instead of shelling out.


The bccalc script#219 and EvalSelection script#889 plugins serve a similar purpose.


The last one seems wrong. Both on Windows and on Linux I saw the expression replaced by "XXXXX...".


I found the problem with the last one: it does work with there is only one line in the buffer. This works (assuming `calcu <expr>' is something to calculate the expression:

vnoremap <silent> <Leader>mr "aygvrXgv"by:r !calcu <C-R>a<CR>km`j0"cyWdd``:s/<C-R>b/<C-R>c/<CR>$

For other three, I am now using a simpler form:

nnoremap <silent> <Leader>ma yypV:!calcu <C-R>"<CR>k$
vnoremap <silent> <Leader>ma yo<Esc>pV:!calcu <C-R>"<CR>k$
nnoremap <silent> <Leader>mr yyV:!calcu <C-R>"<CR>$

I really worry about the replacement trick in the last mapping, so I worked out a safer version (using marking and jumping):

vnoremap <silent> <Leader>mr ygvmaomb:r !calcu <C-R>"<CR>"ay$dd`bv`a"ap$

In addition to bccalc and EvalSelection, there are VimCalc (requires Python) script#3329 and Crunch (pure VimL) script#4686. Taking in account these plugins, maybe tips Tip 1235 Scientific calculator and Tip 73 Using vim as calculator could be merge with this.


Advertisement