15

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 3

7

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`
19

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.

5
  • 3
    this is cleaner and more concise.
    – jdhao
    Commented Apr 10, 2019 at 10:14
  • 1
    Indeed, this should be the accepted answer. (Learned a lot from edi9999's answer though, hence the upvote.)
    – toraritte
    Commented Jun 27, 2019 at 22:00
  • How do I filter in a case-insensitive manner?
    – jdhao
    Commented Sep 22, 2020 at 11:41
  • @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.
    – ZeroKnight
    Commented Sep 22, 2020 at 19:56
  • @ZeroKnight Thanks for your prompt reply. I have also figured out \c is need to search case-insensitively.
    – jdhao
    Commented Sep 23, 2020 at 2:05
5

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.

5
  • 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.
    – Wildcard
    Commented Oct 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.
    – Al Berger
    Commented Oct 21, 2015 at 12:52
  • 2
    If 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. Commented 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.
    – romainl
    Commented Oct 21, 2015 at 14:21
  • 1
    Are 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? Commented Oct 21, 2015 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.