vim location-list:如果在最后位置,如何去第一个位置

wan*_*gzq 5 vim

我已映射gn:lnext<cr>; 如何按住它来循环通过位置列表,即如果在最后位置则转到第一个?

谢谢

rom*_*inl 8

以下是我的配置直接解决方案:

" wrap :cnext/:cprevious and :lnext/:lprevious
function! WrapCommand(direction, prefix)
    if a:direction == "up"
        try
            execute a:prefix . "previous"
        catch /^Vim\%((\a\+)\)\=:E553/
            execute a:prefix . "last"
        catch /^Vim\%((\a\+)\)\=:E\%(776\|42\):/
        endtry
    elseif a:direction == "down"
        try
            execute a:prefix . "next"
        catch /^Vim\%((\a\+)\)\=:E553/
            execute a:prefix . "first"
        catch /^Vim\%((\a\+)\)\=:E\%(776\|42\):/
        endtry
    endif
endfunction

" <Home> and <End> go up and down the quickfix list and wrap around
nnoremap <silent> <Home> :call WrapCommand('up', 'c')<CR>
nnoremap <silent> <End>  :call WrapCommand('down', 'c')<CR>

" <C-Home> and <C-End> go up and down the location list and wrap around
nnoremap <silent> <C-Home> :call WrapCommand('up', 'l')<CR>
nnoremap <silent> <C-End>  :call WrapCommand('down', 'l')<CR>
Run Code Online (Sandbox Code Playgroud)


Pet*_*ker 7

秘密是使用:try:catch在其他语言中一样.您正在寻找以下错误E553E42.

nnoremap ]l :try<bar>lnext<bar>catch /^Vim\%((\a\+)\)\=:E\%(553\<bar>42\):/<bar>lfirst<bar>endtry<cr>
Run Code Online (Sandbox Code Playgroud)

该命令的灵感来自Tim Pope的unimpaired.vim插件.

我也建议不要映射gn.该gn命令是非常方便的动作,对搜索模式采取行动.见:h gn.

有关更多信息,请参阅

:h :try
:h E553
:h E42
:h :lnext
:h :lfirst
Run Code Online (Sandbox Code Playgroud)