Skip to content

Instantly share code, notes, and snippets.

@spy16
Created August 2, 2024 17:23
Show Gist options
  • Save spy16/ef1004b43d38f20da9d8e1956beca52d to your computer and use it in GitHub Desktop.
Save spy16/ef1004b43d38f20da9d8e1956beca52d to your computer and use it in GitHub Desktop.
vim.g.mapleader = ","
vim.g.maplocalleader = ","
vim.g.have_nerd_font = true
-- ======================================================================
-- ========================== Basic Options =============================
-- ======================================================================
-- Set leader key.
vim.opt.updatetime = 250
vim.opt.undofile = true
vim.opt.signcolumn = "yes"
vim.opt.number = true
vim.opt.mouse = "a"
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.hlsearch = true
vim.opt.wrap = true
vim.opt.breakindent = true
vim.opt.tabstop = 2
vim.opt.shiftwidth = 2
vim.opt.expandtab = true
vim.opt.showmode = true
vim.opt.clipboard = "unnamedplus" -- Use system clipboard
vim.opt.splitright = true
vim.opt.splitbelow = true
vim.opt.list = true
vim.opt.listchars = { tab = "» ", trail = "·", nbsp = "␣" }
vim.opt.inccommand = "split"
vim.opt.cursorline = true
vim.opt.scrolloff = 10
vim.api.nvim_set_keymap("i", "<C-k>", "<Up>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("i", "<C-l>", "<Right>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("i", "<C-j>", "<Down>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("i", "<C-h>", "<Left>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<Up>", "<nop>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<Up>", "<nop>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<Down>", "<nop>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<Left>", "<nop>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<Right>", "<nop>", { noremap = true, silent = true })
-- ====================================================================
-- ============================ Plugins ===============================
-- ====================================================================
-- [[ Install `lazy.nvim` plugin manager ]]
-- See `:help lazy.nvim.txt` or https://github.com/folke/lazy.nvim for more info
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
end
---@diagnostic disable-next-line: undefined-field
vim.opt.rtp:prepend(lazypath)
require("lazy").setup({
{ "shortcuts/no-neck-pain.nvim" },
{ "numToStr/Comment.nvim", opts = {} },
{ "MunifTanjim/nui.nvim", lazy = true },
{
"kylechui/nvim-surround",
version = "*", -- Use for stability; omit to use `main` branch for the latest features
event = "VeryLazy",
config = function()
require("nvim-surround").setup({
-- Configuration here, or leave empty to use defaults
})
end,
},
{
"projekt0n/github-nvim-theme",
lazy = false, -- make sure we load this during startup if it is your main colorscheme
priority = 1000, -- make sure to load this before all the other start plugins
config = function()
vim.cmd("colorscheme github_dark_default")
end,
},
{
"stevearc/dressing.nvim",
opts = {},
},
{ "akinsho/toggleterm.nvim", version = "*", config = true },
{
"nvim-neo-tree/neo-tree.nvim",
branch = "v3.x",
cmd = "Neotree",
config = function()
require("neo-tree").setup({
close_if_last_window = true,
enable_git_status = true,
filesystem = {
follow_current_file = {
enabled = true,
leave_dirs_opsn = true,
},
},
buffers = {
follow_current_file = {
enabled = true,
leave_dirs_opsn = false,
},
window = {
width = 30,
},
},
})
end,
},
{ -- Status line bar
"nvim-lualine/lualine.nvim",
opts = {},
},
{ -- Highlight todo, notes, etc in comments
"folke/todo-comments.nvim",
event = "VimEnter",
dependencies = { "nvim-lua/plenary.nvim" },
opts = { signs = false },
},
{ -- Keymap suggestion tooltip
"folke/which-key.nvim",
event = "VimEnter",
config = function()
require("which-key").setup()
end,
},
{
"lewis6991/gitsigns.nvim",
opts = {
signs = {
add = { text = "+" },
change = { text = "~" },
delete = { text = "_" },
topdelete = { text = "β€Ύ" },
changedelete = { text = "~" },
},
},
},
{
"nvim-telescope/telescope.nvim",
event = "VimEnter",
dependencies = {
{ "nvim-lua/plenary.nvim" },
{ "nvim-telescope/telescope-ui-select.nvim" },
{ "nvim-tree/nvim-web-devicons", enabled = vim.g.have_nerd_font },
},
config = function()
local telescope = require("telescope")
local actions = require("telescope.actions")
telescope.setup({
defaults = {
sorting_strategy = "ascending",
layout_strategy = "horizontal",
layout_config = { prompt_position = "top" },
mappings = {
i = {
["<C-k>"] = actions.move_selection_previous, -- move to prev result
["<C-j>"] = actions.move_selection_next, -- move to next result
["<C-q>"] = actions.send_selected_to_qflist + actions.open_qflist, -- send selected to quickfixlist
["<esc>"] = actions.close,
["C-c"] = actions.close,
},
},
},
extensions = {
["ui-select"] = {
require("telescope.themes").get_dropdown(),
},
file_browser = {
path = "%:p:h", -- open from within the folder of your current buffer
display_stat = false, -- don't show file stat
grouped = true, -- group initial sorting by directories and then files
hidden = true, -- show hidden files
hide_parent_dir = true, -- hide `../` in the file browser
hijack_netrw = true, -- use telescope file browser when opening directory paths
prompt_path = true, -- show the current relative path from cwd as the prompt prefix
use_fd = true, -- use `fd` instead of plenary, make sure to install `fd`
},
},
})
pcall(telescope.load_extension, "ui-select")
end,
},
{ -- Autocompletion
"hrsh7th/nvim-cmp",
event = "InsertEnter",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-path",
},
config = function()
local cmp = require("cmp")
cmp.setup({
completion = { completeopt = "menu,menuone,noinsert" },
mapping = cmp.mapping.preset.insert({
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping.select_next_item(),
["<S-Tab>"] = cmp.mapping.select_prev_item(),
-- Scroll the documentation window [b]ack / [f]orward
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
}),
sources = {
{ name = "nvim_lsp" },
{ name = "path" },
},
})
end,
},
{ -- Autoformat
"stevearc/conform.nvim",
lazy = false,
keys = {
{
"<leader>rf",
function()
require("conform").format({ async = true, lsp_fallback = true })
end,
mode = "",
desc = "[F]ormat buffer",
},
},
opts = {
notify_on_error = false,
format_on_save = function(bufnr)
-- Disable "format_on_save lsp_fallback" for languages that don't
-- have a well standardized coding style. You can add additional
-- languages here or re-enable it for the disabled ones.
local disable_filetypes = { c = true, cpp = true }
return {
timeout_ms = 500,
lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
}
end,
formatters_by_ft = {
lua = { "stylua" },
-- Conform can also run multiple formatters sequentially
-- python = { "isort", "black" },
--
-- You can use a sub-list to tell conform to run *until* a formatter
-- is found.
-- javascript = { { "prettierd", "prettier" } },
},
},
},
{
"neovim/nvim-lspconfig",
dependencies = {
-- Automatically install LSPs and related tools to stdpath for Neovim
{ "williamboman/mason.nvim", config = true }, -- NOTE: Must be loaded before dependants
"williamboman/mason-lspconfig.nvim",
"WhoIsSethDaniel/mason-tool-installer.nvim",
-- Useful status updates for LSP.
-- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
{ "j-hui/fidget.nvim", opts = {} },
-- `neodev` configures Lua LSP for your Neovim config, runtime and plugins
-- used for completion, annotations and signatures of Neovim apis
{ "folke/neodev.nvim", opts = {} },
},
config = function()
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("kickstart-lsp-attach", { clear = true }),
callback = function(event)
local map = function(keys, func, desc)
vim.keymap.set("n", keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
end
map("gd", require("telescope.builtin").lsp_definitions, "[G]oto [D]efinition")
map("gr", require("telescope.builtin").lsp_references, "[G]oto [R]eferences")
map("gI", require("telescope.builtin").lsp_implementations, "[G]oto [I]mplementation")
map("<leader>D", require("telescope.builtin").lsp_type_definitions, "Type [D]efinition")
map(
"<C-s>",
require("telescope.builtin").lsp_dynamic_workspace_symbols,
"[F]ind Workspace [S]ymbol"
)
map("<leader>rn", vim.lsp.buf.rename, "Refactor: [n]ame")
-- Execute a code action, usually your cursor needs to be on top of an error
-- or a suggestion from your LSP for this to activate.
map("<leader>ca", vim.lsp.buf.code_action, "[C]ode [A]ction")
-- Opens a popup that displays documentation about the word under your cursor
-- See `:help K` for why this keymap.
map("K", vim.lsp.buf.hover, "Hover Documentation")
-- The following two autocommands are used to highlight references of the
-- word under your cursor when your cursor rests there for a little while.
-- See `:help CursorHold` for information about when this is executed
--
-- When you move your cursor, the highlights will be cleared (the second autocommand).
local client = vim.lsp.get_client_by_id(event.data.client_id)
if client and client.server_capabilities.documentHighlightProvider then
local highlight_augroup =
vim.api.nvim_create_augroup("kickstart-lsp-highlight", { clear = false })
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
buffer = event.buf,
group = highlight_augroup,
callback = vim.lsp.buf.document_highlight,
})
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
buffer = event.buf,
group = highlight_augroup,
callback = vim.lsp.buf.clear_references,
})
vim.api.nvim_create_autocmd("LspDetach", {
group = vim.api.nvim_create_augroup("kickstart-lsp-detach", { clear = true }),
callback = function(event2)
vim.lsp.buf.clear_references()
vim.api.nvim_clear_autocmds({ group = "kickstart-lsp-highlight", buffer = event2.buf })
end,
})
end
-- The following autocommand is used to enable inlay hints in your
-- code, if the language server you are using supports them
--
-- This may be unwanted, since they displace some of your code
if client and client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
map("<leader>th", function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({}))
end, "[T]oggle Inlay [H]ints")
end
end,
})
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = vim.tbl_deep_extend("force", capabilities, require("cmp_nvim_lsp").default_capabilities())
local servers = {
terraformls = {},
gopls = {},
rust_analyzer = {},
lua_ls = {
-- cmd = {...},
-- filetypes = { ...},
-- capabilities = {},
settings = {
Lua = {
completion = {
callSnippet = "Replace",
},
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
-- diagnostics = { disable = { 'missing-fields' } },
},
},
},
}
require("mason").setup()
local ensure_installed = vim.tbl_keys(servers or {})
vim.list_extend(ensure_installed, {
"stylua", -- Used to format Lua code
})
require("mason-tool-installer").setup({ ensure_installed = ensure_installed })
require("mason-lspconfig").setup({
handlers = {
function(server_name)
local server = servers[server_name] or {}
-- This handles overriding only values explicitly passed
-- by the server configuration above. Useful when disabling
-- certain features of an LSP (for example, turning off formatting for tsserver)
server.capabilities = vim.tbl_deep_extend("force", {}, capabilities, server.capabilities or {})
require("lspconfig")[server_name].setup(server)
end,
},
})
end,
},
{
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
lazy = vim.fn.argc(-1) == 0,
init = function(plugin)
-- PERF: add nvim-treesitter queries to the rtp and it's custom query predicates early
-- This is needed because a bunch of plugins no longer `require("nvim-treesitter")`, which
-- no longer trigger the **nvim-treesitter** module to be loaded in time.
-- Luckily, the only things that those plugins need are the custom queries, which we make available
-- during startup.
require("lazy.core.loader").add_to_rtp(plugin)
require("nvim-treesitter.query_predicates")
end,
cmd = { "TSUpdateSync", "TSUpdate", "TSInstall" },
opts = {
ensure_installed = {
"bash",
"diff",
"html",
"lua",
"markdown",
"vim",
"json",
"lua",
"yaml",
"go",
"gowork",
"gomod",
"svelte",
},
auto_install = true,
indent = { enable = true },
highlight = { enabled = true },
incremental_selection = {
enabled = false,
},
textobjects = {
enable = true,
select = {
enable = true,
lookahead = true,
},
move = {
enabled = true,
},
},
},
config = function(_, opts)
require("nvim-treesitter.install").prefer_git = true
require("nvim-treesitter.configs").setup(opts)
end,
keys = {
{ "<c-space>", desc = "Increment Selection" },
{ "<bs>", desc = "Decrement Selection", mode = "x" },
},
},
{
"nvim-treesitter/nvim-treesitter-textobjects",
enabled = true,
},
{
"ray-x/go.nvim",
dependencies = { -- optional packages
"ray-x/guihua.lua",
"neovim/nvim-lspconfig",
"nvim-treesitter/nvim-treesitter",
},
config = function()
require("go").setup()
end,
event = { "CmdlineEnter" },
ft = { "go", "gomod" },
build = ':lua require("go.install").update_all_sync()',
},
{
"windwp/nvim-autopairs",
event = "InsertEnter",
-- Optional dependency
dependencies = { "hrsh7th/nvim-cmp" },
config = function()
require("nvim-autopairs").setup({})
-- If you want to automatically add `(` after selecting a function or method
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
local cmp = require("cmp")
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
end,
},
}, {
ui = {
-- If you are using a Nerd Font: set icons to an empty table which will use the
-- default lazy.nvim defined Nerd Font icons, otherwise define a unicode icons table
icons = vim.g.have_nerd_font and {} or {
cmd = "⌘",
config = "πŸ› ",
event = "πŸ“…",
ft = "πŸ“‚",
init = "βš™",
keys = "πŸ—",
plugin = "πŸ”Œ",
runtime = "πŸ’»",
require = "πŸŒ™",
source = "πŸ“„",
start = "πŸš€",
task = "πŸ“Œ",
lazy = "πŸ’€ ",
},
},
})
-- ====================================================================
-- =========================== Auto Commands ==========================
-- ====================================================================
-- Highlight when yanking (copying) text
-- Try it with `yap` in normal mode
-- See `:help vim.highlight.on_yank()`
vim.api.nvim_create_autocmd("TextYankPost", {
desc = "Highlight when yanking (copying) text",
group = vim.api.nvim_create_augroup("kickstart-highlight-yank", { clear = true }),
callback = function()
vim.highlight.on_yank()
end,
})
-- Format-on-save for Go files using goimports.
local format_sync_grp = vim.api.nvim_create_augroup("goimports", {})
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.go",
callback = function()
require("go.format").goimports()
end,
group = format_sync_grp,
})
-- ====================================================================
-- ====================== Basic Key bindings ==========================
-- ====================================================================
vim.keymap.set("n", "<leader><cr>", "<cmd>nohlsearch<CR>")
vim.keymap.set("n", "<leader>bd", "<cmd>bdelete<CR>")
vim.keymap.set("n", "<leader>gt", "<cmd>GoAddTag<CR>")
vim.keymap.set("n", "<leader>fe", "<cmd>Neotree<CR>")
vim.keymap.set("n", "<leader>gT", "<cmd>GoTestPkg<CR>")
vim.keymap.set("n", "gD", "<cmd>GoImplements<CR>")
-- Telescope related key bindings.
local ts_builtin = require("telescope.builtin")
vim.keymap.set("n", "<D-S-p>", ts_builtin.commands, { desc = "[F]ind using [G]rep" })
vim.keymap.set("n", "<C-f>", ts_builtin.live_grep, { desc = "[F]ind using [G]rep" })
vim.keymap.set("n", "<leader>ff", ts_builtin.find_files, { desc = "[F]ind: [F]iles" })
vim.keymap.set("n", "<leader>bf", ts_builtin.buffers, { desc = "[F]ind: [B]uffers" })
vim.keymap.set("n", "<C-d>", ts_builtin.diagnostics, { desc = "[F]ind: [d]iagnostics" })
vim.keymap.set("n", "<leader>fr", ts_builtin.oldfiles, { desc = "[F]ind: [r]ecent files" })
vim.keymap.set("n", "/", function()
-- You can pass additional configuration to Telescope to change the theme, layout, etc.
ts_builtin.current_buffer_fuzzy_find(require("telescope.themes").get_dropdown({
winblend = 3,
previewer = false,
}))
end, { desc = "[/] Fuzzily search in current buffer" })
local format_sync_grp = vim.api.nvim_create_augroup("goimports", {})
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.go",
callback = function()
require("go.format").goimports()
end,
group = format_sync_grp,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment