Nvim-cmp configuration for auto-completion

I switched my configuration to defaults.nvim as suggested on mjlibach/defaults.nvim and need some help with the completion behaviour.

First, I wanted to buffers to be part of the completion source so I added those, no problems, it just works

Now I want to completion to pop as I type. I don’t want to use <ctrl> - p in insert mode. So I installed
vim-scripts/AutoComplPop . It is not working well. The menu is showing for a milli and then closes immediately (like some event just closed it). If I press <ctrl> - p is does show up the menu.

I removed AutoComlPop plugin, and disabled mouse mode to ensure no events get in the way. no success.

What is the right way to achieve this?

P.S - love the lua configuration form. Finally something I can comprehend :slight_smile:

nvim-cmp is an autocompletion plugin so it should start opening up the popup menu once you start typing, no need for AutoComplPop to be installed.

Maybe you didn’t install the sources? nvim-cmp is just the plugin, to see the items in the popup menu, cmp needs sources to get items from. The simplest form is to install the sources and then add to the cmp.setup().

Here is how I did it with packer to install the plugin and setup:

-- inside packer startup function
use {'hrsh7th/nvim-cmp'},
use {'hrsh7th/cmp-buffer'},
use {'hrsh7th/cmp-nvim-lsp'},
use {'quangnguyen30192/cmp-nvim-ultisnips'}
-- After packer
local cmp = require('cmp')

cmp.setup({
  mapping = {
    ['<C-Space>'] = cmp.mapping.complete(),
  },

  -- You should specify your *installed* sources.
  sources = {
    { name = 'buffer' },
    { name = 'nvim_lsp' },
    { name = 'ultisnips' },
  },
})
3 Likes

I added the cmp-buffer (I thought maybe buffer is out of the box source) and it started working.
Thanks a lot!

1 Like

I think I have found [here](-- nvim-cmp Configuration · GitHub), more complete settings for nvim-cmp + ultisnips

It depend on other plugins (pay attention to this). Lspkind, for example depends on patched fonts. I uploaded a pack of fonts here

local cmp = require('cmp')

local t = function(str)
    return vim.api.nvim_replace_termcodes(str, true, true, true)
end

local check_back_space = function()
    local col = vim.fn.col(".") - 1
    return col == 0 or vim.fn.getline("."):sub(col, col):match("%s") ~= nil
end

cmp.setup {

    formatting = {
        format = function(entry, vim_item)
            -- fancy icons and a name of kind
            vim_item.kind = require("lspkind").presets.default[vim_item.kind] ..
                                " " .. vim_item.kind
            -- set a name for each source
            vim_item.menu = ({
                buffer = "[Buffer]",
                nvim_lsp = "[LSP]",
                ultisnips = "[UltiSnips]",
                nvim_lua = "[Lua]",
                cmp_tabnine = "[TabNine]",
                look = "[Look]",
                path = "[Path]",
                spell = "[Spell]",
                calc = "[Calc]",
                emoji = "[Emoji]"
            })[entry.source.name]
            return vim_item
        end
    },
    mapping = {
        ['<C-p>'] = cmp.mapping.select_prev_item(),
        ['<C-n>'] = cmp.mapping.select_next_item(),
        ['<C-d>'] = cmp.mapping.scroll_docs(-4),
        ['<C-f>'] = cmp.mapping.scroll_docs(4),
        ['<C-Space>'] = cmp.mapping.complete(),
        ['<C-e>'] = cmp.mapping.close(),
        ['<CR>'] = cmp.mapping.confirm({
            behavior = cmp.ConfirmBehavior.Insert,
            select = true
        }),
        ["<Tab>"] = cmp.mapping(function(fallback)
            if vim.fn.pumvisible() == 1 then
                if vim.fn["UltiSnips#CanExpandSnippet"]() == 1 or
                    vim.fn["UltiSnips#CanJumpForwards"]() == 1 then
                    return vim.fn.feedkeys(t(
                                               "<C-R>=UltiSnips#ExpandSnippetOrJump()<CR>"))
                end

                vim.fn.feedkeys(t("<C-n>"), "n")
            elseif check_back_space() then
                vim.fn.feedkeys(t("<tab>"), "n")
            else
                fallback()
            end
        end, {"i", "s"}),
        ["<S-Tab>"] = cmp.mapping(function(fallback)
            if vim.fn.pumvisible() == 1 then
                vim.fn.feedkeys(t("<C-p>"), "n")
            else
                fallback()
            end
        end, {"i", "s"})
    },
    snippet = {expand = function(args) vim.fn["UltiSnips#Anon"](args.body) end},
    sources = {
        {name = 'buffer'}, {name = 'nvim_lsp'}, {name = "ultisnips"},
        {name = "nvim_lua"}, {name = "look"}, {name = "path"},
        {name = 'cmp_tabnine'}, {name = "calc"}, {name = "spell"},
        {name = "emoji"}
    },
    completion = {completeopt = 'menu,menuone,noinsert'}
}

-- Autopairs
require("nvim-autopairs.completion.cmp").setup({
    map_cr = true,
    map_complete = true,
    auto_select = true
})

-- TabNine
local tabnine = require('cmp_tabnine.config')
tabnine:setup({max_lines = 1000, max_num_results = 20, sort = true})

-- Database completion
vim.api.nvim_exec([[
autocmd FileType sql,mysql,plsql lua require('cmp').setup.buffer({ sources = {{ name = 'vim-dadbod-completion' }} })
]], false)