以下是编写vim插件的实践代码。我按照vim文档编写它::help usr_41.txt
第41.11节写一个插件。
let s:save_cpo = &cpo
set cpo&vim
if exists("g:loaded_echoplugin")
finish
endif
function s:EchoWord()
echo expand('<cword>')
endfunction
if !exists(":EchoWord")
command -nargs=0 EchoWord :call s:EchoWord()
endif
if !hasmapto('<Plug>EchoWord')
map <F8> <Plug>EchoWord
endif
noremap <script> <Plug>EchoWord <SID>EchoWord
noremap <SID>EchoWord :call <SID>EchoWord()<CR>
let g:loaded_echoplugin = 1
let &cpo = s:save_cpo
unlet s:save_cpo
代码用于在单击<F8>
时显示光标下的单词。这是映射序列:<F8> -> <Plug>EchoWord -> <SID>EchoWord -> :call <SID>Echoword()
,它可以工作。
但是,我有两个问题:
1.我在这里使用noremap
,为什么它仍然可以重新映射或递归映射?
2.如果我将映射从map <F8> <Plug>EchoWord
更改为noremap <F8> <Plug>EchoWord
,则会执行 NOT 工作。
有人可以帮忙解决一下吗?谢谢!
答案 0 :(得分:5)
<Plug>Foo
是一个映射。无论它本身是递归还是无关紧要。
当你进行递归映射时,Vim使用右侧命令的当前含义:
map b B
map <key> db " works like dB
当您执行非递归映射时,Vim使用右侧命令的原始含义:
map b B
map <key> db " works like db
默认情况下, <Plug>Foo
并不意味着什么,所以没有点非递归地映射它。
你想要递归,这里你应该使用map
,imap
,nmap
等。
答案 1 :(得分:1)
我仔细阅读了相关命令的vim文档,并找到了根本原因。我在这里添加它只是为了任何可能关心的人。
输入命令:help :map-<script>
,原因如下:
Note: ":map <script>" and ":noremap <script>" do the same thing. The
"<script>" overrules the command name. Using ":noremap <script>" is
preferred, because it's clearer that remapping is (mostly) disabled.