repos / dotfiles

my dotfiles

dotfiles / dot_config / nvim / lua
Eric Bower  ·  2025-07-07

init.lua

  1local opt = vim.opt
  2opt.tabstop        = 2
  3opt.shiftwidth     = 2
  4opt.softtabstop    = 2
  5opt.expandtab      = true
  6opt.wrap           = false
  7opt.autoread       = true
  8opt.list           = true
  9opt.signcolumn     = 'yes'
 10opt.backspace      = 'indent,eol,start'
 11opt.shell          = '/bin/bash'
 12opt.colorcolumn    = '80'
 13opt.completeopt    = { "menuone", "noselect", "popup" }
 14opt.laststatus     = 0
 15opt.winborder      = 'rounded'
 16-- opt.number         = true
 17
 18vim.g.mapleader      = ','
 19vim.g.maplocalleader = ','
 20
 21local opts = { noremap=true, silent=true }
 22local set_keymap = vim.keymap.set
 23set_keymap('n', '<leader>b', '<C-O>', opts)          -- jump to prev visited loc
 24set_keymap('n', 'Q', '<nop>', opts)                  -- disable 'Q'
 25set_keymap('n', '<leader>w', ':!make fmt<CR>', opts) -- code format command
 26set_keymap('t', '<Esc>', [[<C-\><C-n>]], opts)       -- exit terminal mode with Esc
 27
 28local autocmd = vim.api.nvim_create_autocmd
 29
 30autocmd('FileType', {
 31  group = vim.api.nvim_create_augroup('close_with_q', { clear = true }),
 32  pattern = { 'help', 'man', 'qf', 'scratch' },
 33  callback = function(args)
 34    vim.keymap.set('n', 'q', '<cmd>quit<cr>', { buffer = args.buf })
 35  end,
 36})
 37
 38autocmd('Filetype', {
 39  pattern = { 'make' },
 40  command = 'setlocal tabstop=4 shiftwidth=4 softtabstop=4',
 41})
 42
 43autocmd('Filetype', {
 44  pattern = { 'qf' },
 45  command = 'setlocal wrap',
 46})
 47
 48autocmd('Filetype', {
 49  pattern = { 'markdown' },
 50  command = 'setlocal wrap tw=79 formatoptions+=t tabstop=2 shiftwidth=2 softtabstop=2',
 51})
 52
 53local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
 54if not (vim.uv or vim.loop).fs_stat(lazypath) then
 55  local lazyrepo = "https://github.com/folke/lazy.nvim.git"
 56  local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
 57  if vim.v.shell_error ~= 0 then
 58    vim.api.nvim_echo({
 59      { "Failed to clone lazy.nvim:\n", "ErrorMsg" },
 60      { out, "WarningMsg" },
 61      { "\nPress any key to exit..." },
 62    }, true, {})
 63    vim.fn.getchar()
 64    os.exit(1)
 65  end
 66end
 67vim.opt.rtp:prepend(lazypath)
 68
 69function setup_treesitter()
 70  require'nvim-treesitter.configs'.setup {
 71    highlight = {
 72      enable = true,
 73    },
 74    indent = {
 75      enable = true
 76    },
 77    ensure_installed = {
 78      "bash",
 79      "c",
 80      "cmake",
 81      "dockerfile",
 82      "fish",
 83      "git_config",
 84      "git_rebase",
 85      "gitattributes",
 86      "gitcommit",
 87      "gitignore",
 88      "go",
 89      "gomod",
 90      "gosum",
 91      "html",
 92      "javascript",
 93      "json",
 94      "json5",
 95      "lua",
 96      "make",
 97      "markdown",
 98      "python",
 99      "r",
100      "rust",
101      "sql",
102      "terraform",
103      "toml",
104      "tsx",
105      "typescript",
106      "typst",
107      "vim",
108      "vimdoc",
109      "yaml",
110      "zig",
111    },
112  }
113end
114
115function setup_lsp()
116  local opts = { noremap=true, silent=true }
117  vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float, opts)
118  vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, opts)
119  -- Use an on_attach function to only map the following keys
120  -- after the language server attaches to the current buffer
121  local on_attach = function(client, bufnr)
122    vim.lsp.completion.enable(true, client.id, bufnr, {
123      autotrigger = true,
124      convert = function(item)
125        return { abbr = item.label:gsub('%b()', '') }
126      end,
127    })
128    local bufopts = { noremap=true, silent=true, buffer=bufnr }
129
130    -- See `:help vim.lsp.*` for documentation on any of the below functions
131    vim.keymap.set('n', '<leader>d', vim.lsp.buf.definition, bufopts)
132    vim.keymap.set('n', '<leader>D', vim.lsp.buf.type_definition, bufopts)
133    vim.keymap.set('n', '<leader>h', vim.lsp.buf.hover, bufopts)
134    vim.keymap.set('n', '<leader>r', vim.lsp.buf.references, bufopts)
135    vim.keymap.set('n', '<leader>i', vim.lsp.buf.implementation, bufopts)
136    vim.keymap.set('i', '<c-k>', vim.lsp.completion.get, bufopts)
137  end
138
139  local nvim_lsp = require('lspconfig')
140
141  function handler(server_name)
142    local opts = {
143      capabilities = capabilities,
144      on_attach = on_attach,
145    }
146
147    if server_name == "denols" then
148      opts.root_dir = nvim_lsp.util.root_pattern("deno.json", "deno.jsonc")
149    elseif server_name == "ts_ls" then
150      opts.root_dir = nvim_lsp.util.root_pattern("package.json")
151      -- https://github.com/neovim/nvim-lspconfig/issues/2507#issuecomment-1471438640
152      opts.single_file_support = false
153    end
154
155    nvim_lsp[server_name].setup(opts)
156  end
157
158  -- npm i -g vscode-langservers-extracted
159  handler("cssls")
160  handler("denols")
161  handler("gopls")
162  handler("html")
163  handler("jsonls")
164  -- npm i -g pyright
165  handler("pyright")
166  -- npm i -g typescript typescript-language-server
167  handler("ts_ls")
168  handler("zls")
169end
170
171require("lazy").setup({
172  rocks = {
173    enabled = false,
174  },
175  performance = {
176    rtp = {
177      disabled_plugins = {
178        "gzip", "matchit", "matchparen", "netrwPlugin",
179        "tarPlugin", "tohtml", "tutor", "zipPlugin",
180      },
181    },
182  },
183  spec = {
184    {
185      'neovim/nvim-lspconfig',
186      config = setup_lsp,
187    },
188    {
189      'nvim-treesitter/nvim-treesitter',
190      build = ':TSUpdate',
191      config = setup_treesitter
192    },
193    {
194      'ruifm/gitlinker.nvim',
195      dependencies = 'nvim-lua/plenary.nvim',
196      config = function()
197        require"gitlinker".setup()
198      end,
199    },
200    {
201      'Mofiqul/dracula.nvim',
202      lazy = false,
203      priority = 1000,
204      config = function()
205        require('dracula').setup({})
206        vim.cmd[[colorscheme dracula]]
207      end
208    },
209    {
210      "ibhagwan/fzf-lua",
211      config = function()
212        local fzf = require('fzf-lua')
213        local fzfFiles = function()
214          fzf.files({
215            winopts = {
216              preview = { hidden = "hidden" },
217            },
218          })
219        end
220        vim.keymap.set("n", "<leader>s", fzfFiles, { desc = "Fzf Files", noremap=true, silent=true })
221        vim.keymap.set("n", "<leader>f", fzf.buffers, { desc = "Fzf Buffers", noremap=true, silent=true })
222        vim.keymap.set("n", "<leader>S", fzf.live_grep, { desc = "Fzf Grep", noremap=true, silent=true })
223        fzf.setup({'max-perf'})
224      end
225    },
226    {
227      "christoomey/vim-tmux-navigator",
228      cmd = { "TmuxNavigateLeft", "TmuxNavigateDown", "TmuxNavigateUp", "TmuxNavigateRight" },
229      keys = {
230        { "<c-h>", "<cmd><C-U>TmuxNavigateLeft<cr>" },
231        { "<c-j>", "<cmd><C-U>TmuxNavigateDown<cr>" },
232        { "<c-k>", "<cmd><C-U>TmuxNavigateUp<cr>" },
233        { "<c-l>", "<cmd><C-U>TmuxNavigateRight<cr>" },
234      },
235    },
236  },
237})