如何防止在 Vim 中遍历跳转列表时离开当前缓冲区?

发布于 2024-11-29 12:33:28 字数 583 浏览 1 评论 0原文

我经常在 Vim 会话中打开多个缓冲区。这意味着我的跳转列表存储来自多个缓冲区的位置。但是,经常当我使用 Ctrl+O 键盘快捷键跳转到上一个位置时,我不想离开缓冲区并想跳转到上一个位置“本地”到当前缓冲区。我该怎么做?

例如,假设我的跳转列表如下所示:

4   10   1 ~/aaa.m
3   20   1 ~/aaa.m
2   12   2 ~/xxx.m
1   15   1 ~/aaa.m

我想在第一次按 Ctrl+O< 时跳转到文件 aaa.m 的第 15 行/kbd>。重要的是,下次我按 Ctrl+O 时,我不想跳转到文件xxx.m。相反,我想跳转到文件aaa.m的第20行,即我在“当前”缓冲区中的先前位置。不过,默认的 Vim 行为是转到文件xxx.m 的第 12 行。

关于如何实现这一目标有什么想法吗?

I frequently have several buffers open in my Vim session. This means that my jump list stores locations from several buffers. However, frequently when I use the Ctrl+O keyboard shortcut to jump to a previous location, I do not want to leave the buffer and want to jump to previous locations “local” to the current buffer. How do I do this?

For example, assume my jump list looks as follows:

4   10   1 ~/aaa.m
3   20   1 ~/aaa.m
2   12   2 ~/xxx.m
1   15   1 ~/aaa.m

I want to jump to line 15 of file aaa.m the first time I press Ctrl+O. Importantly, the next time I press Ctrl+O, I do not want to jump to file xxx.m. Rather, I want to jump to line 20 of file aaa.m, that is, my previous location within the “current” buffer. The default Vim behaviour, though, is to take me to to line 12 of file xxx.m.

Any ideas on how I can achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-12-06 12:33:28

尝试下面的跳转列表遍历函数。它一步一步地
从一个跳转列表位置到另一个位置(使用 Ctrl+O
Ctrl+I 取决于值
提供给其 backforw 参数),如果当前
位置与其起始缓冲区位于同一缓冲区中。
如果无法找到属于
当前缓冲区,函数返回跳转列表中的位置
这是调用该函数之前的当前值。

function! JumpWithinFile(back, forw)
    let [n, i] = [bufnr('%'), 1]
    let p = [n] + getpos('.')[1:]
    sil! exe 'norm!1' . a:forw
    while 1
        let p1 = [bufnr('%')] + getpos('.')[1:]
        if n == p1[0] | break | endif
        if p == p1
            sil! exe 'norm!' . (i-1) . a:back
            break
        endif
        let [p, i] = [p1, i+1]
        sil! exe 'norm!1' . a:forw
    endwhile
endfunction

将此函数用作
Ctrl+O/Ctrl+I - 替换
锁定到当前缓冲区,创建映射,如下所示。

nnoremap <silent> <c-k> :call JumpWithinFile("\<c-i>", "\<c-o>")<cr>
nnoremap <silent> <c-j> :call JumpWithinFile("\<c-o>", "\<c-i>")<cr>

Try the following jump-list traversing function. It steps successively
from one jump-list location to another (using Ctrl+O
or Ctrl+I depending on the values that are
supplied to its back and forw arguments), and stops if the current
location is in the same buffer as that buffer it has started from.
If it is not possible to find a jump-list location that belongs to the
current buffer, the function returns to the position in the jump list
that was the current one before the function was called.

function! JumpWithinFile(back, forw)
    let [n, i] = [bufnr('%'), 1]
    let p = [n] + getpos('.')[1:]
    sil! exe 'norm!1' . a:forw
    while 1
        let p1 = [bufnr('%')] + getpos('.')[1:]
        if n == p1[0] | break | endif
        if p == p1
            sil! exe 'norm!' . (i-1) . a:back
            break
        endif
        let [p, i] = [p1, i+1]
        sil! exe 'norm!1' . a:forw
    endwhile
endfunction

To use this function as a
Ctrl+O/Ctrl+I-replacement
locked to the current buffer, create mappings as it is shown below.

nnoremap <silent> <c-k> :call JumpWithinFile("\<c-i>", "\<c-o>")<cr>
nnoremap <silent> <c-j> :call JumpWithinFile("\<c-o>", "\<c-i>")<cr>
小ぇ时光︴ 2024-12-06 12:33:28

也许 EnhancedJumps 插件会有所帮助。

安装此插件后,仅当随后立即再次重复相同的跳转命令时,才会跳转到另一个缓冲区。

Maybe the EnhancedJumps plugin will help.

With this plugin installed, the jump to another buffer is only done if the same jump command is repeated once more immediately afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文