在Vim中遍历跳转列表时如何防止离开当前缓冲区? [英] How to prevent leaving the current buffer when traversing the jump list in Vim?

查看:139
本文介绍了在Vim中遍历跳转列表时如何防止离开当前缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想在我第一次按 Ctrl + O 时跳到文件 aaa.m 的第15行.重要的是,下次我按下 Ctrl + O 时,我不想跳转到文件 xxx.m .相反,我想跳到文件 aaa.m 的第20行,即我在当前"缓冲区中的上一个位置.不过,Vim的默认行为是将我带到文件 xxx.m 的第12行.

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?

推荐答案

尝试以下跳转列表遍历功能.它一步一步地走从一个跳转列表位置转到另一个位置(使用 Ctrl + O Ctrl + I ,具体取决于提供给它的 back forw 参数),如果当前该位置与其开始所在的缓冲区在同一缓冲区中.如果找不到属于当前缓冲区,函数返回到跳转列表中的位置那是调用该函数之前的当前代码.

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

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

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>

这篇关于在Vim中遍历跳转列表时如何防止离开当前缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆