Skip to content

Instantly share code, notes, and snippets.

@zbyju
Last active March 3, 2024 23:59
Show Gist options
  • Save zbyju/cf5e810528fb926b6ac5b5e3f802ab8b to your computer and use it in GitHub Desktop.
Save zbyju/cf5e810528fb926b6ac5b5e3f802ab8b to your computer and use it in GitHub Desktop.
nvim config
-- TODO: Make pretty
vim.opt.termguicolors = true
vim.opt.background = "dark"
vim.g.mapleader = " "
vim.g.maplocalleader = " "
vim.opt.pumheight = 10
vim.opt.tabstop = 2 -- Set the width of a tab character
vim.opt.shiftwidth = 2 -- Set the width of the indentation
vim.opt.expandtab = true
vim.o.number = true
vim.o.relativenumber = true
vim.opt.signcolumn = "yes"
if vim.fn.has("wsl") == 1 then
vim.api.nvim_create_autocmd("TextYankPost", {
group = vim.api.nvim_create_augroup("Yank", { clear = true }),
callback = function()
vim.fn.system("clip.exe", vim.fn.getreg('"'))
end,
})
end
-- Configuration for diagnostics
local signs = {
{ name = "DiagnosticSignError", text = "´üŚ" },
{ name = "DiagnosticSignWarn", text = "´ü▒" },
{ name = "DiagnosticSignHint", text = "´á┤" },
{ name = "DiagnosticSignInfo", text = "´üÜ" },
}
for _, sign in ipairs(signs) do
vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = "" })
end
local config = {
signs = {
active = signs, -- show signs
},
update_in_insert = false,
underline = true,
severity_sort = true,
float = {
focusable = true,
style = "minimal",
border = "single",
source = "always",
header = "Diagnostic",
prefix = "",
},
}
vim.diagnostic.config(config)
local ensure_packer = function()
local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
if fn.empty(fn.glob(install_path)) > 0 then
fn.system({ "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path })
vim.cmd([[packadd packer.nvim]])
return true
end
return false
end
local packer_bootstrap = ensure_packer()
-- Packer initialization and plugin installation
local packer = require("packer")
packer.startup({
function(use)
-- Packer can manage itself
use("wbthomason/packer.nvim")
-- Common utilities
use("nvim-lua/plenary.nvim")
use("nvim-lua/popup.nvim")
-- Mason for managing LSPs, linters, etc.
use("williamboman/mason.nvim")
use("williamboman/mason-lspconfig.nvim")
use("jay-babu/mason-null-ls.nvim")
use("folke/trouble.nvim")
use("MunifTanjim/nui.nvim")
use("RRethy/vim-illuminate")
use({
"nvim-treesitter/nvim-treesitter",
run = ":TSUpdate",
})
use("neovim/nvim-lspconfig") -- LSP configurations
use("williamboman/nvim-lsp-installer") -- Simple to use language server installer
use("sbdchd/neoformat")
use({
"nvim-telescope/telescope.nvim",
requires = { { "nvim-lua/plenary.nvim" } },
})
use({
"nvim-lualine/lualine.nvim",
requires = { "nvim-tree/nvim-web-devicons", opt = true },
})
use({
"SmiteshP/nvim-navic",
requires = "neovim/nvim-lspconfig",
})
use({
"SmiteshP/nvim-navbuddy",
requires = {
"neovim/nvim-lspconfig",
"SmiteshP/nvim-navic",
"MunifTanjim/nui.nvim",
"numToStr/Comment.nvim", -- Optional
"nvim-telescope/telescope.nvim", -- Optional
},
})
-- Plugin for colorizing and changing color schemes
use("norcalli/nvim-colorizer.lua")
use({
"folke/todo-comments.nvim",
requires = { "nvim-lua/plenary.nvim", "nvim-treesitter/nvim-treesitter" },
})
use("folke/which-key.nvim")
use({
"JellyApple102/easyread.nvim",
})
use({
"numToStr/Comment.nvim",
config = function()
require("Comment").setup({
toggler = {
line = "gc",
block = "gC",
},
opleader = {
line = "gc",
block = "gC",
},
})
end,
})
use({
"nvimdev/dashboard-nvim",
event = "VimEnter",
config = function()
require("dashboard").setup({
-- config
})
end,
requires = { "nvim-tree/nvim-web-devicons" },
})
use("lukas-reineke/indent-blankline.nvim")
use("hrsh7th/nvim-cmp") -- The main completion plugin
use("hrsh7th/cmp-nvim-lsp") -- LSP source for nvim-cmp
use("L3MON4D3/LuaSnip") -- LuaSnip as the snippet engine
use("saadparwaiz1/cmp_luasnip") -- LuaSnip source for nvim-cmp
use("hrsh7th/cmp-buffer")
use("hrsh7th/cmp-path")
use("hrsh7th/cmp-cmdline")
use("rafamadriz/friendly-snippets")
use({
"benfowler/telescope-luasnip.nvim",
})
use({
"windwp/nvim-autopairs",
config = function()
require("nvim-autopairs").setup({})
end,
})
use("windwp/nvim-ts-autotag")
use("nvim-tree/nvim-web-devicons")
-- Colorschemes
use("rebelot/kanagawa.nvim")
use({ "rose-pine/neovim", as = "rose-pine" })
use("sainnhe/gruvbox-material")
use("AlexvZyl/nordic.nvim")
use({ "catppuccin/nvim", as = "catppuccin" })
use("Alexis12119/nightly.nvim")
use({
"rockyzhang24/arctic.nvim",
requires = { "rktjmp/lush.nvim" },
})
use("EdenEast/nightfox.nvim")
use("folke/tokyonight.nvim")
use({
"mcchrish/zenbones.nvim",
requires = "rktjmp/lush.nvim",
})
if packer_bootstrap then
require("packer").sync()
end
end,
config = {
display = {
open_fn = require("packer.util").float,
},
},
})
vim.cmd("colorscheme catppuccin-mocha")
-- Additional Lua configurations
require("mason").setup()
local servers = {
tsserver = {},
}
require("nvim-treesitter.configs").setup({
auto_install = true,
ensure_installed = {
"javascript",
"typescript",
"tsx",
"python",
"lua",
"vim",
"scala",
"java",
"rust",
"go",
"gomod",
},
sync_install = false,
highlight = {
enable = true,
},
-- Additional configuration can be added here
})
local lspconfig = require("lspconfig")
lspconfig.astro.setup({
init_options = {
typescript = {
tsdk = vim.fs.normalize("~/.local/share/pnpm/global/5/node_modules/typescript/lib"),
},
},
})
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
local luasnip = require("luasnip")
local cmp = require("cmp")
-- Load snippets from honza/vim-snippets
require("luasnip.loaders.from_vscode").lazy_load()
require("luasnip.loaders.from_snipmate").lazy_load()
local kind_icons = {
Text = "ţśĺ",
Method = "ˇ░ćž",
Function = "ˇ░ŐĽ",
Constructor = "´Éú",
Field = "ˇ░çŻ",
Variable = "ˇ░éí",
Class = "ˇ░á▒",
Interface = "´âĘ",
Module = "´ĺç",
Property = "ˇ░ťó",
Unit = "ţłč",
Value = "ˇ░Äá",
Enum = "´ůŁ",
Keyword = "ˇ░îő",
Snippet = "´ĹĆ",
Color = "ˇ░Ćś",
File = "ˇ░łÖ",
Reference = "´ĺü",
Folder = "ˇ░ëő",
EnumMember = "´ůŁ",
Constant = "ˇ░Ć┐",
Struct = "´ć│",
Event = "´âž",
Operator = "ˇ░ćĽ",
TypeParameter = "ˇ░ů▓",
}
cmp.setup({
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
formatting = {
format = function(entry, vim_item)
vim_item.kind = string.format("%s %s", kind_icons[vim_item.kind], vim_item.kind)
vim_item.menu = ({
buffer = "[Buffer]",
nvim_lsp = "[LSP]",
luasnip = "[LuaSnip]",
nvim_lua = "[Lua]",
latex_symbols = "[LaTeX]",
})[entry.source.name]
vim_item.abbr = string.sub(vim_item.abbr, 1, 20)
return vim_item
end,
},
window = {
completion = { pumheight = 10 },
},
mapping = {
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, { "i", "s" }),
["<C-l>"] = cmp.mapping(function(fallback)
if luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<C-k>"] = cmp.mapping(function(fallback)
if luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
["<NL>"] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
},
sources = cmp.config.sources({
{ name = "luasnip", priority = 8 },
{ name = "nvim_lsp", priority = 7 },
{ name = "buffer", priority = 5 },
}),
})
cmp.setup.cmdline({ "/", "?" }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = "buffer" },
},
})
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
})
local navic = require("nvim-navic")
local capabilities = require("cmp_nvim_lsp").default_capabilities()
require("mason-lspconfig").setup({
ensure_installed = vim.tbl_keys(servers),
})
require("mason-lspconfig").setup_handlers({
-- The default handler automatically sets up all installed servers
function(server_name)
require("lspconfig")[server_name].setup({
capabilities = capabilities,
on_attach = function(client, bufnr)
-- Check if the language server supports document symbols
if client.server_capabilities.documentSymbolProvider then
navic.attach(client, bufnr)
else
print(
'nvim-navic: Server "'
.. client.name
.. '" does not support documentSymbols. Navic will not attach.'
)
end
end,
settings = servers[server_name],
})
end,
})
require("telescope").load_extension("luasnip")
-- Telescope setup
local actions = require("telescope.actions")
local trouble = require("trouble.providers.telescope")
require("telescope").setup({
pickers = {
colorscheme = {
enable_preview = true,
},
},
defaults = {
mappings = {
i = { ["<c-t>"] = trouble.open_with_trouble },
n = { ["<c-t>"] = trouble.open_with_trouble },
},
},
})
local navbuddy = require("nvim-navbuddy")
local actions = require("nvim-navbuddy.actions")
navbuddy.setup({
lsp = {
auto_attach = true, -- If set to true, you don't need to manually use attach function
},
})
navic.setup({
lsp = {
auto_attach = false,
preference = nil,
},
})
require("lualine").setup({
winbar = {
lualine_c = { "navic" },
lualine_z = { "filename" },
},
})
require("colorizer").setup()
require("todo-comments").setup()
require("easyread").setup({})
local highlight = {
"RainbowRed",
"RainbowYellow",
"RainbowBlue",
"RainbowOrange",
"RainbowGreen",
"RainbowViolet",
"RainbowCyan",
}
local hooks = require("ibl.hooks")
-- create the highlight groups in the highlight setup hook, so they are reset
-- every time the colorscheme changes
hooks.register(hooks.type.HIGHLIGHT_SETUP, function()
vim.api.nvim_set_hl(0, "RainbowRed", { fg = "#E06C75" })
vim.api.nvim_set_hl(0, "RainbowYellow", { fg = "#E5C07B" })
vim.api.nvim_set_hl(0, "RainbowBlue", { fg = "#61AFEF" })
vim.api.nvim_set_hl(0, "RainbowOrange", { fg = "#D19A66" })
vim.api.nvim_set_hl(0, "RainbowGreen", { fg = "#98C379" })
vim.api.nvim_set_hl(0, "RainbowViolet", { fg = "#C678DD" })
vim.api.nvim_set_hl(0, "RainbowCyan", { fg = "#56B6C2" })
end)
require("ibl").setup({ indent = { highlight = highlight } })
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
local cmp = require("cmp")
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
require("nvim-treesitter.configs").setup({
autotag = {
enable = true,
enable_rename = true,
enable_close = true,
enable_close_on_slash = true,
},
})
local wk = require("which-key")
wk.register({
f = {
name = "Search",
f = { "<cmd>Telescope find_files<cr>", "Find Files" },
o = { "<cmd>Telescope git_files<cr>", "Git Files" },
c = { "<cmd>Telescope colorscheme<cr>", "Colorscheme" },
b = { "<cmd>Telescope buffers<cr>", "Buffers" },
r = { "<cmd>Telescope oldfiles<cr>", "Recent Files" },
s = { "<cmd>Telescope current_buffer_fuzzy_find<cr>", "Search in Buffer" },
g = { "<cmd>Telescope live_grep<cr>", "Live Grep" },
d = { "<cmd>Telescope lsp_document_symbols<cr>", "Document Symbols" },
w = { "<cmd>Telescope lsp_workspace_symbols<cr>", "Workspace Symbols" },
h = { "<cmd>Telescope commands<cr>", "Commands" },
a = { "<cmd>Telescope quickfix<cr>", "Quickfix" },
p = { "<cmd>Telescope git_commits<cr>", "Git Commits" },
i = { "<cmd>Telescope git_branches<cr>", "Git Branches" },
u = { "<cmd>Telescope git_status<cr>", "Git Status" },
t = {
name = "+Todos",
r = { "<cmd>TodoTrouble<cr>", "Todos Trouble" },
t = { "<cmd>TodoTelescope<cr>", "Todos Telescope" },
},
},
}, {})
local function lsp_references()
local telescope_builtin = require("telescope.builtin")
local actions = require("telescope.actions")
telescope_builtin.lsp_references({
attach_mappings = function(_, map)
actions.select_default:replace(function(prompt_bufnr)
actions.close(prompt_bufnr) -- Close Telescope on selection
end)
return true
end,
})
end
wk.register({
g = {
name = "LSP",
n = { "<cmd>Navbuddy<CR>", "Navbuddy" },
d = { "<cmd>lua vim.lsp.buf.definition()<CR>", "Go to Definition" },
D = { "<cmd>lua vim.lsp.buf.declaration()<CR>", "Go to Declaration" },
i = { "<cmd>lua vim.lsp.buf.implementation()<CR>", "Go to Implementation" },
t = { lsp_references, "Go to References" },
a = { "<cmd>lua vim.lsp.buf.code_action()<CR>", "Code Action" },
r = { "<cmd>lua vim.lsp.buf.rename()<CR>", "Rename" },
f = { "<cmd>Neoformat<CR>", "Format" },
k = {
name = "+Diagnostics",
k = { "<cmd>lua vim.diagnostic.goto_next()<CR>", "Next Diagnostic" },
l = { "<cmd>lua vim.diagnostic.goto_prev()<CR>", "Previous Diagnostic" },
e = { "<cmd>TroubleToggle<cr>", "Toggle Trouble" },
E = { "<cmd>TroubleToggle workspace_diagnostics<cr>", "Workspace Diagnostics" },
},
l = {
name = "+Code Lens",
r = { "<cmd>lua vim.lsp.codelens.run()<CR>", "Run Code Lens" },
},
c = "Toggle Line Comment",
C = "Toggle Block Comment",
},
K = { "<cmd>lua vim.lsp.buf.hover()<CR>", "Hover" },
gk = { "<cmd>lua vim.diagnostic.open_float()<CR>", "Code Action" },
}, { mode = "n", noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-h>", "<C-w>h", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-j>", "<C-w>j", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-k>", "<C-w>k", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-l>", "<C-w>l", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-Up>", ":resize -2<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-Down>", ":resize +2<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-Left>", ":vertical resize -2<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-Right>", ":vertical resize +2<CR>", { noremap = true, silent = true })
-- Format on save
-- augroup fmt
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*",
callback = function()
vim.cmd("undojoin")
vim.cmd("Neoformat")
end,
})
-- vim.cmd([[autocmd BufWritePre * lua vim.lsp.buf.format()]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment