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
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.
- 1,081
- 7
- 22
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`
- 1,081
- 7
- 22
- 1,192
- 1
- 10
- 16
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.
- 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
-
2If the output of your
:scriptnamesis longer than Magna Carta you might consider installing Tim Pope's scriptease. It provides a function:Scriptnamesthat 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
-
1Are you suggesting that
NERDTreeshould put all its functions in a single file, in order to keep the output of:scriptnamesshort? Would that make it a better written plugin? – Sato Katsura Oct 21 '15 at 15:31
filterdoesn't observe'ignorecase'like other pattern-using commands. You can however force it by prepending\cto your pattern, like so::filter /\cindent/ scriptnames. See also:h \c. – ZeroKnight Sep 22 '20 at 19:56\cis need to search case-insensitively. – jdhao Sep 23 '20 at 02:05