How does one search for a pattern in ex command output? For example, how to search for a file name pattern in the output of :scriptnames
command? In the help for all flavours of grep
and vimgrep
there is only {file}
as a place where to perform search.
3 Answers
You could do:
:redir => scriptn | sil exe 'scriptnames' | redir end | echo(system('grep pattern',scriptn))
What it does:
:redir => scriptn "redirect following output to variable scriptn
:sil exe 'scriptnames' "silently execute scriptnames
:redir end "end the redirection
:echo(system('grep pattern',scriptn)) "echo the call of grep witht that input with the pattern `pattern`
The :filter
command is a simple, one-line approach to this:
:filter /indent/ scriptnames
This would show only lines matching the pattern indent
from the output of the :scriptnames
command.
Unusually, :filter
does not observe either 'ignorecase'
or 'smartcase'
and will always search in a case-sensitive manner. To override this, prepend \c
to your search pattern, e.g.:
:filter /\cindent/ scriptnames
See :h \c
for more info.
I see :redir
-oriented solutions to questions like these often, which are fine and offer a lot of power and options, but I feel they're a bit overkill when more often than not, you just need to quickly check if something is there. :filter
is convenient to use in the moment and dead simple.
-
3
-
1Indeed, this should be the accepted answer. (Learned a lot from edi9999's answer though, hence the upvote.) Jun 27, 2019 at 22:00
-
-
@jdhao Great question! For some reason,
filter
doesn't observe'ignorecase'
like other pattern-using commands. You can however force it by prepending\c
to your pattern, like so::filter /\cindent/ scriptnames
. See also:h \c
. Sep 22, 2020 at 19:56 -
@ZeroKnight Thanks for your prompt reply. I have also figured out
\c
is need to search case-insensitively.– jdhaoSep 23, 2020 at 2:05
First, you need to grab the output of scriptnames
and put it into a buffer.
You can use :redir
for that:
:redir @a " redirect output of following ex commands to register a
:scriptnames " press G to get to the end of the output if it's too long
:redir END " end the redirection
:vnew " new buffer in vertical window
:put a " put content of register
/pattern " search for 'pattern'
That said, a :scriptname
output that's too long to be scanned with your own eyes may be a symptom of deeper problems.
-
Agree with your last sentence. If the output is that long, the filtering should be done directly from the command line, not from within ex.– WildcardOct 21, 2015 at 10:10
-
Sorry, don't see what you mean about too long list. E.g. syntastic has in that list 10 files, nerdtree another 13 files, etc. Oct 21, 2015 at 12:52
-
2If the output of your
:scriptnames
is longer than Magna Carta you might consider installing Tim Pope's scriptease. It provides a function:Scriptnames
that puts all that junk in a quickfix list. There you can search it to your heart's content, save it to a file, or go to the corresponding scripts. Oct 21, 2015 at 13:39 -
@AlBerger, a list too long means too many plugins and/or plugins too large or poorly written. 13 files for a single plugin is way too much.– romainlOct 21, 2015 at 14:21
-
1Are you suggesting that
NERDTree
should put all its functions in a single file, in order to keep the output of:scriptnames
short? Would that make it a better written plugin? Oct 21, 2015 at 15:31