I have a question using ex in a bash script.
I have a file like that
hello
foo
bar
foo
bar
hello
foo
bar
foo
bar
hello
Now I want to run a little macro over all patterns matching "hello" and I want to modify it to "hello world".
I'm using this script for that
ex -s data.txt << script
let @q="A world"
g/hello/normal @q
wq
script
I define a macro and put it to register q. Afterwards I execute this macro in all lines matching "hello". But it looks like that it's not working.
Now I'm wondering if ex scripting is limited somehow.
I know that there are hundreds of ways to achieve the same differently, but I want to use a macro in that case.
Thanks for your help!
There is ex
and ex
. Depending on what program is actually invoked when you do ex
, the available features (or the way they interact with various config files) may or may not match with your expectations so it is strongly advised to read its documentation before using it.
As an example, let @q = "something"
and normal @q
are typical Vim idioms and I knew for sure that my ex
is actually a fairly usable Vim build, so I knew beforehand that your snippet would work as expected on my machine:
But the ex
used in your specific case may be a crippled Vim, or it may not be Vim at all, in which case using Vim idioms may not be a wise move. As an example, here is what I get running your snippet with real ex
:
Adding the full output of $ ex --version
to your question should help in clarifying the situation.
I know that you are not interested in alternatives, but a more defensive approach would be to limit yourself to the subset of features that was part of real ex
. Something like:
$ ex -s data.txt << script
g/hello/s/$/ world/
wq
script