我正在尝试在vim
中使用complete()
,以便它也能读取值。
例如,从vim
的complete()
的例子来看,
inoremap <F5> <C-R>=ListMonths()<CR>
func! ListMonths()
call complete(col('.'), ['January', 'February', 'March',
'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'])
return ''
endfunc
如果我键入<F5>
,我会弹出所有月份。现在,我想要的是,如果我键入"J",则只显示1月、6月和7月,"Ju"将显示6月和七月,依此类推
我读过vim-doc
,也试过complete_check
,但事实并非如此。
此外,我曾尝试在vimdoc
中使用全全示例E839
,但我无法正确调用它,总是得到无效的参数。
请给我推荐菜单的首选方法,并在我打字时完成,以及如何使用它。
首先,该示例完成不考虑已经键入的基,因为它总是在光标位置开始完成(通过col('.')
)。
其次,要获得"键入时细化列表"行为,您需要以下设置:
:set completeopt+=longest
不幸的是,由于一个(长期已知的)错误,complete()
没有考虑'completeopt'
选项。您必须使用'completefunc'
,就像这个重写的例子:
fun! CompleteMonths(findstart, base)
if a:findstart
" locate the start of the word
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~ 'a'
let start -= 1
endwhile
return start
else
echomsg '**** completing' a:base
" find months matching with "a:base"
let res = []
for m in ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
if m =~ '^' . a:base
call add(res, m)
endif
endfor
return res
endif
endfun
inoremap <F5> <C-o>:set completefunc=CompleteMonths<CR><C-x><C-u>