I want to go to the search result in the right-window, identified after performing a /urllib
search in the left window.
Pressing n\N
doesn't proceed to search results in other windows. How can I proceed to search results in other windows?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI want to go to the search result in the right-window, identified after performing a /urllib
search in the left window.
Pressing n\N
doesn't proceed to search results in other windows. How can I proceed to search results in other windows?
If you really want to do this...
function! CycleWindowsSearch(direction) abort
" set forward to set variables accordingly
let forward = a:direction
if ! v:searchforward
let forward = forward ? '0' : '1'
endif
" search forward / backward
let searchflags = forward ? 'W' : 'Wb'
" next or previous window
let winmove = forward ? 'w' : 'W'
" cursor at top or bottom
let curmove = forward ? '1' : '$'
" save first window ID
let firstwin=winnr()
" try to search
if ! search(@/, searchflags)
" move to next (or previous) window
execute('wincmd ' . winmove)
" save cursor position
let savepos = getcurpos()
" move to top (or bottom)
call cursor(curmove, curmove)
" redo for each window untill back to first
while ! search(@/, searchflags) && firstwin != winnr()
call setpos('.', savepos)
execute('wincmd ' . winmove)
call cursor(curmove, curmove)
endwhile
endif
endfunction
nnoremap <silent> n :call CycleWindowsSearch('1')<cr>
nnoremap <silent> N :call CycleWindowsSearch('0')<cr>
Update:
Caveat:
/
or ?
, I'll take it, thanks!
search()
function, not using the normal!
s. Remove the 'n'
flag so it does move the cursor. If it doesn't find it, move to the next window and repeat the same(?) search command. Instead of having two branches, simply store the flags in a variable and set it to 'b'
or ''
depending on which way you're going.
gg
or G
the window then). Thanks for your comments, as I mentioned I'll update my answer later when I have enough time to work on it.
As @D. Ben Knoble said in the comments, you have to change focus to the new window, for example <c-w>w
, if there are only two windows open. Then hit 'n'.
If this is an issue you frequently encounter, consider creating a mapping. In this case, to make <c-s>
the way to search in the new window you could add to your .vimrc the following.
nnoremap <c-s> <c-w>wn
<c-s>
used for here? I know <c-w>w
changes focus to next window.