How do I use the jumplist to jump once per file?

Solution 1:

My EnhancedJumps plugin has mapping variants of <C-o> / <C-i> to restrict jumps to the current or to different buffers.

<Leader><CTRL-O>, <Leader><CTRL-I> Go to [count] older / newer cursor position in another buffer. Jumps inside the current buffer are not considered. Useful for recalling previously visited buffers without going through all local positions. Regardless of the jump direction, the last jump position in a buffer is used when there are multiple subsequent jumps in a buffer.

Solution 2:

I had the exactly same issue and found that EnhancedJump plugin is quite large and requires an even larger depency. I wrote a small function that computes the number of <C-O>/<C-I> needed to jump to a different buffer than the current one. The jumplist stays clean, mapping it to <leader><C-O>/<C-I> fits my needs in a minimal way.

  • <leader><C-O> jumps to the last jump of the previous buffer in the jumplist.
  • <leader><C-I> does the same and moves to the next buffer in the jumplist.
function! JumpToNextBufferInJumplist(dir) " 1=forward, -1=backward
    let jl = getjumplist() | let jumplist = jl[0] | let curjump = jl[1]
    let jumpcmdstr = a:dir > 0 ? '<C-O>' : '<C-I>'
    let jumpcmdchr = a:dir > 0 ? '^O' : '^I'    " <C-I> or <C-O>
    let searchrange = a:dir > 0 ? range(curjump+1,len(jumplist))
                              \ : range(curjump-1,0,-1)
    for i in searchrange
        if jumplist[i]["bufnr"] != bufnr('%')
            let n = (i - curjump) * a:dir
            echo "Executing ".jumpcmdstr." ".n." times."
            execute "silent normal! ".n.jumpcmdchr
            break
        endif
    endfor
endfunction
nnoremap <leader><C-O> :call JumpToNextBufferInJumplist(-1)<CR>
nnoremap <leader><C-I> :call JumpToNextBufferInJumplist( 1)<CR>

Don't forget to replace ^O and ^I on line 4 by the real CTRL+o and CTRL+i characters with CTRL+v. I don't know why but the strings "<C-O>" and "<C-I>" didn't work when executing the normal! command.