vimはカスタムリストを自動補完します。

vimはカスタムリストを自動補完します。

この機能については2つの質問があります。

" 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()

おすすめ記事