I am using zsh with the [prezto](https://github.com/sorin-ionescu/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
1. **Use a program**: I don't want to type python -c
before my term or pipe it into bc
. That's too much typing and I hope it can be done better.
2. **Use a function or an alias**: There are some [solutions](https://github.com/arzzen/calc.plugin.zsh) out there that allow to do these calculation when prefing it with a c
or =
or calc
, like in = 1 + 2 * (3 / 4)
. That is nice but basically the same thing as point 1.
3. **Use another shell**: I know about [xonsh](https://xon.sh/) 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
4. **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.
5. **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.
6. **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.
Asked by Kaligule
(113 rep)
Apr 29, 2022, 09:27 PM
Last activity: Apr 30, 2022, 01:52 PM
Last activity: Apr 30, 2022, 01:52 PM