3

I often need to find several keywords in a sentence.

For example: This is just a sentence of word1 word3 word4 word2 text in my huge Vim data file.

I can't remember the exact pattern of search, hence I need a search that can do that:

Is there way or plugin that enable search of multiple keywords that are not in correct order in a sentence?

For example: I am searching for these keywords (/word1,word2, word3, word4) in a sentence, but in actual fact the order was not like what I thought it was.

andrew_ysk
  • 389
  • 2
  • 12
  • You could take a look at fzf and it's associated plugin - the :Lines or :BLines command would do what you're asking. – mattb Aug 11 '22 at 06:59
  • I donno if fzf is order-insensitive. I'd simply do :grep ''\bword1\b|\word2\b' (with ripgrep as :h grepprg). This will return lines where either or both of these words are present. I'd then do a v:word1:d and v:word2:d in the quickfixlist, which will get rid of the lines where both the words aren't present. And if this were to be a regular operation, I'd write a vimscript function to automate the steps. Lemme know if it helps or if you want me to write the function. I don't wanna work in vain otherwise. – 3N4N Aug 11 '22 at 07:07
  • 1
    I can confirm that fzf is order-insensitive. – mattb Aug 11 '22 at 07:23
  • @mattb How to install fzf in vim ? I have fzf installed in manjaro ( /usr/bin/fzf /usr/share/fzf ), but i can't seems to figure out how to install fzf for vim. the Instruction seems to be Very complicated. After reading , make me dizzy https://github.com/junegunn/fzf/blob/master/README-VIM.md – andrew_ysk Sep 24 '22 at 10:13
  • @andrew_ysk that should be posted as a separate question so that people can google and find it in the future (I'd be happy to post an answer!) – mattb Sep 24 '22 at 12:00
  • I will be gladly to do so. – andrew_ysk Sep 24 '22 at 12:03

1 Answers1

11

A little known plugin that comes with Vim distributed is the LogiPat plugin.

This plugin is distributed with Vim by default and should also be active by default.

In your case I would try to use the following command:

LogiPat "word1" & "word2" & "word3" & "word4"

which will create the following search pattern: \%(.*word1.*\&\%(.*word2.*\&\%(.*word3.*\&.*word4.*\)\)\) and directly search for it. Use :set hls to see the effect directly by highlighting all matching positions in your window.

This uses the so called branch atom (which you can read about in more detail at :h /branch), which basically matches for any order of any of the provided arguments in the same line.

It's a bit of a nasty pattern, but the LogiPat plugin makes it a bit easier to use.

Please check the help (:h logiPat) for how to use it.

Christian Brabandt
  • 25,820
  • 1
  • 52
  • 77