9

I regularly find myself in the situation that I am reading a help topic and see an identifier (the blue underlined words) that I want to follow with <C-]>. However, most of the time the identifier is not (yet) under my cursor.

Instead of searching for the identifier with /part_of_identifier<CR> I would like to use a smarter variant, a unimpaired-like mapping like ]i to go to the next available identifier after my cursor (and [i to go to the previous identifier).

How could I create such a mapping? Or is there a builtin command already for that? If not, do you see an alternative/smarter way how I could navigate identifiers inside Vim help files? Or can I tag all Vim's and the plugins' help files with ctags and use ]t somehow?

3 Answers 3

8

In the help files the identifiers are actually marked as |identifier| (but you don't see the | because of the syntax file, use set ft=txt in an help buffer to check what I'm talking about).

You could then create a mapping which would look for the next pattern \|\W*\| and you should be good.

Something like:

execute "nnoremap ]i :call search('\\|\\w*\\|')<CR>"

(The execute and multi \\| are here to deal with the escaping problems)

Edit @EvergreenTree came up with a much more elegant solution:

nnoremap ]i :call search('\v\\|.{-}\\|')<CR>

It is also possible to use the same method to jump to the help topics (the string used in the commands :h foo) which are delimited by * characters:

nnoremap ]i :call search('*\w*\*')<CR>
9
  • Thank you for your answer. However it does not do what I want, cause I used the wrong term in my problem description (sorry for that). I want the mapping to jump to the next Link to an identifier, so I can then press <C-]>. So it would be nice if you could update the solution to use | instead of * :-)
    – cbaumhardt
    May 25, 2016 at 12:19
  • Does my update is what you were looking for? :-)
    – statox
    May 25, 2016 at 12:26
  • At the moment not yet: If I open help files, e.g. :h help and press ]i, then the cursor doesn't move (I expect it to jump to the blue underlined word help-translated).
    – cbaumhardt
    May 25, 2016 at 12:32
  • 3
    \W is the wrong atom to use in this case, since it matches all non-word characters. Use . instead. Also, using a greedy multi like * means no more than one match will be found for each line. Use {-} instead. Put together: '\v\|.{-}\|'. The reason for \v is that patterns can be interpreted differently depending on the magic option, and \v explicitly sets it to "very magic". May 25, 2016 at 12:50
  • 1
    Also note that you can create a backwards searching version by passing 'b' as an argument to search(). May 25, 2016 at 14:13
5

This is what I use.

As well as |.....| tags, it will also stop on options of the form '.....'

But it's not pretty...

### ~/.vim/ftplugin/help/navigate.vim ###
nnoremap <silent> <buffer> ]i /\('\zs\k\+'\\|[<Bar>]\zs\k\+[<Bar>]\)<CR>:set nohlsearch<CR>
nnoremap <silent> <buffer> [i ?\('\zs\k\+'\\|[<Bar>]\zs\k\+[<Bar>]\)<CR>:set nohlsearch<CR>

We use <zs> to land the cursor on the start of the word, rather than on the | or ' character.

2
  • I have changed the keybind LHS to suit you. May 25, 2016 at 16:49
  • Your solution works great, too :) I must have messed something up when pasting your code in my .vimrc and having changed the <Tab> to ]i, because I tried your code then and it didn't work for me. It does work now and you didn't change anything other than the mapped keys, so I messed that up before. You deserve credit here too :)
    – cbaumhardt
    May 25, 2016 at 18:05
1

Similar to joeytwiddle's answer:

### ~/.vim/ftplugin/help/navigate.vim ###
nnoremap <silent> <buffer> [i ?\('\zs[a-z]\{2,\}'\\|'\zst_..'\\|[<Bar>]\zs\k\+[<Bar>]\)<CR>:noh<CR>
nnoremap <silent> <buffer> ]i /\('\zs[a-z]\{2,\}'\\|'\zst_..'\\|[<Bar>]\zs\k\+[<Bar>]\)<CR>:noh<CR>

Two changes:

  1. The branch finding option tags (those of the form '...') changed to vim's help option syntax exactly. (That link is fixed to latest commit on master, at the time of this writing, but since it was last changed 17 years ago, it's probably safe for a while.)
  2. :set nohlsearch changed to :noh, in case you usually have hlsearch on (I do).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.