Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vim -s scriptfile not working as expected?

30 views
Skip to first unread message

Janis Papanagnou

unread,
Oct 30, 2022, 6:53:05 PM10/30/22
to
I tried vim's option -s for the first time. It is documented that
the -s command-file shall contain the command characters as typed
regularly in vim. My test case is

:%s/></>^M</g

where the ^M is created (on Linux) by typing <Ctrl-V> and <Enter>.

If I type that command inside a vim session newlines are inserted
between subsequent > and < characters. If I have that command in a
file and use vim -s scriptfile the command seems to be ineffective.

Hints and workarounds welcome.

BTW, I tried that vim option -s in a simple test that worked well

:6,8d
2Gdd:wq

so I suppose vim has problems with the ^M in context of option -s.

$ vim --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Nov 24 2016 00:51:32)
Included patches: 1-429
Extra patches: 8.0.0056
...

Janis

Janis Papanagnou

unread,
Oct 30, 2022, 7:06:20 PM10/30/22
to
On 30.10.2022 23:53, Janis Papanagnou wrote:
> I tried vim's option -s for the first time. It is documented that
> the -s command-file shall contain the command characters as typed
> regularly in vim. My test case is
>
> :%s/></>^M</g
>
> where the ^M is created (on Linux) by typing <Ctrl-V> and <Enter>.
>
> If I type that command inside a vim session newlines are inserted
> between subsequent > and < characters. If I have that command in a
> file and use vim -s scriptfile the command seems to be ineffective.
>
> Hints and workarounds welcome.

Found the solution or workaround...

Instead of typing <Ctrl-V> and <Enter> to create the <Ctrl-M> you
have to type <Ctrl-V> <Ctrl-V> <Ctrl-V> and <Enter> .

The documentation is misleading in this case.

Janis

Tony Mountifield

unread,
Nov 1, 2022, 8:43:24 AM11/1/22
to
In article <tjn01a$a3r6$1...@dont-email.me>,
Or you should be able to use \n instead:

:%s/></>\n</g

Cheers
Tony
--
Tony Mountifield
Work: to...@softins.co.uk - http://www.softins.co.uk
Play: to...@mountifield.org - http://tony.mountifield.org

Janis Papanagnou

unread,
Nov 1, 2022, 10:58:27 AM11/1/22
to
On 01.11.2022 13:42, Tony Mountifield wrote:
> In article <tjn01a$a3r6$1...@dont-email.me>,
> Janis Papanagnou <janis_pap...@hotmail.com> wrote:
>> On 30.10.2022 23:53, Janis Papanagnou wrote:
>>> I tried vim's option -s for the first time. It is documented that
>>> the -s command-file shall contain the command characters as typed
>>> regularly in vim. My test case is
>>>
>>> :%s/></>^M</g
>>>
>>> where the ^M is created (on Linux) by typing <Ctrl-V> and <Enter>.
>>>
>>> If I type that command inside a vim session newlines are inserted
>>> between subsequent > and < characters. If I have that command in a
>>> file and use vim -s scriptfile the command seems to be ineffective.
>>>
>>> Hints and workarounds welcome.
>>
>> Found the solution or workaround...
>>
>> Instead of typing <Ctrl-V> and <Enter> to create the <Ctrl-M> you
>> have to type <Ctrl-V> <Ctrl-V> <Ctrl-V> and <Enter> .
>>
>> The documentation is misleading in this case.
>
> Or you should be able to use \n instead:
>
> :%s/></>\n</g

I tried that already. - If I issue this sequence from within an
interactive (non-scripted) vim session it gets *not* expanded to
a newline character but - strangely - to an ASCII NUL character.

Janis

>
> Cheers
> Tony
>

Spiros Bousbouras

unread,
Nov 2, 2022, 5:11:15 AM11/2/22
to
\r instead of \n should work.

:help s/\\n

The page has a few helpful examples.

Regarding the more general issues , for scripts (as opposed to the vim
command line) , I prefer to be verbose and unambiguous. So , instead of
coming up with a special key combination to insert within the script a
special character , I would put instead in the script

execute "%substitute /></>\r</g"

Having said that , with a construct like vim -s script file ,
it is not obvious to me when exactly the commands in script will be
executed. Is it after loading file into memory ? What happens with
a construct like vim -s script file1 file2 ? I remember that somewhere
in the vim documentation it explains in which order events happen but
I don't remember where in the documentation. I would be wary of doing stuff
like vim -s script file is the script makes substantial changes in the
file.

--
A computer scientist is neither a computer nor a scientist.

Stan Brown

unread,
Nov 2, 2022, 10:50:06 AM11/2/22
to
On Wed, 2 Nov 2022 09:11:13 -0000 (UTC), Spiros Bousbouras wrote:
> Having said that , with a construct like vim -s script file ,
> it is not obvious to me when exactly the commands in script will be
> executed. Is it after loading file into memory ? What happens with
> a construct like vim -s script file1 file2 ? I remember that somewhere
> in the vim documentation it explains in which order events happen but
> I don't remember where in the documentation.

:h startup

Mostly out of inertia, I'm still on 7.4, but I don't imagine old help
searches will have stopped working in newer versions.

--
Stan Brown, Tehachapi, California, USA https://BrownMath.com/
Shikata ga nai...

Janis Papanagnou

unread,
Nov 2, 2022, 11:19:24 AM11/2/22
to
On 02.11.2022 10:11, Spiros Bousbouras wrote:
>
> Having said that , with a construct like vim -s script file ,
> it is not obvious to me when exactly the commands in script will be
> executed. Is it after loading file into memory ? What happens with
> a construct like vim -s script file1 file2 ?

For the test-script I had
:6,8d
2Gdd:wq
these commands will be executed on file1, and the :wq will result in a
message that there's another file to be edited. It seems you can take
the man page comment literally:
"The characters in the file are interpreted as if you had typed them."
which would let the files on the argument list be edited individually,
one after the other as done interactively.

Decades ago I recall to have done vi-"scripting" using something like
echo "..." | ex
and got some strange effects (e.g. spurious messages). Vim's purpose
of option -s may be (primarily?) to not show these effects.

I seem to recall - but I never used that myself - that there's another
vim feature if you want the same replacements/actions done on every
file given as argument. (Or am I misremembering?)

Janis

0 new messages