Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Khaled Nassar
Khaled Nassar

Posted on

     

Autocompletion for Neovim

One of the coolest features that can make you more productive with NeoVim is autocompletion, so we have some plugins to choose between them based on the performance

  • YouCompleteMe
  • Coc.nvim

these plugins are great to use but they are very heavy because of Javascript and Python, so I decided to switch to another solution
and the solution is Lua Plugin callednvim-cmp
with this plugin you can add an auto-completion to your neovim with minimal performance, all you have to do is download thelanguage server

Install

before installing the plugins you need to install Lua first

$sudopacman-S lua
Enter fullscreen modeExit fullscreen mode

and you need a Plugin manager you can choose between

  • vim-plug ( I use this )
  • Pathogen
  • Vundle

after installing the nvim-cmp plugin you have to install some external plugins

" ~/.config/nvim/init.nvim    Plug'hrsh7th/cmp-nvim-lsp'    Plug'hrsh7th/cmp-buffer'    Plug'hrsh7th/cmp-path'    Plug'hrsh7th/cmp-cmdline'    Plug'hrsh7th/nvim-cmp'    Plug'hrsh7th/vim-vsnip'
Enter fullscreen modeExit fullscreen mode

after installing the server of your language you may need some Lua code to enable it
so you can use this code to copy & paste into yourinit.nvim file
Also, Don't forget to checkthis page to get the right server for your language

in my case I'll installrust-analyzer

$sudopacman-S rust-analyzer
Enter fullscreen modeExit fullscreen mode
" ~/.config/nvim/init.nvimlua<<EOF-- Setup nvim-cmp.  localcmp= require'cmp'cmp.setup({    snippet={-- REQUIRED- you must specifya snippet engineexpand=function(args)vim.fn["vsnip#anonymous"](args.body)-- For `vsnip` users.-- require('luasnip').lsp_expand(args.body)-- For `luasnip` users.-- require('snippy').expand_snippet(args.body)-- For `snippy` users.--vim.fn["UltiSnips#Anon"](args.body)-- For `ultisnips` users.      end,},    mapping={['<C-b>']=cmp.mapping(cmp.mapping.scroll_docs(-4),{'i','c'}),['<C-f>']=cmp.mapping(cmp.mapping.scroll_docs(4),{'i','c'}),['<C-Space>']=cmp.mapping(cmp.mapping.complete(),{'i','c'}),['<C-y>']=cmp.config.disable,-- Specify `cmp.config.disable`if you wanttoremove the default `<C-y>` mapping.['<C-e>']=cmp.mapping({i=cmp.mapping.abort(),c=cmp.mapping.close(),}),['<CR>']=cmp.mapping.confirm({ select= true}),-- Accept currently selected item. Set `select`to `false`toonlyconfirm explicitly selecteditems.},    sources=cmp.config.sources({{ name='nvim_lsp'},{ name='vsnip'},-- For vsnip users.--{ name='luasnip'},-- For luasnip users.--{ name='ultisnips'},-- For ultisnips users.--{ name='snippy'},-- For snippy users.},{{ name='buffer'},})})-- Set configurationfor specificfiletype.cmp.setup.filetype('gitcommit',{    sources=cmp.config.sources({{ name='cmp_git'},-- You can specify the `cmp_git`sourceif you were installed it.},{{ name='buffer'},})})-- Usebuffersourcefor `/`(if you enabled `native_menu`, this won't work anymore).cmp.setup.cmdline('/',{    sources={{ name='buffer'}}})-- Use cmdline &pathsourcefor':'(if you enabled `native_menu`, this won't work anymore).cmp.setup.cmdline(':',{    sources=cmp.config.sources({{ name='path'}},{{ name='cmdline'}})})-- Setup lspconfig.  local capabilities= require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())-- Replace<YOUR_LSP_SERVER> with eachlsp server you've enabled.  require'lspconfig'.rust_analyzer.setup{      capabilities= capabilities,}EOF
Enter fullscreen modeExit fullscreen mode

You can scroll in the code suggestion with CTRL+B or CTRL+F

Image description

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
mazen000001 profile image
Mazen
  • Joined

pretty helpful !

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I use Arch btw -
  • Joined

More fromKhaled Nassar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp