LoginSignup
0
1

More than 3 years have passed since last update.

Vimで定型文の補完を行う簡易な方法(2)

Posted at

問題

以前の投稿 Vimで定型文の補完を行う簡易な方法 にて、任意のテキストファイルから補完するスクリプトを紹介した。
紹介したスクリプトは単純であるが、候補表示してからタグのような < を入力するとポップアップメニューが消えてしまい、不便であった。
そこで、カーソル位置の直前にある文字列(空白またはタブ区切り)で絞り込むように修正したので、紹介しておく。

対処

以下を、~/.vimrcに追加する。

function! s:CompleteFrom(files)
  let pos = col('.') - 1
  let line = getline('.')
  let start = pos
  while start > 0 && line[start - 1] !~ '[ \t]'
    let start -= 1
  endwhile
  let word = strpart(line, start, pos - start)
  let lines = []
  for f in a:files
    let inputfile = expand(f)
    for line in readfile(inputfile)
      if line =~ '^'.word
        call add(lines, line)
      endif
    endfor
  endfor
  call complete(start + 1, lines)
  return ''
endfunction

inoremap <F10> <C-R>=<SID>CompleteFrom(["$HOME/mydict.txt"])<CR><C-P>
nnoremap <F10> i<C-R>=<SID>CompleteFrom(["$HOME/mydict.txt"])<CR><C-P>
inoremap <F11> <C-R>=<SID>CompleteFrom(["$HOME/mydict2.txt", "$HOME/mydict3.txt"])<CR><C-P>
nnoremap <F11> i<C-R>=<SID>CompleteFrom(["$HOME/mydict2.txt", "$HOME/mydict3.txt"])<CR><C-P>

仕様については、以前の 投稿 を参照してください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1