A bare bones modern nvim config in < 100 lines

· Nano Tips for Vim


This is a bare-minimum config to get started with your own nvim config. There is also the excellent kickstart.nvim, but for the sake of simplicity and seeing "just the bare bones" of a modern nvim config, I wanted to share this more minimal config. This config is meant for people who already know lua and how to use and configure nvim, but haven't quite made the jump to their own config yet.

This config includes:

  1. a plugin manager (lazy.nvim)
  2. LSPs (in this case lua_ls and ts_ls)
  3. auto-completions for the LSPs
  4. Treesitter for syntax highlighting

Everything is bootstrapped, meaning lazy.nvim, LSPs, and treesitter parsers are automatically installed on the first run. Less than 100 lines of code.

Usage

  1. Save the code below as: ~/.config/nvim/init.lua (macOS) or $XDG_CONFIG_HOME/nvim/lua/init.lua (Linux)
  2. Run nvim in your terminal.
 1-- Bootstrap the plugin mananager (lazy.nvim)
 2-- copypasted from: https://lazy.folke.io/installation
 3local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
 4if not (vim.uv or vim.loop).fs_stat(lazypath) then
 5	local lazyrepo = "https://github.com/folke/lazy.nvim.git"
 6	local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
 7	if vim.v.shell_error ~= 0 then
 8		vim.api.nvim_echo({
 9			{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
10			{ out, "WarningMsg" },
11			{ "\nPress any key to exit..." },
12		}, true, {})
13		vim.fn.getchar()
14		os.exit(1)
15	end
16end
17vim.opt.rtp:prepend(lazypath)
18
19--------------------------------------------------------------------------------
20
21local myPlugins = {
22	{ -- install LSPs
23		"williamboman/mason.nvim",
24		opts = {},
25		dependencies = {
26			{
27				"WhoIsSethDaniel/mason-tool-installer.nvim", -- auto-install capability
28				opts = {
29					ensure_installed = {
30						"lua_ls",
31						"ts_ls",
32						-- add other LSPs here, find the names here: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
33					},
34					run_on_start = true,
35				},
36			},
37			"williamboman/mason-lspconfig.nvim", -- make mason & lspconfig work together
38		},
39	},
40	{ -- auto-setup LSPs
41		"neovim/nvim-lspconfig",
42		config = function()
43			-- enable completions via nvim-cmp
44			local capabilities = vim.lsp.protocol.make_client_capabilities()
45			capabilities.textDocument.completion.completionItem.snippetSupport = true
46
47			-- run the `.setup` call for each LSP, together with any LSP
48			-- configuration you want to make
49			require("lspconfig").lua_ls.setup({
50				capabilities = capabilities,
51				settings = {
52					Lua = {
53						-- so lua_ls does not complain about `vim` being an undefined global
54						diagnostics = { globals = {"vim"} }
55					}
56				},
57			})
58			require("lspconfig").ts_ls.setup({
59				capabilities = capabilities,
60				settings = {},
61			})
62		end,
63	},
64	{ -- Completion Engine
65		"hrsh7th/nvim-cmp",
66		dependencies = "hrsh7th/cmp-nvim-lsp", -- make cmp work with LSPs
67		config = function()
68			local cmp = require("cmp")
69			cmp.setup({
70				sources = cmp.config.sources({
71					{ name = "nvim_lsp" }, -- tell cmp to use LSPs for completion
72				}),
73				mapping = cmp.mapping.preset.insert({
74					["<CR>"] = cmp.mapping.confirm({ select = true }),
75					["<C-e>"] = cmp.mapping.abort(),
76					["<C-n>"] = cmp.mapping.select_next_item(),
77					["<C-p>"] = cmp.mapping.select_prev_item(),
78				}),
79			})
80		end,
81	},
82	{ -- Syntax Highlighting
83		"nvim-treesitter/nvim-treesitter",
84		build = ":TSUpdate",
85		main = "nvim-treesitter.configs",
86		opts = {
87			auto_install = true, -- auto-install missing parsers when entering buffer
88			highlight = { enable = true },
89		},
90	},
91}
92
93--------------------------------------------------------------------------------
94-- tell lazy to load the plugins
95require("lazy").setup({
96	spec = myPlugins,
97})