suppressing 'ansi' control output?

27 views
Skip to first unread message

Ron Aaron

unread,
Mar 25, 2021, 1:53:07 AM3/25/21
to vim_dev
I want to get the vim version for a bash script. The way I'm trying to use is:

vim --not-a-term --cmd 'echo v:versionlong|quit'

However, that spews a bunch of ANSI terminal control stuff as well, which I don't want. Is there an option to suppress that output?

Ron Aaron

unread,
Mar 25, 2021, 2:45:29 AM3/25/21
to vim_dev
So what I've come up with is the following, though it seems way too verbose:

vim --not-a-term --cmd 'redir! >/tmp/vimver.txt|echon v:versionlong|quit'
verlong=`cat /tmp/vimver.text`

Gary Johnson

unread,
Mar 25, 2021, 3:52:45 AM3/25/21
to vim_dev
I was able to suppress it by adding the argument

--cmd 'set t_cd= t_ce= t_cm=\ t_ve= t_vi='

like this, all on one line:

vim --not-a-term --cmd 'set t_cd= t_ce= t_cm=\ t_ve= t_vi=' --cmd 'echo v:versionlong|quit'

That clears the values of all those escape sequences in the output.
Vim loudly complains if the t_cm sequence is empty, so I just set it
to a space.

It seems to me that the --not-a-term option is not doing everything
it should if Vim outputs terminal escape sequences when no terminal
is supposed to be there.

Regards,
Gary

Christian Brabandt

unread,
Mar 25, 2021, 4:00:32 AM3/25/21
to vim_dev
Have you tried TERM=dumb vim --cmd 'echo v:versionlong|quit'?


Best
Christian
--
Sich selbst etwas versprechen und es nicht halten ist der nächste Weg
zur Nullität und Charakterlosigkeit.
-- Christian Friedrich Hebbel

Gary Johnson

unread,
Mar 25, 2021, 4:25:26 AM3/25/21
to vim_dev
On 2021-03-25, Christian Brabandt wrote:
> On Mi, 24 Mär 2021, Ron Aaron wrote:
>
> > I want to get the vim version for a bash script. The way I'm trying to use is:
> >
> > vim --not-a-term --cmd 'echo v:versionlong|quit'
> >
> > However, that spews a bunch of ANSI terminal control stuff as well, which I don't want. Is there an option to suppress that output?
>
> Have you tried TERM=dumb vim --cmd 'echo v:versionlong|quit'?

I did. It eliminated all but t_cm and added a ^M.

That would have simplified my solution, but I tried it early on in
my experiments and saw the output only as escape sequence stuff.
I didn't understand the individual sequences at the time and didn't
think about it later when I was eliminating escape sequences one by
one.

Regards,
Gary

Ron Aaron

unread,
Mar 25, 2021, 9:51:56 AM3/25/21
to vim_dev
That doesn't work for me. Redirected to a file (and it complains there's no terminal), and the output of the file via xxd is:

00000000: 1b5b 3338 3b31 4838 3032 3236 3530 0d0d  .[38;1H8022650..
00000010: 0a

Gary Johnson

unread,
Mar 26, 2021, 6:56:49 PM3/26/21
to vim_dev
On 2021-03-25, Gary Johnson wrote:
> On 2021-03-25, Christian Brabandt wrote:
> > On Mi, 24 Mär 2021, Ron Aaron wrote:
> >
> > > I want to get the vim version for a bash script. The way I'm trying to use is:
> > >
> > > vim --not-a-term --cmd 'echo v:versionlong|quit'
> > >
> > > However, that spews a bunch of ANSI terminal control stuff as well, which I don't want. Is there an option to suppress that output?
> >
> > Have you tried TERM=dumb vim --cmd 'echo v:versionlong|quit'?
>
> I did. It eliminated all but t_cm and added a ^M.

I think this is partly a bug in Vim. There are a few places in the
code where Vim assumes that it is running in a terminal as it
normally does and should output terminal control sequences without
regard to the presence of --not-a-term.

I have attached a patch that seems to fix that problem. If someone
would rather I submitted a PR, I can do that, too. Since this is
just a proposal at this point, the patch was just easier for me.

Even with the patch, though, I think using :echo to send something
to stdout is incorrect. The :echo command is intended to write to
the screen and does so under the control of Vim's screen management.
I think it would be a mistake to modify that code to have :echo
write to the screen or directly to stdout. I think a better
approach is to use echoraw() when you want to write directly to
stdout.

That would change your command to this:

vim --not-a-term --cmd 'call echoraw(v:versionlong)|quit'

If you want to put a newline on the end of that, use this:

vim --not-a-term --cmd 'call echoraw(v:versionlong."\n")|quit'

Of course, neither of those will do what you want without my patch
applied. We'll have to see if Bram et al. think that's a good idea.

Regards,
Gary
gj_not-a-term_fixes.diff

Bram Moolenaar

unread,
Mar 27, 2021, 7:13:51 AM3/27/21
to vim...@googlegroups.com, Gary Johnson
Well, it's better than adding yet another command line argument.
There is a tiny chance --not-a-term was only added to suppress the 2
second delay and still wanting to catch all the output. Let's see if
anything has this problem.

--
One difference between a man and a machine is that a machine is quiet
when well oiled.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Christian Brabandt

unread,
Mar 27, 2021, 12:25:24 PM3/27/21
to vim...@googlegroups.com

On Sa, 27 Mär 2021, Bram Moolenaar wrote:

> There is a tiny chance --not-a-term was only added to suppress the 2
> second delay and still wanting to catch all the output. Let's see if
> anything has this problem.

Yes, IIRC it was added to avoid the 2 second delay (and also the warning
message) when the test suite is run on the CI systems.

Best,
Christian
--
Ein Zwerg ist klein, selbst wenn er auf einem Berge steht;
ein Riese bleibt groß, selbst wenn er in einem Tal steht.
-- Lucius Annaeus Seneca (4-65 n.Chr.)
Reply all
Reply to author
Forward
0 new messages