9

When navigating in Vim, I often find myself jumping to a file, jumping around that file, then onto another file and so on. Finally, I want to return to the original file. Using <CTRL-O> does the job, but requires many more presses than I'd like.

I'm aware that I could set a mark beforehand, but I'd like a solution that doesn't require any up-front thinking.

I'm looking for a way to navigate back through the jumplist, stopping exactly once per file.

1
  • I needed this, for when a window's contents were inadvertently changed, and I couldn't remember which buffer I had there previously, only that I wanted it to be there. Jul 28, 2014 at 3:38

4 Answers 4

9

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.

2
  • Perfect, that's exactly what I'm looking for. Thanks.
    – Paul Carey
    Mar 31, 2013 at 14:46
  • This is exactly what I'm looking for as well. Thank you!
    – kohane15
    Jul 1, 2022 at 1:04
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.

0

I am not sure if I got you right. You want to jump between files/buffers, not the positions in jumplist. (which could be many positions in same file)

Then you could check buffer related commands. e.g.

:ls        "list all buffers, Number, name
:b[N]      "switch to buffer N
:b <tab>   "after tab, buffer name auto-completion 

and there are many plugins that help us to switch buffer easily.

0

The jumplist's purpose is not to help you jump from buffer to buffer: it records all the jump motions you do. Since typical vim usage includes lots of jumps within and across buffers, it would be a mistake to use it to navigate between buffers.

Unless you never use jump motions, that is. But that would be an even bigger mistake.

Vim has the buffer list which, as the name implies, is a list of all your buffers and comes with a bunch of navigation commands of its own. Unfortunately, this list is "static": you can navigate to the previous buffer in the list but not to the previously accessed buffer. Vim doesn't keep such a list.

I navigate buffers with names. But it could probably be doable to write a function that filers the jumplist and navigates to the first different buffer. I can't do that on this phone right now but I don't think it would be too hard.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .