michal-h21 / vim-zettel

VimWiki addon for managing notes according to Zettelkasten method
MIT License
557 stars 73 forks source link

Using ZettelSearch to only search note titles #95

Closed joshuaberetta closed 3 years ago

joshuaberetta commented 3 years ago

Is it possible to modify the ZettelSearch command to only search note titles and not the contents of the notes too? Something along the lines of what Obsidian does when pressing [[.

rober-m commented 3 years ago

I made my own remaps to accomplish just that. They worked just fine until I updated the vim-zettel version. Care to try them out to check if you can use them? They are at #98. Please, let me know if they work for you!

joshuaberetta commented 3 years ago

Hi @IIPatternII, thanks for linking your solution — unfortunately it's not working for me either 😢 This is the main thing that's forcing me to stick with Obsidian for now.

rober-m commented 3 years ago

@joshuaberetta ok! Thanks! At least you managed to reproduce the error, which is a step in the right direction!

Sadly, I don't know VimScript, so I can't do much to solve the problem. Hopefully, someone will save us! 😅

joshuaberetta commented 3 years ago

@IIPatternII, I'm also useless with VimScript. I tried making some small changes but just broke everything :joy:

rattletat commented 3 years ago

@joshuaberetta Here is my solution using fzf:

let g:zettel_fzf_command = "rg --column --line-number --color=always --smart-case --regexp '^title: (.+)' --replace '$1'"
let g:zettel_fzf_options = ['--with-nth=-1']

Not sure however how you could hide the preview window.

joshuaberetta commented 3 years ago

@rattletat thanks! I tried your suggestion but it only seems to work for files in the current directory 😢

rattletat commented 3 years ago

@joshuaberetta Since I have my files all in one folder, this is enough for me atleast. Do you want to search files that are in different directories? You can probably provide them to ripgrep in some way.

joshuaberetta commented 3 years ago

@rattletat managed to get it working for me by adding ./* to the end of your rg command 🥲

rober-m commented 3 years ago

@joshuaberetta your solution works great! I also need to be able to search the entire file. Do you know a way choose between searching only the title or the entire file? See #98 for more info.

@rattletat you should close the issue.

rattletat commented 3 years ago

@joshuaberetta you should close it^^

@IIPatternII You could just get the 'junegunn/fzf.vim' and bind the :Rg command yourself. Using fzf (and ripgrep) you can configure everything as you want, since they are very powerful tools. It is really worthwhile to skim the manpages of ripgrep.

For example, a vim function which would do the same thing as the issue above just using the fzf.vim plugin:

    function! ZettelSearchTitle ()
      let command = "rg --column --line-number --color=always --smart-case --regexp '^title: (.+)' --replace '$1' -- . " . expand("%:h")
      let spec = {'options': ['--delimiter=:', '--with-nth=-1']}
      call fzf#vim#grep(command, 1, fzf#vim#with_preview(spec), 0)
    endfunction

If you want to search all lines, you only need to remove the regex and replace part:

    function! ZettelSearchTitle ()
      let command = "rg --column --line-number --color=always --smart-case -- . " . expand("%:h")
      let spec = {'options': ['--delimiter=:', '--with-nth=-1']}
      call fzf#vim#grep(command, 1, fzf#vim#with_preview(spec), 0)
    endfunction

I will leave the mappings of the functions as an exercise for the reader :smile: