Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I had these two questions about this function

" 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 = []

The intent is that I can just hit a hot key and auto complete a list from the file system.


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
share|improve this question
3  
Can you maybe make your question a bit more clear? Pulling it out of the code might help get more people looking at it. –  gabe. Nov 4 '12 at 17:20
    
thats fairly clear. I need to read a file into an array in a language thats 30 years old and I am a little out of practice with. –  James Andino Nov 4 '12 at 17:30

1 Answer 1

You can retrieve the filenames via glob(), like this, which offers all text files in your home directory for completion:

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

To strip off the path, I used fnamemodify(), which I map()'ed over the list.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.