如何配置 matchit.vim 以使用而不是%?

发布于 2025-01-03 14:34:23 字数 658 浏览 1 评论 0原文

我是 matchit.vim 插件的忠实粉丝,但我更喜欢使用 键在匹配的分隔符之间跳转。然而,似乎 matchit 是硬编码的,以便在按 % 键时激活。

我的第一个想法是,我只需将此行放入我的 .vimrc 中,并将 '%' 更改为 '',从而将 Match_wrapper 调用绑定到Tab 键:

nnoremap <silent> %  :<C-U>call <SID>Match_wrapper('',1,'n') <CR>

但是,这似乎不起作用;我猜这与 (据我所知是脚本唯一的 ID?)或 Match_wrapper 是脚本本地的事实有关。 (我对 Vimscript 还很陌生)

到目前为止,我已经成功地通过使用“nmap”将 映射到 % 来实现,但这是一个非常脆弱的黑客攻击。

无论如何,任何帮助将不胜感激! :)

I'm a big fan of the matchit.vim plugin, but I prefer to jump between matching delimiters with the <tab> key. However, it seems that matchit is hard-coded to activate when pressing the % key.

My first thought would be that I would simply put this line in my .vimrc, and change '%' to '<tab>', thus binding the Match_wrapper call to the tab key:

nnoremap <silent> %  :<C-U>call <SID>Match_wrapper('',1,'n') <CR>

However, this doesn't seem to work; I'm guessing it has got something to do with the <SID> (which as far as I understand is an ID unique to the script?) or the fact that Match_wrapper is script-local. (I'm pretty new to Vimscript)

Thus far I've managed to get by mapping <tab> to % with 'nmap', but it's a pretty fragile hack.

Anyway, any help would be greatly appreciated! :)

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

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

发布评论

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

评论(3

孤城病女 2025-01-10 14:34:23

好吧,如果您知道 %总是被重新映射,那么使用

map <Tab> %

是安全的(前面缺少 n 是故意的:%:map 涵盖的所有模式中定义。但您始终可以做的是将 替换为 {N}_,其中 {N} 是数字:scriptnames 输出中的 matchit 脚本的名称。在较新的 vim 中,您还可以使用 maparg('%', 'n', 0, 1),它将输出一个字典,其中包含 lhs 和 <代码>sid。在这种情况下,代码可能如下所示:

for s:mode in ['n', 'v', 'o']
    let s:map=maparg('%', s:mode, 0, 1)
    execute s:mode.'noremap <Tab> '.substitute(s:map.lhs, '<SID>', '<SNR>'.s:map.sid.'_', 'g')
endfor

在这种情况下

for s:mode in ['n', 'v', 'o']
    execute s:mode.'noremap <Tab> '.maparg('%', s:mode)
endfor

,也可以接受,因为 maparg 的“旧”(没有第四个参数)行为是扩展

Well, if you know that % will always be remapped, then using

map <Tab> %

is safe (absence of n in front is intentional: % is defined in all modes covered by :map). But what you can always do is to replace <SID> with <SNR>{N}_ where {N} is the number of the matchit script in the outputs of :scriptnames. In a newer vim you can also use maparg('%', 'n', 0, 1), it will output a dictionary that among other values contains lhs and sid. In this case code may look like this:

for s:mode in ['n', 'v', 'o']
    let s:map=maparg('%', s:mode, 0, 1)
    execute s:mode.'noremap <Tab> '.substitute(s:map.lhs, '<SID>', '<SNR>'.s:map.sid.'_', 'g')
endfor

In this case

for s:mode in ['n', 'v', 'o']
    execute s:mode.'noremap <Tab> '.maparg('%', s:mode)
endfor

is also acceptable as “old” (without fourth argument) behavior of maparg is to expand <SID>.

尹雨沫 2025-01-10 14:34:23

这就是我所做的:

" <C-I> and <TAB> are the same thing.
" So, I changed <C-I> to <C-O><C-I> and <C-O> to <C-O><C-O> to match.
" I didn't want to lose the <C-I> jump functionality.
noremap <C-O><C-O> <C-O>
noremap <C-O><C-I> <C-I>

" This is what the plugin sets on %. I just set it on <TAB>
onoremap <TAB> :<C-U>call <SNR>41_Match_wrapper('',1,'o')<CR>
nnoremap <TAB> :<C-U>call <SNR>41_Match_wrapper('',1,'n')<CR>
vnoremap <TAB> :<C-U>call <SNR>41_Match_wrapper('',1,'v')<CR>m'gv``

Here's what I did:

" <C-I> and <TAB> are the same thing.
" So, I changed <C-I> to <C-O><C-I> and <C-O> to <C-O><C-O> to match.
" I didn't want to lose the <C-I> jump functionality.
noremap <C-O><C-O> <C-O>
noremap <C-O><C-I> <C-I>

" This is what the plugin sets on %. I just set it on <TAB>
onoremap <TAB> :<C-U>call <SNR>41_Match_wrapper('',1,'o')<CR>
nnoremap <TAB> :<C-U>call <SNR>41_Match_wrapper('',1,'n')<CR>
vnoremap <TAB> :<C-U>call <SNR>41_Match_wrapper('',1,'v')<CR>m'gv``
小清晰的声音 2025-01-10 14:34:23

或者,为了以防万一,您也可以使用这些映射(使用 Vim 8.0 进行测试):

nnoremap <silent> <Tab> :normal %<CR>
xnoremap <silent> <Tab> :normal %<CR>m`gv``

Or, just in case, you can also use these mapping (tested with Vim 8.0):

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