Getting the value of runtimepath from the command line

250 views
Skip to first unread message

HarleyPig

unread,
May 6, 2012, 12:06:09 AM5/6/12
to vim...@googlegroups.com
I'm writing a script to generate my own version of the vim docs and I'm trying to find a better way of getting the information I need.

The variable 'runtimepath' contains the paths of all the directories that will be searched for doc and tags files. So, I want to check each of these directories.

If I run the following command I get the default or base set of directories.

$ vim --cmd 'echo &rtp' --cmd 'q' /dev/null
/home/harleypig/.vim,/usr/share/vim/vimfiles,/usr/share/vim/vim73,/usr/share/vim/vimfiles/after,/home/harleypig/.vim/after

But that's incomplete. The '--cmd' is run *before* any configuration files are parsed and handled. However, if I run the same command with the '-c' switch, which is run *after* configuration files are handled, I get all the correct directories--I also get a bunch of ANSI escape sequences. I have to dump it in a file to see it, the codes clear the screen.

$ vim -c 'echo &rtp' -c 'q' /dev/null > t
Vim: Warning: Output is not to a terminal

I've tried filtering it through 'col' and 'col -b' but that just strips the ESC codes, leaving a jumbled mess.

I'm using perl's Regexp::Common::ANSIescape to strip out the escape codes, and it seems to be reliable so far. But it would be better if there weren't any ansi codes in the first place.

So. Is there a way to get the value from vim without the ansi codes?

John Little

unread,
May 6, 2012, 4:57:47 AM5/6/12
to vim...@googlegroups.com
How about

vim -c "redir! > $RTP" -c 'echo &rtp' -c 'redir END' -c 'q'

given $RTP set to an appropriate target. Starts the output with a blank line, I don't know why.

Regards, John

HarleyPig

unread,
May 6, 2012, 11:13:00 AM5/6/12
to vim...@googlegroups.com
On Sunday, May 6, 2012 2:57:47 AM UTC-6, John Little wrote:
> vim -c "redir! > $RTP" -c 'echo &rtp' -c 'redir END' -c 'q'

That did it, though I was hoping to avoid temp files.

> given $RTP set to an appropriate target. Starts the output with a blank line, I don't know why.

I think that has to do with not actually editing a file.

Thanks for the help.

Gary Johnson

unread,
May 6, 2012, 5:06:08 PM5/6/12
to vim...@googlegroups.com
On 2012-05-05, HarleyPig wrote:
> I'm writing a script to generate my own version of the vim docs
> and I'm trying to find a better way of getting the information I
> need.
>
> The variable 'runtimepath' contains the paths of all the
> directories that will be searched for doc and tags files. So, I
> want to check each of these directories.
>
> If I run the following command I get the default or base set of
> directories.
>
> $ vim --cmd 'echo &rtp' --cmd 'q' /dev/null
> /home/harleypig/.vim,/usr/share/vim/vimfiles,/usr/share/vim/vim73,/usr/share/vim/vimfiles/after,/home/harleypig/.vim/after
>
> But that's incomplete. The '--cmd' is run *before* any
> configuration files are parsed and handled. However, if I run the
> same command with the '-c' switch, which is run *after*
> configuration files are handled, I get all the correct
> directories--I also get a bunch of ANSI escape sequences. I have
> to dump it in a file to see it, the codes clear the screen.
>
> $ vim -c 'echo &rtp' -c 'q' /dev/null > t
> Vim: Warning: Output is not to a terminal

> So. Is there a way to get the value from vim without the ansi
> codes?

Try this:

$ vim -E -c 'echo &rtp' -c 'q'

HTH,
Gary

HarleyPig

unread,
May 6, 2012, 6:49:17 PM5/6/12
to vim...@googlegroups.com
On Sunday, May 6, 2012 3:06:08 PM UTC-6, Gary Johnson wrote:
> $ vim -E -c 'echo &rtp' -c 'q'

That still leaves ansi escape codes in the output.

Christian Brabandt

unread,
May 7, 2012, 6:05:52 AM5/7/12
to vim...@googlegroups.com
I think this is because the echo always prints on a new line, so prepends
a "\n" to all it's output.

regards,
Christian

John Little

unread,
May 7, 2012, 7:34:53 AM5/7/12
to vim...@googlegroups.com
On Monday, May 7, 2012 3:13:00 AM UTC+12, HarleyPig wrote:

> That did it, though I was hoping to avoid temp files.

A challenge left hanging... I foolishly took the bait, though I'm not claiming success, such inelegance as follows cannot be termed such.

One can do this with coprocess trickery, if you have bash version 4:

coproc vim 10>&1- &>/dev/null -c 'exe "!echo \"" . &rtp. "\" >&10"' -c q
read -u${COPROC[0]} rtp
eval "exec ${COPROC[1]}<&-"

Regards, John

HarleyPig

unread,
May 7, 2012, 10:01:41 AM5/7/12
to vim...@googlegroups.com
On Monday, May 7, 2012 5:34:53 AM UTC-6, John Little wrote:
> A challenge left hanging... I foolishly took the bait, though I'm not claiming success, such inelegance as follows cannot be termed such.
[snip scary looking stuff]

:D I didn't mean that as a challenge, but thanks for the hour's worth of yak-shaving exploring coproc. But, I'm using perl and that would make it more complicated. This is what I've done, if anyone's curious.

my @doc_dirs = do {

my $fh = File::Temp->new;
my $tmpfile = $fh->filename;

# Thanks to John Little on vim_use for the pointer to redir.
# https://groups.google.com/d/msg/vim_use/tGQdQX4MB68/kIhYPAMMsTgJ
my @cmd = ( 'vim', '-c', "redir! > $tmpfile", '-c', 'echo &rtp', '-c', 'redir END', '-c', 'q' );
my ( undef, $err, $syserr, $time ) = run3( \@cmd );

warn "Took $time seconds to run @cmd\n";
die "err: ($syserr) $err\n" if $syserr;
die "$err\n" if $err ne '' && $err ne 'Vim: Warning: Output is not to a terminal';

map { "$_/doc" }
grep { ! /^$/ }
split /[,\s]+/, do { local $/ ; <$fh> };

};

Christian Brabandt

unread,
May 7, 2012, 10:18:13 AM5/7/12
to vim...@googlegroups.com
This works for me, doesn't leave any Ansi escapes codes around and
avoids temp files:

vim -Esc 'verbose echo &rtp|q'

I think the verbose is needed, because otherwise -s will silent out
all output (like the ANSI escape sequences and the echo message).

You might need to prevent messages from some plugins, e.g. here the
CSApprox plugin kept on complaining that the terminal had no colors
available (so I set --cmd 'let g:CSApprox_verbose_level=0' in the
command line.

regards,
Christian

HarleyPig

unread,
May 7, 2012, 11:17:44 AM5/7/12
to vim...@googlegroups.com
On Monday, May 7, 2012 8:18:13 AM UTC-6, Christian Brabandt wrote:
> vim -Esc 'verbose echo &rtp|q'

That gives me the same result as

vim --cmd 'echo &rtp|q'

Thanks for the hint to use |'s, makes the command so much easier to read.

In both cases above, it appears the command is being run before config files are being sourced. I want to create html files for what the user has available in a normal session though, so any modifications to runtimepath by config files need to be accounted for.

Christian Brabandt

unread,
May 7, 2012, 12:22:37 PM5/7/12
to vim...@googlegroups.com
Oh yes, this is briefly mentioned at :h -s-ex

Anyway, a workaround might be to explicitly source all configuration files,
e.g. something like

vim -u '$VIM/vimrc' -u ~/.vimrc -Esc 'verbose echo &rtp|q'

But this may trigger some file not found errors.

Most probably, vim -u ~/.vimrc -Esc 'verbose echo &rtp|q' should work
however.

regards,
Christian

HarleyPig

unread,
May 7, 2012, 1:00:46 PM5/7/12
to vim...@googlegroups.com
On Monday, May 7, 2012 10:22:37 AM UTC-6, Christian Brabandt wrote:
> Oh yes, this is briefly mentioned at :h -s-ex

I haven't looked at starting.txt in a while. Time to reread it ...

> Most probably, vim -u ~/.vimrc -Esc 'verbose echo &rtp|q' should work
> however.

That worked. From what I understand from the docs this should be fine in most cases. Thanks for the help.

Reply all
Reply to author
Forward
0 new messages