Vim Tips Wiki
(Move categories to tip template)
(Just for reference - forgot to log in before publishing the last comment)
 
(4 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{TipImported
 
{{TipImported
 
|id=987
 
|id=987
|previous=986
+
|previous=984
 
|next=988
 
|next=988
|created=September 7, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Brent Rice
 
|author=Brent Rice
|version=5.7
+
|version=6.0
 
|rating=7/6
 
|rating=7/6
 
|category1=
 
|category1=
Line 16: Line 16:
 
Evaluate an expression contained on the full current line and place answer in a new line below the current line:
 
Evaluate an expression contained on the full current line and place answer in a new line below the current line:
 
<pre>
 
<pre>
nnoremap &lt;Leader&gt;ma yyp^y$V:!perl -e '$x = &lt;C-R&gt;"; print $x'&lt;CR&gt;-y0j0P
+
nnoremap <Leader>ma yyp^y$V:!perl -e '$x = <C-R>"; print $x'<CR>-y0j0P
 
</pre>
 
</pre>
   
 
Evaluate an expression contained in a visual selection and place the answer in a new line below the current line:
 
Evaluate an expression contained in a visual selection and place the answer in a new line below the current line:
 
<pre>
 
<pre>
vnoremap &lt;Leader&gt;ma yo&lt;ESC&gt;p^y$V:!perl -e '$x = &lt;C-R&gt;"; print $x'&lt;CR&gt;-y0j0P
+
vnoremap <Leader>ma yo<Esc>p^y$V:!perl -e '$x = <C-R>"; print $x'<CR>-y0j0P
 
</pre>
 
</pre>
   
 
Evaluate an expression contained on the full current line and replace the current line with the answer:
 
Evaluate an expression contained on the full current line and replace the current line with the answer:
 
<pre>
 
<pre>
nnoremap &lt;Leader&gt;mr ^"gy0^y$V:!perl -e '$x = &lt;C-R&gt;"; print $x'&lt;CR&gt;^"gP
+
nnoremap <Leader>mr ^"gy0^y$V:!perl -e '$x = <C-R>"; print $x'<CR>^"gP
 
</pre>
 
</pre>
   
 
Evaluate an expression contained in a visual selection and replace the visual selection with the answer:
 
Evaluate an expression contained in a visual selection and replace the visual selection with the answer:
 
<pre>
 
<pre>
vnoremap &lt;Leader&gt;mr "aygvrXgv"by:r !perl -e '$x = &lt;C-R&gt;a; print $x'&lt;CR&gt;0"cyWddk:s/&lt;C-R&gt;b/&lt;C-R&gt;c/&lt;CR&gt;
+
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>
 
</pre>
 
</pre>
   
 
==Comments==
 
==Comments==
Or you could use <tt>:perldo</tt> instead of shelling out.
+
Or you could use <code>:perldo</code> instead of shelling out.
   
 
----
 
----
Line 44: Line 44:
   
 
----
 
----
I found the problem with the last one: it does work with there is only one line in the buffer. This works (assuming `calcu &lt;expr&gt;' is something to calculate the expression:
+
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:
   
 
<pre>
 
<pre>
vnoremap &lt;silent&gt; &lt;Leader&gt;mr "aygvrXgv"by:r !calcu &lt;C-R&gt;a&lt;CR&gt;km`j0"cyWdd``:s/&lt;C-R&gt;b/&lt;C-R&gt;c/&lt;CR&gt;$
+
vnoremap <silent> <Leader>mr "aygvrXgv"by:r !calcu <C-R>a<CR>km`j0"cyWdd``:s/<C-R>b/<C-R>c/<CR>$
 
</pre>
 
</pre>
   
Line 53: Line 53:
   
 
<pre>
 
<pre>
nnoremap &lt;silent&gt; &lt;Leader&gt;ma yypV:!calcu &lt;C-R&gt;"&lt;CR&gt;k$
+
nnoremap <silent> <Leader>ma yypV:!calcu <C-R>"<CR>k$
vnoremap &lt;silent&gt; &lt;Leader&gt;ma yo&lt;ESC&gt;pV:!calcu &lt;C-R&gt;"&lt;CR&gt;k$
+
vnoremap <silent> <Leader>ma yo<Esc>pV:!calcu <C-R>"<CR>k$
nnoremap &lt;silent&gt; &lt;Leader&gt;mr yyV:!calcu &lt;C-R&gt;"&lt;CR&gt;$
+
nnoremap <silent> <Leader>mr yyV:!calcu <C-R>"<CR>$
 
</pre>
 
</pre>
   
Line 62: Line 62:
   
 
<pre>
 
<pre>
vnoremap &lt;silent&gt; &lt;Leader&gt;mr ygvmaomb:r !calcu &lt;C-R&gt;"&lt;CR&gt;"ay$dd`bv`a"ap$
+
vnoremap <silent> <Leader>mr ygvmaomb:r !calcu <C-R>"<CR>"ay$dd`bv`a"ap$
 
</pre>
 
</pre>
  +
  +
----
  +
  +
In addition to bccalc and EvalSelection, there are VimCalc (requires Python) {{script|id=3329}} and Crunch (pure VimL) {{script|id=4686}}. Taking in account these plugins, maybe tips [[VimTip1235|Tip 1235 Scientific calculator]] and [[VimTip73|Tip 73 Using vim as calculator]] could be merge with this.
   
 
----
 
----

Latest revision as of 16:56, 30 November 2013

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.