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.

Al Berger
  • 393
  • 1
  • 11

3 Answers3

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.

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

romainl
  • 40,486
  • 5
  • 85
  • 117
  • 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 Oct 21 '15 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 Oct 21 '15 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. – Sato Katsura Oct 21 '15 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 Oct 21 '15 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? – Sato Katsura Oct 21 '15 at 15:31