【问题标题】:NavigateBackward in Vim?在 Vim 中向后导航?
【发布时间】:2014-08-23 09:33:37
【问题描述】:

所以Ctrl-o与跳跃一起工作并且有历史,'' 让你回到你的最后位置不管你是如何结束那里的(跳跃,导航等),但没有历史记录。

我正在寻找的是这两个世界中最好的,比如Visual studio's NavigateBackwardCtrl-o 很好,但很多时候它会让我回到我没想到的位置,跳跃并不是我导航的唯一方式......

  1. vim 中是否有内置命令/方式可以执行此操作?
  2. 如果没有,有插件吗?
  3. 如果没有,我自己编写插件没有问题,我知道如何设置/获取插入符号的位置,但我查看了autocmd-events,找不到任何触发 插入符号改变位置。我将如何检测插入符号位置的“变化”?

谢谢。

【问题讨论】:

  • 不完全确定你想要什么。你能添加一个例子/用例吗?您链接的问题是关于跳转到最后编辑的位置,即[count]g;(另见:changes)。
  • 感谢g; 不知道。我不确定有什么不清楚的地方,VS 的向后导航会带你回到最后一个光标位置,不管它是最后编辑的位置、最后导航的位置、最后跳转的位置等。我想回到任何地方,不管我是怎么到那里的。如果我执行5j10k 等,当前vim 中的所有命令都不会让我回到原来的位置(有历史记录),我要么必须跳转并使用Ctrl-o,编辑并使用g; 或@ 987654333@ 并接受它没有历史记录。所以基本上我要的是一个带有历史的'' 版本。
  • 我认为 Vim 不支持它,因为每个 hjkl 都必须向历史堆栈推送一个位置。
  • :he CursorMoved 可能有帮助吗?
  • 哦,我错过了,谢谢!我会看看我能用它做什么。

标签: vim


【解决方案1】:

我不确定直接记录所有水平运动是否是个好主意。至少我认为能够撤消逐字移动等没有多大用处。您的脚本很好,但您可能还喜欢更“专业”的解决方案..

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

【讨论】:

  • 完全同意你的看法。我只是想让一些东西“工作”——还有很多需要改进的地方,包括你提到的。
  • 哦,我刚刚阅读了关于m' 的内容,它似乎做了我在编写脚本之前最初想到的事情,并且附加到了跳转列表中。这实际上要容易得多。现在我想知道如何让它通过缓冲区...
  • 好吧,我找到了这个答案stackoverflow.com/questions/15768860/… 用于在窗口中导航,而这个stackoverflow.com/questions/3706958/… 用于缓冲区。我想我现在很好。
【解决方案2】:

好的,感谢@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'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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-29
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2012-02-03
    相关资源
    最近更新 更多