I am trying to write a couple of simple functions to find the next and previous links in Vimwiki and move to them. Links in Vimwiki are indicated with the syntax [[link to something]]. Since I only want to move the cursor and I want to avoid highlighting all matches, I am using the functions searchpos() and setpos() instead of the usual / and ?. searchpos() has the same options as search(), so I imagine they work equivalently.
I have written the following:
function! MoveToNextLink()
" Get line number and column
let [lnum, col] = searchpos('\[\[.\{-}\]\]', 'nz')
" Move cursor
call setpos('.', [0, lnum, col, 0])
endfunction
function! MoveToPrevLink()
" Get line number and column
let [lnum, col] = searchpos('\[\[.\{-}\]\]', 'bnz')
" Move cursor
call setpos('.', [0, lnum, col, 0])
endfunction
nnoremap <silent> <C-l> :call MoveToNextLink()<CR>
nnoremap <silent> <C-h> :call MoveToPrevLink()<CR>
The options for search() are: n, that indicates to not move the cursor; b, to search backwards; and z, for starting the search at the cursor position rather than at the beginning or the end of the line.
For the most part these mappings seem to work. However, I run into problems when there are multiple matches [[link to something]] in the current line:
- The forward function
MoveToNextLinkworks in its current form. Originally, I did not have thezoption and it didn't work because all searches started at the beginning of the line, so only the first match was found, regardless of the position of the cursor in the line. Adding thezoption fixed this, as expected. - The backward function
MoveToPrevLinkdoes not work. Similarly to the behaviour ofMoveToNextLinkwithout thezoption, it only finds the last match in the current line, suggesting thatsearch()starts looking at the end of the line and keeps searching backwards from there. Adding thezoption worked as intended in the forward direction, but did not change anything in the backward direction, which was surprising.
Why is this? Am I doing something wrong, or is there a bug/inconsistency in Vim such that the z option does not work for backward search?
Woption to avoid wrapping around file words in backward direction insearchpos(), but not in forward direction; and second,search()doesn't work in a normal mapping, but it has to be invoked within a function (or at least I haven't managed to make it work in a mapping). – mgarort Feb 18 '21 at 12:26Wseems weird indeed, but it has the same behavior in both vim and neovi maybe it's a matter a documentation I don't know. However aboutsearch()in a mapping I don't know what you were trying to do butnnoremap <F1> :call search('.')<CR>works perfectly for example. – statox Feb 18 '21 at 12:45nnoremap <F1> :call search('.')<CR>, at the end of myvimrc, pressing<F1>works as expected. But if I put the following mapping immediately after,nnoremap <C-l> :call search('.')<CR>, pressing<C-l>gives me the errorE119: Not enough arguments for function: search. This must be because of my configuration, since a clean Vim without any custom configuration maps<C-l>as expected. I fail to understand how an error like this one can arise. Anyway, thanks a lot for your help! – mgarort Feb 18 '21 at 14:47