(Move categories to tip template) |
(Remove html character entities) |
||
Line 45: | Line 45: | ||
<pre> |
<pre> |
||
− | vnoremap ;bc "ey:call CalcBC() |
+ | vnoremap ;bc "ey:call CalcBC()<CR> |
function! CalcBC() |
function! CalcBC() |
||
let has_equal = 0 |
let has_equal = 0 |
||
Line 67: | Line 67: | ||
" append answer or echo |
" append answer or echo |
||
if has_equal == 1 |
if has_equal == 1 |
||
− | normal ` |
+ | normal `> |
exec "normal a" . answer |
exec "normal a" . answer |
||
else |
else |
||
Line 77: | Line 77: | ||
==Comments== |
==Comments== |
||
Please do not give such things as tips! Make this as a script and put this in the scripts section. Starting from version 6.0 (which is hopefully what most people are using), |
Please do not give such things as tips! Make this as a script and put this in the scripts section. Starting from version 6.0 (which is hopefully what most people are using), |
||
− | vim provides the ability to have "plugins". (:he plugin). imho, |
+ | vim provides the ability to have "plugins". (:he plugin). imho, it's bad practice to keep extending your ~/.vimrc. the rc file should be used for tweaking Vim's settings and such. |
it is not a good place to put functions etc. this make the .vimrc very bloated. |
it is not a good place to put functions etc. this make the .vimrc very bloated. |
||
Line 86: | Line 86: | ||
See also {{help|id=quote=}} about the expression register. E.g., in command mode, type |
See also {{help|id=quote=}} about the expression register. E.g., in command mode, type |
||
− | "=10+15 |
+ | "=10+15<enter>p |
− | (where |
+ | (where <enter> means press the Enter key) will put 25 into your file at the current cursor position. See {{help|functions}} for the list of functions that Vim supports (no maths functions). |
---- |
---- |
||
Line 97: | Line 97: | ||
<pre> |
<pre> |
||
" escape chars for shell |
" escape chars for shell |
||
− | "not for NT: let @e = escape (@e, '*();& |
+ | "not for NT: let @e = escape (@e, '*();&><|') |
" windows echo must escape the caret with the caret, repeat if piped |
" windows echo must escape the caret with the caret, repeat if piped |
||
" backslashes needed to supress evaluation within vim |
" backslashes needed to supress evaluation within vim |
Revision as of 05:23, 29 September 2008
created February 16, 2002 · complexity basic · author scotch2 · version 5.7
The following map and function calculates equations using the program 'bc' (found on most Linux systems, available for most systems). Visually select the equation you want to calculate, then hit ;bc - if the selection ends with an '=' sign, the answer will be appended after the equal, otherwise, the answer is echoed as a message. The code to put in a vimrc and source is at the end.
Equations can span multiple lines, and the full bc syntax is probably supported. Additionally, sin (), cos (), etc, are transformed into the names used by bc (s () c (), etc).
Here are some example lines:
2 * sqrt (2) = 3 * (2 - 1) + 4.0 ^ 6 = 4 / 3 = 3 + 4 - 2 * (1 / (3 + 2)) = define rad (x) { return (x / 180) * 4 * atan (1) } cos (rad (45)) =
Select each of these in turn and hit ;bc for each. This is what you get:
2 * sqrt (2) = 2.82842712474619009760 3 * (2 - 1) + 4.0 ^ 6 = 4099.000000 4 / 3 = 1.33333333333333333333 3 + 4 - 2 * (1 / (3 + 2)) = 6.60000000000000000000 define rad (x) { return (x / 180) * 4 * atan (1) } cos (rad (45)) = .70710678118654752440
Here is the code you need to put in your vimrc file:
vnoremap ;bc "ey:call CalcBC()<CR> function! CalcBC() let has_equal = 0 " remove newlines and trailing spaces let @e = substitute (@e, "\n", "", "g") let @e = substitute (@e, '\s*$', "", "g") " if we end with an equal, strip, and remember for output if @e =~ "=$" let @e = substitute (@e, '=$', "", "") let has_equal = 1 endif " sub common func names for bc equivalent let @e = substitute (@e, '\csin\s*(', "s (", "") let @e = substitute (@e, '\ccos\s*(', "c (", "") let @e = substitute (@e, '\catan\s*(', "a (", "") let @e = substitute (@e, "\cln\s*(", "l (", "") " escape chars for shell let @e = escape (@e, '*()') " run bc, strip newline let answer = substitute (system ("echo " . @e . " \| bc -l"), "\n", "", "") " append answer or echo if has_equal == 1 normal `> exec "normal a" . answer else echo "answer = " . answer endif endfunction
Comments
Please do not give such things as tips! Make this as a script and put this in the scripts section. Starting from version 6.0 (which is hopefully what most people are using), vim provides the ability to have "plugins". (:he plugin). imho, it's bad practice to keep extending your ~/.vimrc. the rc file should be used for tweaking Vim's settings and such. it is not a good place to put functions etc. this make the .vimrc very bloated.
Good points - I've uploaded it as script#219 with a fix that escapes ";" chars.
See also :help quote= about the expression register. E.g., in command mode, type
"=10+15<enter>p
(where <enter> means press the Enter key) will put 25 into your file at the current cursor position. See :help functions for the list of functions that Vim supports (no maths functions).
I found a bc.exe which does at least some of the calculations on Windows 2000 (http://unxutils.sourceforge.net/). Adaptions for Windows 2000 using bc.exe from sourceforge:
Change the lines in the script as:
" escape chars for shell "not for NT: let @e = escape (@e, '*();&><|') " windows echo must escape the caret with the caret, repeat if piped " backslashes needed to supress evaluation within vim let @e = substitute (@e, "\\\^", "\\\^\\\^\\\^\\\^", "")