0

I was trying to google how to search within a scope in VIM and found this link. Limiting search scope for code in Vim

I wouldn't imagine to come up with the keyword "/\%V" to search help in VIM help. So, I'm wondering how most people get help from VIM help, like in this case.

Community
  • 1
  • 1
Nakarin
  • 67
  • 1
  • 4
  • 4
    `:help help` In first attempt I try `:help ` following with any letter that I believe what I need find and then press `` for autocomplete. I can not explain it, it's a part of art of Vim maybe? – Alex Kroll Jun 13 '15 at 00:48
  • I’d never search for `/\%V` to start with. If I could remember that magic spell, I wouldn’t need to search for it! In such situations, I ask for `:help` on general topics such as `regex`, with the help of completion suggestions, then I skim the help page looking at the headings, or `/search` relevant keywords in it. – Maëlan Apr 28 '21 at 17:58

2 Answers2

1

You did the right thing. Google is probably going to be your best bet. You can also use the vim help site, it has a search function built in: http://vimdoc.sourceforge.net/htmldoc/

Aaron Jensen
  • 6,030
  • 1
  • 30
  • 40
1

General:

  1. :help help-summary is a great document explaining how to search effectively.

Using wildcard expansion

I've had the same frustration, but I just realised that you can use wildcard expansion when searching.

In your case it wouldn't have helped, but possibly if you'd started with :h pattern and then searched in that page for key words related to what you were trying to achieve you might have found it.

In my case I was looking for a page I'd been on before only the other night, :h ex-cmd-index, (on a different machine, so I couldn't search my command history) but I could only remember than it had to do with ex commands, and had the word index in there. I spent 20 mins trying to find it unsuccessfully (I eventually googled it just so I could then return to vim and look it up...). I was trying ex + Tab and being shown hundreds of possibilities...

But with wildcard expansion, I can just do :h ex*index* and hit Tab and I get a shortlist of two possibilities, ('ex-cmd-index' and 'ex-edit-index') one of which was the one I wanted!

So as long as you have an idea of the order that things should appear in the help topic, you should be able to narrow down the possibilities much faster.

Using :helpgrep

There is a in-built command :helpgrep which puts every line matching a pattern into a quickfix list. Then you can jump through each line with the :cnext and cprev commands.

Fuzzy search the help with fzf (and optionally ag-silver-searcher)

The fzf plugin has the command :Helptags to do a fuzzy search on... the help tags. This is very nice.

In case all the above fails, I added a few functions to search the entire contents (not just help-tags) of all the files in the vim's help doc directory using ag-silver-searcher and fzf.

First is a function that I found on an issue on the fzf github repo. I have no idea how it works, but the command that calls this function allows me to search the contents of a directory using ag, which pumps the results into fzf:

" Ag: Start ag in the specified directory e.g. :Ag ~/foo

function! s:ag_in(bang, ...)
  if !isdirectory(a:1)
    throw 'not a valid directory: ' .. a:1
  endif
  " Press `?' to enable preview window.
  call fzf#vim#ag(join(a:000[1:], ' '),
              \ fzf#vim#with_preview({'dir': a:1}, 'right:50%', '?'), a:bang)
endfunction

" Ag call a modified version of Ag where first arg is directory to search
command! -bang -nargs=+ -complete=dir Ag call s:ag_in(<bang>0, <f-args>)

So I wrote a function (Help_Ag) that calls the above command, and supplies the directory containing the vim help docs (141 .txt files, plus a handful of other files (a README, the tag files themselves etc).

If we select a help page to open, we set it to non-nomodifiable. I noticed that not all the tags worked, and some redirected to the wrong page (e.g. a tag like *help-topic* would take me to a page called help if the cursor was on that word, and take me to topic if the cursor was on that word, whereas the in real help, I would be taken to help-topic). I found that if I opened the real help, the tags would start behaving normally in files selected with ag+fzf. So I simply open and close the real help to trigger this behaviour (it's too quick to even see, so I'm happy enough with this hack):

function! Help_Ag()
    let orig_file = expand(@%)
    let v1 = v:version[0]
    let v2 = v:version[2]
    " search in the help docs with ag-silver-search and fzf and open file
    execute "normal! :Ag /usr/share/vim/vim".v1.v2."/doc/\<cr>"
    " if we opened a help doc
    if orig_file != expand(@%)
        set nomodifiable
        " for some reason not all the tags work unless I open the real help
        " so get whichever help was found and opened through Ag
        let help_doc=expand("%:t")
        " open and close that help doc - now the tags will work
        execute "normal! :tab :help " help_doc "\<cr>:q\<cr>"
    endif
endfunction

" get some help
command! H :call Help_Ag()
mattb
  • 2,787
  • 2
  • 6
  • 20