vim 自动完成自定义列表

vim 自动完成自定义列表

我对这个功能有两个问题

" how do I load a file into a list here?
" set some variable  

func! CustomComplete() 


" and then read the variable here so that b:list = a \n split file ?
let b:list = ["spoogle","spangle","frizzle"]
let b:matches = []

目的是我只需按一下热键即可自动完成文件系统中的列表。


inoremap <F5> <C-R>=CustomComplete()<CR>

" how do I load a file into a list?
func! CustomComplete()

echom 'select word under cursor'
let b:word = expand('<cword>')
echom '->'.b:word.'<-'
echom 'save position'
let b:position = col('.')
echom '->'.b:position.'<-'
normal e
normal l
echom 'move to end of word'

" and then read the list here?
let b:list = ["spoogle","spangle","frizzle"]
let b:matches = []


echom 'begin checking for completion'
for item in b:list
echom 'checking '
echom '->'.item.'<-'
  if(match(item,'^'.b:word)==0)
  echom 'adding to matches'
  echom '->'.item.'<-'
  call add(b:matches,item)
  endif
endfor
call complete(b:position, b:matches)
return ''
    endfunc

答案1

您可以通过 检索文件名glob(),如下所示,它提供主目录中的所有文本文件以供完成:

inoremap <F5> <C-R>=ListFiles()<CR>

func! ListFiles()
    let files = map(split(glob('~/*.txt'), "\n"), 'fnamemodify(v:val, ":t")')
    call complete(col('.'), files)
    return ''
endfunc

为了去掉路径,我使用了fnamemodify(),我将其map()放在列表中。

相关内容