No edit summary |
(Undo revision 36710 by 75.147.94.245 (talk) command was correct, but is normal-mode command not command-line one) |
||
(9 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{duplicate|216|1235|1349|1359}} |
||
{{review}} |
{{review}} |
||
{{TipImported |
{{TipImported |
||
Line 9: | Line 10: | ||
|version=5.7 |
|version=5.7 |
||
|rating=95/57 |
|rating=95/57 |
||
+ | |category1= |
||
+ | |category2= |
||
}} |
}} |
||
− | Basic calculations can done within Vim easily by typing (insert-mode): |
+ | Basic calculations using integers can done within Vim easily by typing (insert-mode): |
CTRL-R followed by = then, for example, 2+2 and press Enter. |
CTRL-R followed by = then, for example, 2+2 and press Enter. |
||
The result 4 will be inserted into the document. |
The result 4 will be inserted into the document. |
||
+ | |||
+ | ==See also== |
||
+ | *[[VimTip73|Tip 73 Using vim as calculator]] Built-in integers (this tip). |
||
+ | *[[VimTip216|Tip 216 Calculate equations from within vim]] Uses Linux bc. |
||
+ | *[[VimTip987|Tip 987 Easy floating point arithmetic]] uses Perl. |
||
+ | *[[VimTip1070|Tip 1070 Inline integer arithmetic]] only works on integers. |
||
+ | *[[VimTip1235|Tip 1235 Scientific calculator]] Uses Python. |
||
+ | *[[VimTip1349|Tip 1349 Calculator Editing]] Uses Linux bc. |
||
+ | *[[VimTip1359|Tip 1359 Calculator and code evaluation using Perl]] Uses Perl. |
||
==Comments== |
==Comments== |
||
Adding the following map to your vimrc |
Adding the following map to your vimrc |
||
− | ino |
+ | ino <C-A> <C-O>yiW<End>=<C-R>=<C-R>0<CR> |
− | Then, just type 8*8 |
+ | Then, just type 8*8<C-A> |
you will get 8*8=64 |
you will get 8*8=64 |
||
Line 32: | Line 44: | ||
---- |
---- |
||
− | Passing to the unix utility bc which can do advanced math is very useful. |
+ | Passing to the unix utility bc which can do advanced math is very useful. To pipe the current line to bc and replace with it with the result, type (in normal mode): |
<pre> |
<pre> |
||
!!bc |
!!bc |
||
</pre> |
</pre> |
||
+ | To pipe the lines that are currently selected in visual mode to bc and replace with it with the result: |
||
− | for the current line. In visual mode, simply: |
||
<pre> |
<pre> |
||
!bc |
!bc |
||
</pre> |
</pre> |
||
− | to evaluate the selected text. |
||
---- |
---- |
||
I have a mapping for passing a line to bc: |
I have a mapping for passing a line to bc: |
||
− | map gbc yypkA = |
+ | 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: |
I guess it could be made a bit better. Another useful mapping I once needed was summing numbers on multiple lines: |
||
<pre> |
<pre> |
||
− | vmap gs y' |
+ | vmap gs y'>p:'[,']-1s/$/+/\|'[,']+1j!<CR>'[0"wy$:.s§.*§\=w§<CR>'[yyP:.s/./=/g<CR>_j |
</pre> |
</pre> |
||
Revision as of 17:25, 4 September 2013
Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
created June 5, 2001 · complexity basic · author Volker Duetsch · version 5.7
Basic calculations using integers can done within Vim easily by typing (insert-mode):
CTRL-R followed by = then, for example, 2+2 and press Enter.
The result 4 will be inserted into the document.
See also
- Tip 73 Using vim as calculator Built-in integers (this tip).
- Tip 216 Calculate equations from within vim Uses Linux bc.
- Tip 987 Easy floating point arithmetic uses Perl.
- Tip 1070 Inline integer arithmetic only works on integers.
- Tip 1235 Scientific calculator Uses Python.
- Tip 1349 Calculator Editing Uses Linux bc.
- Tip 1359 Calculator and code evaluation using Perl Uses Perl.
Comments
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
For simple arithmetic, 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).
Passing to the unix utility bc which can do advanced math is very useful. To pipe the current line to bc and replace with it with the result, type (in normal mode):
!!bc
To pipe the lines that are currently selected in visual mode to bc and replace with it with the result:
!bc
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