1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# Recipes
[[toc]]
## General
### Disable per filetype
```lua
enabled = function()
return not vim.tbl_contains({ "lua", "markdown" }, vim.bo.filetype)
and vim.bo.buftype ~= "prompt"
and vim.b.completion ~= false
end,
```
### Border
```lua
completion = {
menu = { border = 'single' },
documentation = { window = { border = 'single' } },
},
signature = { window = { border = 'single' } },
```
### Change selection type per mode
```lua
completion = {
list = {
selection = {
preselect = function(ctx) return ctx.mode ~= 'cmdline' end,
auto_insert = function(ctx) return ctx.mode ~= 'cmdline' end
}
}
}
```
### Don't show completion menu automatically in cmdline mode
```lua
completion = {
menu = { auto_show = function(ctx) return ctx.mode ~= 'cmdline' end }
}
```
### Don't show completion menu automatically when searching
```lua
completion = {
menu = {
auto_show = function(ctx)
return ctx.mode ~= "cmdline" or not vim.tbl_contains({ '/', '?' }, vim.fn.getcmdtype())
end,
},
}
```
### Select Nth item from the list
Here's an example configuration that allows you to select the nth item from the list, based on [#382](https://github.com/Saghen/blink.cmp/issues/382):
```lua
keymap = {
preset = 'default',
['<A-1>'] = { function(cmp) cmp.accept({ index = 1 }) end },
['<A-2>'] = { function(cmp) cmp.accept({ index = 2 }) end },
['<A-3>'] = { function(cmp) cmp.accept({ index = 3 }) end },
['<A-4>'] = { function(cmp) cmp.accept({ index = 4 }) end },
['<A-5>'] = { function(cmp) cmp.accept({ index = 5 }) end },
['<A-6>'] = { function(cmp) cmp.accept({ index = 6 }) end },
['<A-7>'] = { function(cmp) cmp.accept({ index = 7 }) end },
['<A-8>'] = { function(cmp) cmp.accept({ index = 8 }) end },
['<A-9>'] = { function(cmp) cmp.accept({ index = 9 }) end },
['<A-0>'] = { function(cmp) cmp.accept({ index = 10 }) end },
},
completion = {
menu = {
draw = {
columns = { { 'item_idx' }, { 'kind_icon' }, { 'label', 'label_description', gap = 1 } },
components = {
item_idx = {
text = function(ctx) return ctx.idx == 10 and '0' or ctx.idx >= 10 and ' ' or tostring(ctx.idx) end,
highlight = 'BlinkCmpItemIdx' -- optional, only if you want to change its color
}
}
}
}
}
```
### `mini.icons`
[Original discussion](https://github.com/Saghen/blink.cmp/discussions/458)
```lua
completion = {
menu = {
draw = {
components = {
kind_icon = {
ellipsis = false,
text = function(ctx)
local kind_icon, _, _ = require('mini.icons').get('lsp', ctx.kind)
return kind_icon
end,
-- Optionally, you may also use the highlights from mini.icons
highlight = function(ctx)
local _, hl, _ = require('mini.icons').get('lsp', ctx.kind)
return hl
end,
}
}
}
}
}
```
### Hide Copilot on suggestion
```lua
vim.api.nvim_create_autocmd('User', {
pattern = 'BlinkCmpMenuOpen',
callback = function()
require("copilot.suggestion").dismiss()
vim.b.copilot_suggestion_hidden = true
end,
})
vim.api.nvim_create_autocmd('User', {
pattern = 'BlinkCmpMenuClose',
callback = function()
vim.b.copilot_suggestion_hidden = false
end,
})
```
### Show on newline, tab and space
Note that you may want to add the override to other sources as well, since if the LSP doesnt return any items, we won't show the menu if it was triggered by any of these three characters.
```lua
-- by default, blink.cmp will block newline, tab and space trigger characters, disable that behavior
completion.trigger.blocked_trigger_characters = {}
-- add newline, tab and space to LSP source trigger characters
sources.providers.lsp.override.get_trigger_characters = function(self)
local trigger_characters = self:get_trigger_characters()
vim.list_extend(trigger_characters, { '\n', '\t', ' ' })
return trigger_characters
end
```
## Sources
### Dynamically picking providers by treesitter node/filetype
```lua
sources.default = function(ctx)
local success, node = pcall(vim.treesitter.get_node)
if vim.bo.filetype == 'lua' then
return { 'lsp', 'path' }
elseif success and node and vim.tbl_contains({ 'comment', 'line_comment', 'block_comment' }, node:type()) then
return { 'buffer' }
else
return { 'lsp', 'path', 'snippets', 'buffer' }
end
end
```
### Hide snippets after trigger character
> [!NOTE]
> Untested, might not work well, please open a PR if you find a better solution!
Trigger characters are defined by the sources. For example, for Lua, the trigger characters are `.`, `"`, `'`.
```lua
sources.providers.snippets.should_show_items = function(ctx)
return ctx.trigger.initial_kind ~= 'trigger_character'
end
```
### Disable all snippets
See the [relevant section in the snippets documentation](./configuration/snippets.md#disable-all-snippets)
### Set minimum keyword length by filetype
```lua
sources.min_keyword_length = function()
return vim.bo.filetype == 'markdown' and 2 or 0
end
```
## For writers
When writing prose, you may want significantly different behavior than typical LSP completions. If you find any interesting configurations, please open a PR adding it here!
### Keep first letter capitalization on buffer source
```lua
sources = {
providers = {
buffer = {
-- keep case of first char
transform_items = function (a, items)
local keyword = a.get_keyword()
local correct, case
if keyword:match('^%l') then
correct = '^%u%l+$'
case = string.lower
elseif keyword:match('^%u') then
correct = '^%l+$'
case = string.upper
else
return items
end
-- avoid duplicates from the corrections
local seen = {}
local out = {}
for _, item in ipairs(items) do
local raw = item.insertText
if raw:match(correct) then
local text = case(raw:sub(1,1)) .. raw:sub(2)
item.insertText = text
item.label = text
end
if not seen[item.insertText] then
seen[item.insertText] = true
table.insert(out, item)
end
end
return out
end
}
}
}
```
|