I am using zsh with the prezto configuration framework. I want to be able to type in terms like 1 + 2 * (3 / 4) and get "2.5" out of it.
Those terms could be recognized because they only consist of certain characters: 0123456789 +-*/(). For everything more complicated I am fine with opening a dedicated program (like a python shell).
solutions that are not good enough
- Use a program: I don't want to type
python -cbefore my term or pipe it intobc. That's too much typing and I hope it can be done better. - Use a function or an alias: There are some solutions out there that allow to do these calculation when prefing it with a
cor=orcalc, like in= 1 + 2 * (3 / 4). That is nice but basically the same thing as point 1. - Use another shell: I know about xonsh and I am sure there are other shells that can do that. But I like zsh and I don't want to change shells.
what might work
- zsh plugin: I could imagine a zsh-plugin that finds if a command matches a certain regex and then pipes it into python/bc/whatever. Somewhat similar to what the automatic cd thing does when you enter just the name of a directory. I don't know if this is possible though. I would love to hear some hints so I could write that.
- Intercept
command not found: Similar to 4. perhaps it is possible to intercept the behaviour of zsh when a command is not found. But again, I don't know how I would do that. - alias all the numbers: I guess you could write an alias for all the numbers so that they are commands that take the rest of the math-string as arguments. That might work, but seems pretty hacky. Also 'all the numbers' is quite a lot aliases, even when autogenerating the code for it.
I would like to hear ideas on how to tackle this problem. I am not afraid of writing code, but I would prefere a clean solution over a hacky one.