The headers and options are highlighted with syntax highlighting, so your question could be considered as a special case of "how can I jump to the next syntax item?" If we solve that problem, we've also solved this one, since we use it to get to the syntax item for the man page header.
Here's a basic version of such a function:
fun! FindSyntax(name) abort
" Try to find this syntax item. Unfortunately this can't really be queried
" with a function, so this is a bit ugly.
let l:syn_list = execute(':syntax list ' . a:name)
let l:pattern = ''
for l:line in split(l:syn_list, '\n')
if l:line[:len(a:name)-1] == a:name
let l:pattern = l:line
break
endif
endfor
if l:pattern == ''
echoerr 'unable to find syntax pattern for ' . a:name
endif
" Find pattern. This is somewhat limited and only works for :syn match ..
let l:pattern = substitute(l:pattern, a:name . '\s\+xxx match /\(.\+\)/', '\1', 0)[:-3]
" Jump to next instance of this pattern
call search(l:pattern)
endfun
It only works stuff defined with :syntax match
for now (which is good enough for the purpose of this question), but this can be extended.
You can get the name of the text under the cursor with:
:echo synIDattr(synID(line('.'), col('.'), 1), 'name')
For man pages, this seems to be manSectionHeading
and manOptionDesc
. We can now use this in various ways:
:call FindSyntax('manSectionHeading')
:call FindSyntax('manOptionDesc')
" Next section
nnoremap <Leader>ns :call FindSyntax('manSectionHeading')<CR>
" Next option
nnoremap <Leader>no :call FindSyntax('manOptionDesc')<CR>
:command NextSection :call FindSyntax('manSectionHeading')
:command NextOption :call FindSyntax('manOptionDesc')
I asked a somewhat related question over at Unix.SE last year, by the way: how can I search for bolded or underlined text?, which also has some Vim-specific answers.