简体   繁体   English

NavimBackward在Vim?

[英]NavigateBackward in Vim?

So Ctrl-o works only with jumps and has a history, '' gets you back to your last position regardless of how you ended up there (jumping, navigating, etc) but there's no history for it. 因此, Ctrl-o 仅适用于跳跃并且具有历史记录, '' 无论您如何在那里结束(跳跃,导航等),都会让您回到最后位置,但是没有历史记录。”

What I'm looking for is the best of those two worlds, something like Visual studio's NavigateBackward . 我正在寻找的是这两个世界中最好的,就像Visual studio的NavigateBackward Ctrl-o is good but a lot of the times it takes me back to positions I wouldn't expect, jumping is not the only way I navigate... Ctrl-o很好,但很多时候它让我回到我不会想到的位置,跳跃不是我导航的唯一方式......

  1. Is there a built-in command/way in vim that does this? 在vim中是否有内置的命令/方式来执行此操作?
  2. if not, is there a plugin for it? 如果没有,是否有插件?
  3. if not, I have no problem writing a plugin myself, I know how to set/get the caret position, but I looked at the autocmd-events and couldn't find anything that fires when the caret changes position. 如果没有,我自己编写插件没有问题,我知道如何设置/获取插入位置,但我查看了autocmd-events并且在插入符号改变位置时找不到任何触发。 How would I go about detecting the 'change' of the caret position? 如何检测插入位置的“变化”?

Thanks. 谢谢。

Alright so thanks to @nodakai for CursorMoved I'm getting somewhere. 好吧,谢谢@nodakai为CursorMoved我到了某个地方。 It's not perfect yet - there's many areas to improve - it works only on one buffer and there's no forward navigation. 它还不完美 - 还有很多方面需要改进 - 它只能在一个缓冲区上运行,而且没有前向导航。 Not bad for my first useful vimscript. 对我的第一个有用的vimscript来说还不错。 Will improve 会有所改善

" 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's 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

I'm not sure if it's a good idea to bluntly record all horizontal movement. 我不确定直截了当地记录所有水平移动是否是个好主意。 At least I don't see much use for being able to undo word-wise movements and such. 至少我没有看到太多用于能够撤消单词运动等。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM