Created
November 8, 2021 18:22
-
-
Save EgZvor/e804cdd8259455a41eaa9f6ed0cdde53 to your computer and use it in GitHub Desktop.
Vim mappings to jump through jumplist by files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if exists('g:loaded_jumpfile') | |
finish | |
endif | |
let g:loaded_jumpfile = 1 | |
function! JumpFileComputePrevious() | |
let [jump_list, pos] = getjumplist() | |
let previous_list = jump_list | |
\ ->map({idx, val -> [idx, val]})[:pos] | |
\ ->reverse() | |
\ ->filter({idx, pos_b -> pos_b[1].bufnr != bufnr()}) | |
if previous_list != [] | |
return pos - previous_list[0][0] | |
else | |
return 0 | |
endif | |
endfunction | |
function! JumpFileComputeNext() | |
let [jump_list, pos] = getjumplist() | |
let next_list = jump_list | |
\ ->map({idx, val -> [idx, val]})[pos:] | |
\ ->filter({idx, pos_b -> pos_b[1].bufnr != bufnr()}) | |
if next_list != [] | |
return next_list[0][0] - pos | |
else | |
return 0 | |
endif | |
endfunction | |
nnoremap <plug>(JumpPrevFile) <cmd>execute 'normal ' . JumpFileComputePrevious() . "\<c-o>"<cr> | |
nnoremap <plug>(JumpNextFile) <cmd>execute 'normal ' . JumpFileComputeNext() . "\<c-i>"<cr> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little explainer, since the code is pretty dense.
We're calling a
getjumplist
function which returns both the list of entries and our current position in that list.Say, this is a jump list (each letter signifies a different file).
The provided functions return the distance between the desired and current position in the jump list, which is the closest position (in each direction) that is contained in a different file.
Let's trace through
JumpFileComputePrevious
.First we use
map
to create a new list to preserve indexes:Cut off the "newer" entries, since we only care about "older" (previous) ones.
Reverse the list
Filter out all the entries matching the current file
Compute the difference between the current position and the desired one (the first of the resulting list)
The count to jump is
2
.If it happens that all entries belong to the current buffer, return
0
, which will mean "don't move".