2

Does anybody know why this works with bash calculator (bc) in vim:

echo system ("echo 3+5 \| dos2unix \| bc -l") ---> output: 8   
echo system ("echo 3/5 \| dos2unix \| bc -l") ---> output: .600000000000000  
echo system ("echo 3*5 \| dos2unix \| bc -l") ---> output: 15   

but this not:

echo system ("echo 2^5 \| dos2unix \| bc -l") ---> output: 25  
echo system ("echo 3^5 \| dos2unix \| bc -l") ---> output: 35  

This is used under cygwin in Windows.

imp25
  • 2,327
  • 16
  • 23
Reman
  • 7,931
  • 11
  • 55
  • 97
  • I don't know why, but have you tried to escape the ^-character. For example echo system ("echo 2\^5 \| dos2unix \| bc -l") – Ahe Nov 09 '12 at 11:03
  • 1
    It looks like the `^` is causing problems. On my Mac, `2^5` is interpreted as `255` and on your PC it is interpreted as `25`. Not sure how to fix it besides playing around with multiple `^`. – romainl Nov 09 '12 at 11:04
  • @Ahe, yes I tried to escape the ^ character. Same output. – Reman Nov 09 '12 at 11:11
  • @Romain, yes with 4 `^` characters it works. `2^^^^5` --> output: `32`, but why? – Reman Nov 09 '12 at 11:44
  • It's the first time I run into this behavior. I have no idea what would be the cause. – romainl Nov 09 '12 at 12:48
  • I think it has something to do with Vim's somewhat archaic input system. The command line likely interprets `^5` as `Ctrl+5` which produces `5`. It sucks. Did you know that Vim can do some calculations? While you are inserting text, hit `=2+5`. It doesn't do `^`, though, but you can use `pow()`: `:=pow(2,5)`. – romainl Nov 09 '12 at 12:55
  • 1
    @romainl If you input `^` then `5` you will *never* get ``, same for any `^{Char}`. If you are not using mappings you won’t even get `` treated specially (unless it is `\` inside double quoted string of course). Vim archaic input system is not ever used when calling functions. In any case, vim does not support ``, so there is no way to get `^5` shown in a special color by `` either. – ZyX Nov 09 '12 at 18:00

1 Answers1

2

What is the value of &shell option? It must be /path/to/cygwin/bash, not something ending with cmd.exe. The problem is that ^ is an escape character in windows cmd.exe, so echo 2^5 is somewhat equivalent to echo 2\5 resolving into echo 25.

If it is cmd.exe add a line

set shell=/path/to/cygwin/bash

to your vimrc.

Note though that echo 2^^^^5 resolving to echo 2^5 is somewhat strange (in plain cmd.exe it is echo 2^^5), but I saw very long discussion on vim-dev regarding escaping issues for cmd.exe some time ago, so it may be one of them. Should not happen on the most recent vim (or it is a bug).

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • hi Zyx, you're absolutely right. The shell was the windows shell. I installed bash calculator months ago also in windows (gnu bc). You're right echo 2^^5 works also. 2^5 not, 2^^^5 not, 2^^^^5 works also. – Reman Nov 09 '12 at 19:03
  • Zyx, I was wrong yesterday. `2^^5` (two carets) doesn't work (it gives an answer but not the correct one). The only one which works is 4 carets. `2^^^^5`. – Reman Nov 10 '12 at 08:17