
【neovim】: 使っているプラグイン 5 ddc.vim
※2025/12/29 TABキーの設定がおかしかったので修正 と、あと少しコメントを増やしました。
ということで、ddu.vim をやったので
ddc.vim のほうも。
かつてのUnite.vim とか denite.vim の後継であるところのddu.vim に対して、こちらは neocomplete.vim とか deoplete.vim の後継な補完機能を提供するプラグインです。
とりあえずdduと同じような感じでvim script をこちらはhook_add ではなくて hook_source、つまりプラグインが読み込まれたあとのタイミングで設定をしています。
まずはvimscript版
#ddc .toml
[[plugins]]
repo = 'Shougo/ddc.vim'
on_event = 'InsertEnter'
depends = ['denops.vim']
hook_source = '''
set shortmess+=c
"set ui
call ddc#custom#patch_global('ui','native')
"sources
if &filetype == 'choshoctl'
call ddc#custom#patch_global('sources', ['vsnip'])
else
call ddc#custom#patch_global('sources', ['around', 'file'])
endif
"configure sources
call ddc#custom#patch_global('sourceOptions', #{
\ _: #{
\ matchers: ['matcher_head','matcher_length'],
\ sorters: ['sorter_rank'],
\ converters: ['converter_remove_overlap'],
\ },
\ around: #{
\ mark: 'A'
\ },
\ file: #{
\ mark: 'F',
\ isVolatile: v:true,
\ forceCompletionPattern: '\S/\S*',
\ },
\ nvim-lsp: #{
\ mark: 'L',
\ forceCompletionPattern: '\.\w*|:\w*|->\w*',
\ },
\ vsnip: #{
\ mark: 'vsnip',
\ },
\ })
"set parameters for sources
call ddc#custom#patch_global('sourceParams', {
\ 'around': {'maxSize': 500},
\ 'file': {'smartCase': v:true},
\ })
"to use TAB or S-TAB over popup menu
inoremap <silent><expr> <TAB>
\ pumvisible() ? '<C-n>' :
\ (col('.') <= 1 <Bar><Bar> getline('.')[col('.') - 2] =~# '\s') ?
\ '<TAB>' : ddc#map#manual_complete()
inoremap <expr><S-TAB> pumvisible() ? '<C-p>' : '<C-h>'
"activate ddc
call ddc#enable()
'''
そしてこれを(ほぼGeminiが)以下のようにlua形式に
#ddc .toml
[[plugins]]
repo = 'Shougo/ddc.vim'
on_event = 'InsertEnter'
depends = ['denops.vim']
lua_source = '''
require('config_ddc')
'''--config_ddc.lua
-- configuration for ddc.vim
local fn = vim.fn
-- 1. 基本設定
-- 補完時のメッセージを抑制する
vim.opt.shortmess:append("c")
-- 2. Global Patch (全体設定)
fn["ddc#custom#patch_global"]({
-- UIにnativeを使用
ui = 'native',
-- デフォルトソース
sources = { 'around', 'file' },
sourceOptions = {
['_'] = {
matchers = { 'matcher_head', 'matcher_length' },
sorters = { 'sorter_rank' },
-- 重複削除
converters = { 'converter_remove_overlap' },
},
around = { mark = 'A' },
file = {
mark = 'F',
isVolatile = true,
forceCompletionPattern = [[\S/\S*]],
},
['nvim-lsp'] = {
mark = 'L',
forceCompletionPattern = [[\.\w*|:\w*|->\w*]],
},
vsnip = { mark = 'vsnip' },
},
sourceParams = {
around = { maxSize = 500 },
file = { smartCase = true },
}
})
-- 3. Filetype 特有の設定 (choshoctl 用)
-- Luaの if 文で分岐させる代わりに ddc の機能でスマートに管理できます
fn["ddc#custom#patch_filetype"]('choshoctl', {
sources = { 'vsnip' }
})
-- 4. キーマッピング
local opts = { silent = true, expr = true, replace_keycodes = true }
-- TABで次の候補を選択、または手動補完を開始
vim.keymap.set('i', '<TAB>', function()
-- ポップアップウィンドウが表示されていたら
if fn.pumvisible() == 1 then
-- (TAB文字を入力するのではなく)次の行に行く
return '<C-n>'
end
-- 現在のカーソルの位置を取得
local col = fn.col('.')
-- 現在の行を取得
local line = fn.getline('.')
-- カーソルの位置が行の先頭か、スペースの後だったらTAB文字を入力
if col <= 1 or line:sub(col - 1, col - 1):match('%s') then
return '<TAB>'
end
-- 上記いずれでもない場合はマニュアルで補完する
return fn["ddc#map#manual_complete"]()
end, opts)
-- S-TABで一つ前の候補を選択
vim.keymap.set('i', '<S-TAB>', function()
return fn.pumvisible() == 1 and '<C-p>' or '<C-h>'
end, opts)
-- 5. ddcの有効化
fn["ddc#enable"]() ※コメントはGeminiと僕
実は僕はいわゆるIDE(プログラミング用の統合開発環境)のようにはあまり使っていなくて、
#snippet
[[plugins]]
repo = 'hrsh7th/vim-vsnip'
on_event = 'InsertEnter'
on_ft = 'choshoctl'
lua_add = '''
require('config_vim-vsnip')
'''
#vsnip integration for ddc
[[plugins]]
repo = 'uga-rosa/ddc-source-vsnip'
on_source ='ddc.vim'のようにインストールしているsnippetのプラグイン vsnip
を使って、こんな感じの、snippet部分がただの文字列のjsonを読み込んで
"doroho_01":{"prefix":"doroho_01",
"body":"調書名=道路台帳第1表",
"description":"道路台帳第1表"
},
"doroho_02":{"prefix":"doroho_02",
"body":"調書名=道路台帳第2表(実延長調書)",
"description":"実延長調書"
},
"dorogenkyo_01":{"prefix":"dorogenkyo_01",
"body":"調書名=道路現況調書(2葉の1)",
"description":"2葉の1 "
},
"dorogenkyo_02":{"prefix":"dorogenkyo_02",
"body":"調書名=道路現況調書(2葉の2)",
"description":"2葉の2 "こんな風に超絶面倒な文字列をインデックスで選んで入力できるようにしています。

colorscheme は neovim はnefertiti、lualine は everforest です。
絞り込みに使う文字列と、選ぶために表示する文字列と、実際に入力される文字列が違う のを実現するのには普通の補完でなくてsnippetがいいなと思ってやっているのですが、なんとなくddc.vim のsourceを自作するとすればこの辺(jsonでなくてcsvで並べて書けば使えるみたいな)かなと思います。
なぜならこんなページまで作ってくれているからです。