nvim-cmp

repository·main·Indexed 27 days ago

https://github.com/hrsh7th/nvim-cmp

A Lua-based completion engine for Neovim providing full support for LSP completion capabilities. It is highly customizable via Lua functions and requires a snippet engine for operation.

Tokens
1.3K
Snippets
3
Records
3
Agent score
44%

What's inside nvim-cmp

  1. Integrate nvim-cmp with LSP

    main

    To enable LSP completion, you must pass the capabilities provided by cmp-nvim-lsp to your LSP server configuration. This ensures the LSP server knows the client supports completion capabilities.

    -- Set up lspconfig.
    local capabilities = require('cmp_nvim_lsp').default_capabilities()
    -- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
    vim.lsp.config('<YOUR_LSP_SERVER>', {
      capabilities = capabilities
    })
    vim.lsp.enable('<YOUR_LSP_SERVER>')
  2. Install and configure nvim-cmp

    main

    To use nvim-cmp, you must install the engine along with various completion sources (e.g., cmp-nvim-lsp, cmp-buffer, cmp-path). You also need a snippet engine (e.g., vsnip, LuaSnip, or native Neovim snippets) because specifying a snippet engine in the snippet.expand function is REQUIRED.

    This example uses vim-plug and vsnip.

    call plug#begin(s:plug_dir)
    Plug 'neovim/nvim-lspconfig'
    Plug 'hrsh7th/cmp-nvim-lsp'
    Plug 'hrsh7th/cmp-buffer'
    Plug 'hrsh7th/cmp-path'
    Plug 'hrsh7th/cmp-cmdline'
    Plug 'hrsh7th/nvim-cmp'
    
    " For vsnip users.
    Plug 'hrsh7th/cmp-vsnip'
    Plug 'hrsh7th/vim-vsnip'
    
    call plug#end()
    
    lua <<EOF
      local cmp = require'cmp'
    
      cmp.setup({
        snippet = {
          -- REQUIRED - you must specify a snippet engine
          expand = function(args) 
            vim.fn["vsnip#anonymous"](args.body) 
          end,
        },
        mapping = cmp.mapping.preset.insert({
          ['<C-b>'] = cmp.mapping.scroll_docs(-4),
          ['<C-f>'] = cmp.mapping.scroll_docs(4),
          ['<C-Space>'] = cmp.mapping.complete(),
          ['<C-e>'] = cmp.mapping.abort(),
          ['<CR>'] = cmp.mapping.confirm({ select = true }),
        }),
        sources = cmp.config.sources({
          { name = 'nvim_lsp' },
          { name = 'vsnip' },
        }, {
          { name = 'buffer' },
        })
      })
    
      -- Use buffer source for / and ?
      cmp.setup.cmdline({ '/', '?' }, {
        mapping = cmp.mapping.preset.cmdline(),
        sources = {
          { name = 'buffer' }
        }
      })
    
      -- Use cmdline & path source for :
      cmp.setup.cmdline(':', {
        mapping = cmp.mapping.preset.cmdline(),
        sources = cmp.config.sources({
          { name = 'path' }
        }, {
          { name = 'cmdline' }
        }),
        matching = { disallow_symbol_nonprefix_matching = false }
      })
    EOF
  3. Configure nvim-cmp cmdline completion

    main

    You can configure specific completion behavior for the command line using cmp.setup.cmdline.

    • For / and ? search commands, you can use the buffer source.
    • For the : command, you can combine path and cmdline sources.

    Note: If you enable native_menu, these specific configurations may not work as expected.

    -- Use buffer source for `/` and `?` 
    cmp.setup.cmdline({ '/', '?' }, {
      mapping = cmp.mapping.preset.cmdline(),
      sources = {
        { name = 'buffer' }
      }
    })
    
    -- Use cmdline & path source for ':'
    cmp.setup.cmdline(':', {
      mapping = cmp.mapping.preset.cmdline(),
      sources = cmp.config.sources({
        { name = 'path' }
      }, {
        { name = 'cmdline' }
      }),
      matching = { disallow_symbol_nonprefix_matching = false }
    })