0

Is there a (built-in) way to search all vimwiki files for a certain pattern? I don't mean searching tags, but something akin to using grep on all the wiki files in the vimwiki directory.

If there is not a native vimwiki way, is there a way to do it without exiting the vimwiki (i.e a vim command of some sort)?

  • I don't know about vimwiki plugin or what you mean by vimwiki directory, i.e. whether it is the location the plugin is installed or where you store you own wiki files. But you could look into :h :vimgrep. It's fairly simple, and as you suggested, mostly alike grep – klaus Mar 29 at 5:00
  • @klaus You should add that as an answer! – Rich Mar 29 at 9:02
  • @Rich, I just talked about :h :vimgrep and :h grep in the answer because I actually didn't clearly understand what OP actually wanted. If s/he is actually using the plugin, he should be able to skim through the readme and help docs to be able to figure out what functionalities the plugin provides. If there is confusion, s/he should report it in the issue tracker of the plugin, not in a help forum. That's why I guessed the most basic way his requirement could have been interpreted. – klaus Mar 29 at 10:34
  • 1
    @klaus I think that's a great answer! As I read it, the OP isn't too bothered about whether they use a plugin feature or a native Vim feature: they just want to get the job done. Upvoted! – Rich Mar 29 at 11:04
3

Vimwiki has a simple search included. You can run :VimwikiSearch /pattern/ (the trailing slash is important). To go to the next hit run :lnext and :lprevious for the previous.

For more details you can look into the help pages :h :VimwikiSearch.

  • :lfirst and :llast might be needed cause these jumps are not cyclic. – klaus Mar 29 at 10:33
2

vimgrep

There is a command called vimgrep (see :h :vimgrep) that is a native grepping utility built into vim.

To search for a pattern in every wiki files in a directory, assuming the wiki files have .wiki extension, you would do:

:vimgrep /{pattern}/[g][j] path/to/directory/*.wiki

Check :h :vimgrep for info on the flags

With the above command vim will search the specified files with .wiki extension in the specified directory for the specified {pattern} and set the error list to the matches, i.e. load the quickfix window (see :h quickfix). You can move through the matches in the following ways:

  • :copen : To open the quickfix window
  • :cclose : To close the quickfix window
  • :cnext : To jump to the next match
  • :cprev : To jump to the previous match
  • :cfirst : To jump to the first match
  • :clast : To jump to the last match

You can find info about the above keybindings and many more functionalities of quickfix in :h quickfix

There is a variant of quickfix window called :h location-list. It's a window local quickfix list, where a quickfix list is independent of a window/split. That is you can have multiple location-list but not more than one quickfix windows. So, you can create your workflow based on your needs.

In order to open quickfix and location-list windows right after a quickfix/location-list command has been issued, you can put the following in your vimrc:

augroup quickfix
    autocmd!
    autocmd QuickFixCmdPost [^l]* nested cwindow
    autocmd QuickFixCmdPost    l* nested lwindow
augroup END

grep

You can also use the grepping utility provided by your system to fill up the quickfix list. The command is called, well, :grep (see :h grep). By default, the grep program to be used is set to use GNU grep and in a weird way that I couldn't figure out how to use it. But if you have some other grepping utility, which you should, like silversearcher-ag or ripgrep-rg, then you can set up the grep command in vim in the following way:

if executable('ag')
    set grepprg=ag\ --nogroup\ --nocolor\ --hidden\ --ignore\ .git
endif

You can set up vim :h grepprg for ripgrep-rg in similar way. And it will also fill up your quickfix list the same way vimgrep does, and then you can jump among the matches with the same keybindings.

The equivalent of the vimgrep command mentioned above for :h grep would be:

:grep '{pattern}' path/to/directory/*.wiki

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.