0

I am trying to work with a large JSON file and I would like to print the lines with an occurrence of a any one of two particular strings.

For this I'm using ilist, for one string I can do as follows to print all lines where the term Token appears for example:

:ilist /Token

This is great but it only prints the line with the token and the token value, as expected.

Since this is a large file I'd like more context. There is another field not far from this Token called name. I'd like to print all lines with either Token or name. So I tried:

:ilist /Token|name

But it doesn't work and I get a E389: Couldn't find pattern error.

I learnt that regular expressions are a little different in vim and so I found this and tried to understand how to achieve the above, but I failed and I am now here. Can anyone help me understand how to use pattern in this context?

1 Answer 1

1

While :help :ilist can be used to print lines matching a given pattern, it is not really designed for that.

Combining :help :global and one of :help :list, :help :print, :help :number, :help :#, and :help :z, on the other hand, is the canonical way to do what you are trying to do. Note that :g/pattern, without a command, defaults to :print.

All you need is the correct pattern.

If you want to print lines with either token or name:

:g/\<token\>\|\<name\>

See :help \<, :help \>, and :help \|. You could probably get by without the word boundaries but it is cleaner that way.

If you want to print lines with both token and name, in that order:

:g/\<token\>.*\<name\>

If you want to print lines with both token and name, in any order:

:g/.*\<name\>\&.*\<token\>.*

If you insist on using :ilist:

:ilist token\|name
2
  • 1
    If you want to print lines with both token and name, in any order: :g/.*name\&.*token.*. See :h /\& Commented Apr 5 at 17:42
  • Answer amended.
    – romainl
    Commented Apr 5 at 18:21

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.