How to prevent leaving the current buffer when traversing the jump list in Vim?

后端 未结 2 720
遇见更好的自我
遇见更好的自我 2021-02-14 22:55

I frequently have several buffers open in my Vim session. This means that my jump list stores locations from several buffers. However, frequently when I use the Ctrl+

2条回答
  •  盖世英雄少女心
    2021-02-14 23:08

    Try the following jump-list traversing function. It steps successively from one jump-list location to another (using Ctrl+O or Ctrl+I depending on the values that are supplied to its back and forw arguments), and stops if the current location is in the same buffer as that buffer it has started from. If it is not possible to find a jump-list location that belongs to the current buffer, the function returns to the position in the jump list that was the current one before the function was called.

    function! JumpWithinFile(back, forw)
        let [n, i] = [bufnr('%'), 1]
        let p = [n] + getpos('.')[1:]
        sil! exe 'norm!1' . a:forw
        while 1
            let p1 = [bufnr('%')] + getpos('.')[1:]
            if n == p1[0] | break | endif
            if p == p1
                sil! exe 'norm!' . (i-1) . a:back
                break
            endif
            let [p, i] = [p1, i+1]
            sil! exe 'norm!1' . a:forw
        endwhile
    endfunction
    

    To use this function as a Ctrl+O/Ctrl+I-replacement locked to the current buffer, create mappings as it is shown below.

    nnoremap   :call JumpWithinFile("\", "\")
    nnoremap   :call JumpWithinFile("\", "\")
    

提交回复
热议问题