├── .github └── screenshot.jpg ├── lua ├── completion.lua ├── treesitter.lua ├── lsp.lua └── statusbar.lua ├── README.md └── init.vim /.github/screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikvdp/nvim-lsp-config/HEAD/.github/screenshot.jpg -------------------------------------------------------------------------------- /lua/completion.lua: -------------------------------------------------------------------------------- 1 | vim.o.completeopt = "menuone,noselect" 2 | 3 | require'compe'.setup { 4 | enabled = true, 5 | autocomplete = true, 6 | debug = false, 7 | min_length = 1, 8 | preselect = 'enable', 9 | throttle_time = 80, 10 | source_timeout = 200, 11 | incomplete_delay = 400, 12 | max_abbr_width = 100, 13 | max_kind_width = 100, 14 | max_menu_width = 100, 15 | documentation = true, 16 | 17 | source = { 18 | path = {kind = "  (Path)"}, 19 | buffer = {kind = "  (Buffer)"}, 20 | calc = {kind = "  (Calc)"}, 21 | vsnip = {kind = "  (Snippet)"}, 22 | nvim_lsp = {kind = "  (LSP)"}, 23 | -- nvim_lua = {kind = "  "}, 24 | nvim_lua = false, 25 | spell = {kind = "  (Spell)"}, 26 | tags = false, 27 | vim_dadbod_completion = true, 28 | -- snippets_nvim = {kind = "  "}, 29 | -- ultisnips = {kind = "  "}, 30 | treesitter = {kind = "  "}, 31 | emoji = {kind = " ﲃ (Emoji)", filetypes={"markdown", "text"}} 32 | -- for emoji press : (idk if that in compe tho) 33 | } 34 | } 35 | 36 | local t = function(str) 37 | return vim.api.nvim_replace_termcodes(str, true, true, true) 38 | end 39 | 40 | local check_back_space = function() 41 | local col = vim.fn.col('.') - 1 42 | if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then 43 | return true 44 | else 45 | return false 46 | end 47 | end 48 | 49 | -------------------------------------------------------------------------------- /lua/treesitter.lua: -------------------------------------------------------------------------------- 1 | require'nvim-treesitter.configs'.setup { 2 | -- ensure_installed can be "all" or a list of languages { "python", "javascript" } 3 | ensure_installed = {"python", "bash", "javascript", "clojure", "go"}, 4 | 5 | highlight = { -- enable highlighting for all file types 6 | enable = true, -- you can also use a table with list of langs here (e.g. { "python", "javascript" }) 7 | }, 8 | incremental_selection = { 9 | enable = true, -- you can also use a table with list of langs here (e.g. { "python", "javascript" }) 10 | disable = { "cpp", "lua" }, 11 | keymaps = { -- mappings for incremental selection (visual mappings) 12 | init_selection = "gnn", -- maps in normal mode to init the node/scope selection 13 | node_incremental = "grn", -- increment to the upper named parent 14 | scope_incremental = "grc", -- increment to the upper scope (as defined in locals.scm) 15 | node_decremental = "grm", -- decrement to the previous node 16 | } 17 | }, 18 | textobjects = { 19 | -- These are provided by 20 | select = { 21 | enable = true, -- you can also use a table with list of langs here (e.g. { "python", "javascript" }) 22 | keymaps = { 23 | -- You can use the capture groups defined here: 24 | -- https://github.com/nvim-treesitter/nvim-treesitter-textobjects/blob/master/queries/c/textobjects.scm 25 | ["af"] = "@function.outer", 26 | ["if"] = "@function.inner", 27 | ["ab"] = "@block.outer", 28 | ["ib"] = "@block.inner", 29 | ["as"] = "@statement.outer", 30 | ["is"] = "@statement.inner", 31 | }, 32 | }, 33 | }, 34 | } 35 | -------------------------------------------------------------------------------- /lua/lsp.lua: -------------------------------------------------------------------------------- 1 | -- lsp setup 2 | -- Set Default Prefix. 3 | -- Note: You can set a prefix per lsp server in the lv-globals.lua file 4 | vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( 5 | vim.lsp.diagnostic.on_publish_diagnostics, { 6 | virtual_text = { 7 | prefix = "", 8 | spacing = 0, 9 | }, 10 | signs = true, 11 | underline = true, 12 | } 13 | ) 14 | 15 | -- -- uncomment below to enable nerd-font based icons for diagnostics in the 16 | -- -- gutter, see: 17 | -- -- https://github.com/neovim/nvim-lspconfig/wiki/UI-customization#change-diagnostic-symbols-in-the-sign-column-gutter 18 | -- local signs = { Error = " ", Warning = " ", Hint = " ", Information = " " } 19 | -- 20 | -- for type, icon in pairs(signs) do 21 | -- local hl = "LspDiagnosticsSign" .. type 22 | -- vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" }) 23 | -- end 24 | 25 | -- symbols for autocomplete 26 | vim.lsp.protocol.CompletionItemKind = { 27 | "  (Text) ", 28 | "  (Method)", 29 | "  (Function)", 30 | "  (Constructor)", 31 | " ﴲ (Field)", 32 | "[] (Variable)", 33 | "  (Class)", 34 | " ﰮ (Interface)", 35 | "  (Module)", 36 | " 襁 (Property)", 37 | "  (Unit)", 38 | "  (Value)", 39 | " 練 (Enum)", 40 | "  (Keyword)", 41 | "  (Snippet)", 42 | "  (Color)", 43 | "  (File)", 44 | "  (Reference)", 45 | "  (Folder)", 46 | "  (EnumMember)", 47 | " ﲀ (Constant)", 48 | " ﳤ (Struct)", 49 | "  (Event)", 50 | "  (Operator)", 51 | "  (TypeParameter)" 52 | } 53 | 54 | local function documentHighlight(client, bufnr) 55 | -- Set autocommands conditional on server_capabilities 56 | if client.resolved_capabilities.document_highlight then 57 | vim.api.nvim_exec( 58 | [[ 59 | hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646 60 | hi LspReferenceText cterm=bold ctermbg=red guibg=#464646 61 | hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646 62 | augroup lsp_document_highlight 63 | autocmd! * 64 | autocmd CursorHold lua vim.lsp.buf.document_highlight() 65 | autocmd CursorMoved lua vim.lsp.buf.clear_references() 66 | augroup END 67 | ]], 68 | false 69 | ) 70 | end 71 | end 72 | 73 | -- configure mason and it's LSP integration (provides the :LspInstall command) 74 | require("mason").setup() 75 | require("mason-lspconfig").setup() 76 | 77 | require("mason-lspconfig").setup_handlers { 78 | -- This is a default handler that will be called for each installed server (also for new servers that are installed during a session) 79 | function(server_name) 80 | if server_name then 81 | local lspconfig = require 'lspconfig' 82 | lspconfig[server_name].setup {} 83 | end 84 | end, 85 | -- You can also override the default handler for specific servers by providing them as keys, like so: 86 | -- ["pyright"] = function() 87 | -- -- for example, you could use the below to specify which python 88 | -- -- interpreter you'd like to use with to use with pyright 89 | -- require "lspconfig".pyright.setup { 90 | -- settings = {python = {pythonPath = "/Users/nik/miniconda3/bin/python"}} 91 | -- } 92 | -- end 93 | } 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Neovim with LSP and tree-sitter 2 | 3 | An opinionated and simple neovim configuration with native LSP and treesitter 4 | support. 5 | 6 | You can use this repo either as a base config to build your own neovim 7 | configuration on top of, or as an example of how to configure neovim with 8 | treesitter and LSP support. 9 | 10 | ## Why would you want this? 11 | 12 | In short, because LSP and treesitter together give neovim superpowers. 13 | Treesitter provides highlighting and syntax checking for almost any 14 | programming language by parsing and creating an AST of the file. This 15 | beats the pants off of previous approaches which used error-prone regexes 16 | that didn't necessarily understand the *contents* of your code. 17 | 18 | Neovim's new native LSP client means Neovim can now do things like 19 | intellisense/auto-completion, automated code refactoring, navigating to 20 | definitions (even across import statements or multiple files) and even 21 | automated code formatting. 22 | 23 | In short, nearly everything that you would get with a full-on IDE like 24 | PyCharm or VS Code, but with all the speed, power and customizability of 25 | vim at your disposal. 26 | 27 | This repo also includes 28 | [Telescope](https://github.com/nvim-telescope/telescope.nvim) which gives 29 | neovim fuzzy-finding capabilities on par with 30 | [fzf](https://github.com/junegunn/fzf) and even lets you do 31 | [ripgrep](https://github.com/BurntSushi/ripgrep) like searches across all 32 | the files in your project (or just the ones committed in git!). 33 | 34 | For a demo of the final setup's capabilities check the video: 35 | 36 |

37 | 38 | 39 | 40 |

41 | 42 | # Installation 43 | 44 | This repo uses recently neovim-specific features, and as a result **this repo requires 45 | [neovim 0.5.0+/nightly](https://github.com/neovim/neovim/releases/tag/nightly). This configuration will not work with vanilla vim or 46 | neovim 0.4** 47 | 48 | To install, first make sure you have installed [neovim nightly](https://github.com/neovim/neovim/releases/tag/nightly). 49 | 50 | If you already have an existing neovim configuration, then it's best to 51 | cherrypick the pieces of this configuration into your own files as you see fit. 52 | 53 | If you're configuring neovim for the first time, then do the following: 54 | 55 | 1. clone this repo into nvim config location: 56 | 57 | ```bash 58 | mkdir -p ~/.config/nvim 59 | git clone https://github.com/nikvdp/nvim-lsp-config/ ~/.config/nvim 60 | ``` 61 | 2. install vim-plug: 62 | 63 | ```bash 64 | sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \ 65 | https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim' 66 | ``` 67 | 68 | 3. Install a [nerdfont](https://github.com/ryanoasis/nerd-fonts) and configure your terminal to use it (otherwise icons will not display correctly) 69 | 70 | On a mac you can install one via, eg: 71 | ``` 72 | brew tap homebrew/cask-fonts 73 | brew install --cask font-victor-mono-nerd-font 74 | ``` 75 | and then select "VictorMono Nerd Font" in your terminal emulator's font config. 76 | 77 | For iTerm2, this means hit `⌘+,` to open up preferences, then go to Profiles 78 | -> Text -> Font and use the dropdown to select "VictorMono Nerd Font" 79 | 80 | Linux or Windows pelase follow the instructions provided at the [nerd fonts repository](https://github.com/ryanoasis/nerd-fonts). 81 | 82 | 4. Install plugins: 83 | 84 | ```shell 85 | nvim '+PlugInstall | qa' 86 | ``` 87 | 88 | 5. Start neovim: `nvim` 89 | 6. Install LSPs for the languages you care about via eg `:LspInstall python`. 90 | You can use tab completion after typing `:LspInstall ` to see which language 91 | servers are available 92 | 93 | 7. Add/customize your keybindings to `~/.config/nvim/init.vim`. 94 | -------------------------------------------------------------------------------- /init.vim: -------------------------------------------------------------------------------- 1 | " >> initialize vim plug (see [1]) 2 | " [1] https://github.com/junegunn/vim-plug/wiki/tips#automatic-installation) 3 | let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim' 4 | if empty(glob(data_dir . '/autoload/plug.vim')) 5 | silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim' 6 | autocmd VimEnter * PlugInstall --sync | source $MYVIMRC 7 | endif 8 | 9 | " >> load plugins 10 | call plug#begin(stdpath('data') . 'vimplug') 11 | Plug 'nvim-lua/plenary.nvim' 12 | Plug 'nvim-lua/popup.nvim' 13 | Plug 'nvim-telescope/telescope.nvim' 14 | Plug 'williamboman/mason.nvim' 15 | Plug 'williamboman/mason-lspconfig.nvim', { 'branch': 'main' } 16 | Plug 'neovim/nvim-lspconfig' 17 | Plug 'tami5/lspsaga.nvim', { 'branch': 'main' } 18 | Plug 'hrsh7th/nvim-compe' 19 | Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'} 20 | Plug 'nvim-treesitter/nvim-treesitter-textobjects' 21 | 22 | Plug 'NTBBloodbath/galaxyline.nvim', { 'branch': 'main' } " Maintained galaxyline fork 23 | Plug 'kyazdani42/nvim-web-devicons' " needed for galaxyline icons 24 | 25 | Plug 'NLKNguyen/papercolor-theme' 26 | Plug 'nikvdp/neomux' 27 | 28 | Plug 'tpope/vim-ragtag' 29 | Plug 'tpope/vim-surround' 30 | Plug 'tpope/vim-unimpaired' 31 | 32 | Plug 'tpope/vim-eunuch' 33 | Plug 'tpope/vim-fugitive' 34 | 35 | Plug 'tomtom/tcomment_vim' 36 | call plug#end() 37 | 38 | 39 | 40 | colorscheme PaperColor 41 | 42 | " basic settings 43 | syntax on 44 | set number 45 | set relativenumber 46 | set ignorecase " ignore case 47 | set smartcase " but don't ignore it, when search string contains uppercase letters 48 | set nocompatible 49 | set incsearch " do incremental searching 50 | set visualbell 51 | set expandtab 52 | set tabstop=4 53 | set ruler 54 | set smartindent 55 | set shiftwidth=4 56 | set hlsearch 57 | set virtualedit=all 58 | set backspace=indent,eol,start " allow backspacing over everything in insert mode 59 | set autoindent 60 | set mouse=a " mouse support 61 | 62 | 63 | " set leader key to , 64 | let g:mapleader="," 65 | 66 | " >> Telescope bindings 67 | nnoremap pp lua require'telescope.builtin'.builtin{} 68 | 69 | " most recently used files 70 | nnoremap m lua require'telescope.builtin'.oldfiles{} 71 | 72 | " find buffer 73 | nnoremap ; lua require'telescope.builtin'.buffers{} 74 | 75 | " find in current buffer 76 | nnoremap / lua require'telescope.builtin'.current_buffer_fuzzy_find{} 77 | 78 | " bookmarks 79 | nnoremap ' lua require'telescope.builtin'.marks{} 80 | 81 | " git files 82 | nnoremap f lua require'telescope.builtin'.git_files{} 83 | 84 | " all files 85 | nnoremap bfs lua require'telescope.builtin'.find_files{} 86 | 87 | " ripgrep like grep through dir 88 | nnoremap rg lua require'telescope.builtin'.live_grep{} 89 | 90 | " pick color scheme 91 | nnoremap cs lua require'telescope.builtin'.colorscheme{} 92 | 93 | 94 | " >> setup nerdcomment key bindings 95 | let g:NERDCreateDefaultMappings = 0 96 | let g:NERDSpaceDelims = 1 97 | 98 | xnoremap ci call NERDComment('n', 'toggle') 99 | nnoremap ci call NERDComment('n', 'toggle') 100 | 101 | 102 | " >> Lsp key bindings 103 | nnoremap gd lua vim.lsp.buf.definition() 104 | nnoremap lua vim.lsp.buf.definition() 105 | nnoremap gD lua vim.lsp.buf.declaration() 106 | nnoremap gr lua vim.lsp.buf.references() 107 | nnoremap gi lua vim.lsp.buf.implementation() 108 | nnoremap K Lspsaga hover_doc 109 | nnoremap lua vim.lsp.buf.signature_help() 110 | nnoremap Lspsaga diagnostic_jump_prev 111 | nnoremap Lspsaga diagnostic_jump_next 112 | nnoremap gf lua vim.lsp.buf.formatting() 113 | nnoremap gn lua vim.lsp.buf.rename() 114 | nnoremap ga Lspsaga code_action 115 | xnoremap ga Lspsaga range_code_action 116 | nnoremap gs Lspsaga signature_help 117 | 118 | lua <