NavigateBackward in Vim?

So, it Ctrl-oworks only with paraphrases and has a story, it ''returns you to your last position no matter how you got there (jumps, transitions, etc.), but there is no story for it.

What I'm looking for is the best of these two worlds, something like Visual studio NavigateBackward . Ctrl-ogood, but many times it returns me to positions that I would not have expected, jumping is not the only way to navigate ...

  • Is there a built-in command / method in vim that does this?
  • If not, is there a plugin for this?
  • If not, I have no problem writing the plugin, I know how to set / get the carriage position, but I looked at it autocmd-eventsand could not find anything that works when the carriage changes position. How can I find a β€œchange” in carriage position?

Thank.

+4
source share
2 answers

I'm not sure it would be nice to record all the horizontal movements. At least I don’t see much point in that I can cancel text movements, etc. Your script is nice, but you might also like a more "specialized" solution.

" Make j and k leave jump marks when used with counts
function! JKMarks(motion)
    execute 'normal! ' . (v:count1 > 1 ? "m'" . v:count1 : '') . a:motion
endfunction
nnoremap <silent> j :<c-u>call JKMarks("j")<cr>
nnoremap <silent> k :<c-u>call JKMarks("k")<cr>

" Make mouse clicks leave jump marks
nnoremap <LeftMouse> m'<LeftMouse>
inoremap <LeftMouse> <c-o>m'<LeftMouse>
+1
source

, @nodakai CursorMoved - . - - . vimscript.

" Detect movement
augroup CursorChange
    autocmd!
    autocmd CursorMoved * call OnCursorMoved()
augroup END

" Does the navigation
nnoremap <silent><leader>nb :call NavigateBackward()<cr>

let CursorPositions  = []
let PrevJump = []

function! OnCursorMoved()
    "echo "Cursor moved to: " . line('.') . ':' . col('.')
    "echo g:CursorPositions
    let l:current = [line('.'), col('.')]
    if (l:current != g:PrevJump) " to prevent getting stuck in a circle
        let g:CursorPositions = add(g:CursorPositions, l:current)
    endif
endf

function! NavigateBackward()
    let l:toIndex = len(g:CursorPositions) - 2  " get target jump position index
    if (l:toIndex < 0) | return | endif         " make sure it within range
    let l:to = g:CursorPositions[l:toIndex]     " jump target
    let g:PrevJump = l:to                       " set previous to target
    call remove(g:CursorPositions, -1)          " remove last position
    call cursor(l:to[0], l:to[1])               " jump to target
endf

function! ResetCursorPositions() " for debugging
    let g:CursorPositions = [[line('.'), col('.')]]
endf
+1

All Articles