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.


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`
| improve this answer | |

The :filter command is a simple, one-line approach to this.

:filter /indent/ scriptnames

Would show only lines matching the pattern indent from the output of the :scriptnames command.

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.

| improve this answer | |
  • 2
    this is cleaner and more concise. – jdhao Apr 10 '19 at 10:14
  • 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

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.

| improve this answer | |
  • 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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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