Wikia

Vim Tips Wiki

Calculate equations from within vim

Talk0
1,612pages on
this wiki
Revision as of 00:49, April 25, 2008 by JohnBot (Talk | contribs)

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

Around Wikia's network

Random Wiki