## FANDOM

1,640 Pages

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 216 Printable Monobook Previous Next

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)) =
return (x / 180) * 4 * atan (1)
}
```

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
return (x / 180) * 4 * atan (1)
}
```

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", "", "")
if has_equal == 1
normal `>
else
endif
endfunction
```

## Comments Edit

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.

```"=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, "\\\^", "\\\^\\\^\\\^\\\^", "")
```

These are not equations. They are arithmetic expressions. http://en.wikipedia.org/wiki/Equation Solving equations requires more complex software such as GNU Octave.

Community content is available under CC-BY-SA unless otherwise noted.