vim auto-complete

 

iabbrev pr print

 

 

Ah, that error happens because Vim doesn't know what plug#begin or Plug means yet—you have the configuration written down, but the actual Vim-Plug manager tool hasn't been downloaded to your machine.

We can fix this in less than a minute.

The Fix

Run this command in your terminal. It will download the missing plug.vim file and put it exactly where Vim looks for it:

Bash
 
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Next Steps

  1. Reopen your config: Open Vim again using vim ~/.vimrc. The errors should completely vanish now.

  2. Install the plugin: Inside Vim, type the following command and hit Enter:

    Vim Script
     
    :PlugInstall
    
   A split window will open up on the left side showing you that `coc.nvim` is downloading and installing. 
3. **Restart:** Once it says `Done!`, close Vim (`:qa`) and reopen it. Your new autocomplete engine will be active!


 

 

 

That error means coc.nvim is trying to start up, but it can't find Node.js and npm installed on your system. Because coc.nvim is essentially a bridge between Vim and VS Code's extension ecosystem, it requires Node.js to run.

You just need to install Node.js (which includes npm) on your Linux system.

How to Fix It

Open your terminal and run the commands for your specific Linux distribution:

For Ubuntu / Debian / Linux Mint:

Bash
 
sudo apt update
sudo apt install -y nodejs npm

For Fedora / RHEL:

Bash
 
sudo dnf install -y nodejs npm

For Arch Linux:

Bash
 
sudo pacman -S nodejs npm

Verify the Installation

Once the installation finishes, make sure your system can see them by running:

Bash
 
node -v
npm -v

If both commands return a version number (like v20.x.x and 10.x.x), you are good to go!

Next Step

Reopen Vim (vim ~/.vimrc). The error should be gone, and coc.nvim will now boot up smoothly in the background.

 

 

 

 

 

To get a full, modern autocomplete dropdown menu (similar to VS Code), the configuration depends on whether you are using Neovim or classic Vim.

Because Neovim has a built-in Language Server Protocol (LSP) client, it uses a plugin called nvim-cmp. Classic Vim usually pairs best with coc.nvim.

Choose the setup below that matches your editor.

Option A: For Neovim (init.lua + nvim-cmp)

This uses lazy.nvim (the most popular modern plugin manager) to install nvim-cmp and hook it up to your code completion engine.

1. Add the Plugins

Add this block to your Lua configuration where you manage your plugins:

Lua
 
return {
  -- The main completion engine
  {
    'hrsh7th/nvim-cmp',
    dependencies = {
      'hrsh7th/cmp-nvim-lsp', -- LSP source for autocomplete
      'hrsh7th/cmp-path',     -- File system paths source
      'hrsh7th/cmp-buffer',   -- Current buffer text source
      'L3MON4D3/LuaSnip',     -- Snippet engine required by nvim-cmp
    },
    config = function()
      local cmp = require('cmp')
      cmp.setup({
        snippet = {
          expand = function(args)
            require('luasnip').lsp_expand(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(), -- Manually trigger dropdown
          ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Enter to confirm
          ['<Tab>'] = cmp.mapping(function(fallback)
            if cmp.visible() then
              cmp.select_next_item()
            else
              fallback()
            end
          end, { 'i', 's' }),
        }),
        sources = cmp.config.sources({
          { name = 'nvim_lsp' },
          { name = 'buffer' },
          { name = 'path' },
        })
      })
    end
  }
}

Option B: For Classic Vim (.vimrc + coc.nvim)

coc.nvim (Conquer of Completion) brings full VS Code-like intelligence to classic Vim. It requires Node.js installed on your system.

1. Install via Vim-Plug

Add this to your .vimrc:

Vim Script
 
call plug#begin()
" Use release branch for coc.nvim
Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()

Run :PlugInstall inside Vim to install it.

2. Add the Auto-Complete Keymaps

Add this configuration to your .vimrc so that <Tab> selects items and <Enter> confirms them:

Vim Script
 
" Use Tab for trigger completion with characters ahead and navigate
inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

" Make <CR> (Enter) to accept selected completion item
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

3. Install Language Packages

Once CoC is running, you just tell it what language you are typing. For example, if you want Python autocompletes, run this command inside Vim:

Vim Script
 
:CocInstall coc-pyright

(For web development, you'd run :CocInstall coc-tsserver coc-html coc-css).

Which one should you pick?

  • Go with Option A if you are on Neovim and want a highly customizable, blazing-fast native Lua setup.

  • Go with Option B if you want it to "just work" out of the box with minimal configuration, or if you are using older Vim.

 

posted @ 2026-05-25 16:21  profesor  阅读(8)  评论(0)    收藏  举报