-- [[ NOTE: ]] Must happen before plugins are required (otherwise wrong leader will be used) vim.g.mapleader = ',' vim.g.maplocalleader = ',' -- [[ Install `lazy.nvim` plugin manager ]] local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim' if not vim.loop.fs_stat(lazypath) then vim.fn.system { 'git', 'clone', '--filter=blob:none', 'https://github.com/folke/lazy.nvim.git', '--branch=stable', -- latest stable release lazypath, } end vim.opt.rtp:prepend(lazypath) -- [[ Configure plugins ]] require('lazy').setup({ -- Git related plugins 'junegunn/vim-easy-align', -- Nice alignment 'jpalardy/vim-slime', -- Send text from buffer to a tmux pane 'neovim/nvim-lspconfig', -- LSP 'nvim-mini/mini.nvim', -- Collection of small utilities 'stevearc/conform.nvim', -- Autoformatting 'tanvirtin/monokai.nvim', -- Theme { -- Adds git related signs to the gutter, as well as utilities for managing changes 'lewis6991/gitsigns.nvim', opts = { -- See `:help gitsigns.txt` signs = { add = { text = '+' }, change = { text = '~' }, delete = { text = '_' }, topdelete = { text = '‾' }, changedelete = { text = '~' }, }, on_attach = function(bufnr) local gs = package.loaded.gitsigns local function map(mode, l, r, opts) opts = opts or {} opts.buffer = bufnr vim.keymap.set(mode, l, r, opts) end -- Navigation map({ 'n', 'v' }, ']c', function() if vim.wo.diff then return ']c' end vim.schedule(function() gs.next_hunk() end) return '' end, { expr = true, desc = 'Jump to next hunk' }) map({ 'n', 'v' }, '[c', function() if vim.wo.diff then return '[c' end vim.schedule(function() gs.prev_hunk() end) return '' end, { expr = true, desc = 'Jump to previous hunk' }) -- Actions -- visual mode map('v', 'hs', function() gs.stage_hunk { vim.fn.line '.', vim.fn.line 'v' } end, { desc = 'stage git hunk' }) map('v', 'hr', function() gs.reset_hunk { vim.fn.line '.', vim.fn.line 'v' } end, { desc = 'reset git hunk' }) -- normal mode map('n', 'hs', gs.stage_hunk, { desc = 'git stage hunk' }) map('n', 'hr', gs.reset_hunk, { desc = 'git reset hunk' }) map('n', 'hS', gs.stage_buffer, { desc = 'git Stage buffer' }) map('n', 'hu', gs.undo_stage_hunk, { desc = 'undo stage hunk' }) map('n', 'hR', gs.reset_buffer, { desc = 'git Reset buffer' }) map('n', 'hp', gs.preview_hunk, { desc = 'preview git hunk' }) map('n', 'hb', function() gs.blame_line { full = false } end, { desc = 'git blame line' }) map('n', 'hd', gs.diffthis, { desc = 'git diff against index' }) map('n', 'hD', function() gs.diffthis '~' end, { desc = 'git diff against last commit' }) -- Toggles map('n', 'tb', gs.toggle_current_line_blame, { desc = 'toggle git blame line' }) map('n', 'td', gs.toggle_deleted, { desc = 'toggle git show deleted' }) -- Text object map({ 'o', 'x' }, 'ih', ':Gitsigns select_hunk', { desc = 'select git hunk' }) end, }, }, -- Fuzzy Finder (files, lsp, etc) { 'nvim-telescope/telescope.nvim', version = '*', dependencies = { 'nvim-lua/plenary.nvim', -- optional but recommended { 'nvim-telescope/telescope-fzf-native.nvim', build = 'make' }, }, }, -- Marks management { 'chentoast/marks.nvim', event = 'VeryLazy', opts = { builtin_marks = { '<', '>', '{', '}' }, }, }, }, {}) vim.lsp.enable 'ruff_lsp' vim.lsp.enable 'gopls' vim.lsp.enable 'clangd' vim.lsp.enable 'elixirls' -- [[ Setting options ]] vim.opt.ai = true -- Autoindent vim.opt.updatetime = 50 vim.opt.mouse = '' vim.opt.ttimeoutlen = 1000 vim.opt.ttimeout = true -- vim.opt.ttimeoutlen = 0 -- Leader timeout (default 50) -- Whitespace vim.opt.tabstop = 2 vim.opt.softtabstop = 2 vim.opt.shiftwidth = 2 vim.opt.expandtab = true -- Search vim.opt.ignorecase = true vim.opt.incsearch = true vim.opt.hlsearch = false vim.opt.smartcase = true -- Display vim.opt.autocomplete = true vim.opt.nu = true vim.opt.wrap = false vim.opt.termguicolors = false vim.opt.scrolloff = 8 vim.opt.signcolumn = 'yes' vim.opt.foldlevel = 99 vim.opt.foldmethod = 'expr' vim.opt.wildmode = 'longest:full' -- swap vim.opt.swapfile = false vim.opt.backup = false vim.opt.undodir = os.getenv 'HOME' .. '/tmp/vim.undo' vim.opt.undofile = true -- Sync clipboard between OS and Neovim. -- Remove this option if you want your OS clipboard to remain independent. -- See `:help 'clipboard'` -- vim.o.clipboard = 'unnamedplus' -- Enable break indent vim.o.breakindent = true -- Keep signcolumn on by default vim.wo.signcolumn = 'yes' -- Decrease update time vim.o.updatetime = 250 vim.o.timeoutlen = 300 -- Set completeopt to have a better completion experience vim.o.completeopt = 'menuone,noselect' -- NOTE: You should make sure your terminal supports this vim.o.termguicolors = true -- [[ Basic Keymaps ]] -- Keymaps for better default experience -- See `:help vim.keymap.set()` vim.keymap.set({ 'n', 'v' }, '', '', { silent = true }) -- Remap for dealing with word wrap vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true }) vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true }) -- Diagnostic keymaps vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' }) vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' }) vim.keymap.set('n', 'e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' }) vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' }) vim.keymap.set('n', ',', ':w!') vim.keymap.set('n', 'qq', ':qa!') vim.keymap.set('n', "'", vim.cmd.tabn) vim.keymap.set('n', ';', vim.cmd.tabp) vim.keymap.set('n', '', ':tabe ') vim.keymap.set('n', '', vim.diagnostic.goto_next) vim.keymap.set('n', '', vim.diagnostic.goto_prev) vim.keymap.set('n', 'r', ':%s/\\s*$//') -- Moving code!! -- vim.keymap.set('v', 'J', ":m '>+1gv=gv") -- vim.keymap.set('v', 'K', ":m '<-2gv=gv") -- vim.keymap.set('x', 'o', '"_dP') -- System copy/paste vim.keymap.set('n', 'y', '"+y') vim.keymap.set('v', 'y', '"+y') vim.keymap.set('n', 'y', '"+Y') vim.keymap.set('n', 'Y', ':.w! ~/.vimpaste') vim.keymap.set('v', 'Y', ':w! ~/.vimpaste') vim.keymap.set('n', 'P', ':r ~/.vimpaste') vim.api.nvim_create_autocmd('FileType', { pattern = { 'markdown' }, callback = function(_) vim.keymap.set('n', 'f', '{!}fmt -p 150') vim.keymap.set('v', 'f', ':!fmt -p 150') end, }) require('monokai').setup { palette = require('monokai').ristretto } -- [[ Configure Telescope ]] -- See `:help telescope` and `:help telescope.setup()` require('telescope').setup { defaults = { mappings = { i = { [''] = false, [''] = false, }, }, }, } require('mini.icons').setup {} require('mini.completion').setup {} -- Enable telescope fzf native, if installed pcall(require('telescope').load_extension, 'fzf') -- Formatting require('conform').setup { formatters_by_ft = { elixir = { 'mix' }, heex = { 'mix' }, go = { 'gofmt' }, javascript = { 'prettier' }, lua = { 'stylua' }, python = { 'black' }, rust = { 'rustfmt', lsp_format = 'fallback' }, cpp = { 'clang-format' }, }, format_on_save = { -- These options will be passed to conform.format() timeout_ms = 500, lsp_format = 'fallback', }, } -- Telescope live_grep in git root -- Function to find the git root directory based on the current buffer's path local function find_git_root() -- Use the current buffer's path as the starting point for the git search local current_file = vim.api.nvim_buf_get_name(0) local current_dir local cwd = vim.fn.getcwd() -- If the buffer is not associated with a file, return nil if current_file == '' then current_dir = cwd else -- Extract the directory from the current file's path current_dir = vim.fn.fnamemodify(current_file, ':h') end -- Find the Git root directory from the current file's path local git_root = vim.fn.systemlist('git -C ' .. vim.fn.escape(current_dir, ' ') .. ' rev-parse --show-toplevel')[1] if vim.v.shell_error ~= 0 then print 'Not a git repository. Searching on current working directory' return cwd end return git_root end -- Custom live_grep function to search in git root local function live_grep_git_root() local git_root = find_git_root() if git_root then require('telescope.builtin').live_grep { search_dirs = { git_root }, } end end vim.api.nvim_create_user_command('LiveGrepGitRoot', live_grep_git_root, {}) -- See `:help telescope.builtin` vim.keymap.set('n', '?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' }) vim.keymap.set('n', '', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' }) vim.keymap.set('n', '/', function() -- You can pass additional configuration to telescope to change theme, layout, etc. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown { winblend = 10, previewer = false, }) end, { desc = '[/] Fuzzily search in current buffer' }) local function telescope_live_grep_open_files() require('telescope.builtin').live_grep { grep_open_files = true, prompt_title = 'Live Grep in Open Files', } end vim.keymap.set('n', 's/', telescope_live_grep_open_files, { desc = '[S]earch [/] in Open Files' }) vim.keymap.set('n', 'ss', require('telescope.builtin').builtin, { desc = '[S]earch [S]elect Telescope' }) vim.keymap.set('n', 'gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' }) vim.keymap.set('n', 'sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' }) vim.keymap.set('n', 'sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' }) vim.keymap.set('n', 'sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' }) vim.keymap.set('n', 'sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' }) vim.keymap.set('n', 'sG', ':LiveGrepGitRoot', { desc = '[S]earch by [G]rep on Git Root' }) vim.keymap.set('n', 'sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' }) vim.keymap.set('n', 'sr', require('telescope.builtin').resume, { desc = '[S]earch [R]esume' }) -- This function gets run when an LSP connects to a particular buffer. local on_attach = function(_, bufnr) -- NOTE: Remember that lua is a real programming language, and as such it is possible -- to define small helper and utility functions so you don't have to repeat yourself -- many times. -- -- In this case, we create a function that lets us more easily define mappings specific -- for LSP related items. It sets the mode, buffer and description for us each time. local nmap = function(keys, func, desc) if desc then desc = 'LSP: ' .. desc end vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) end nmap('rn', vim.lsp.buf.rename, '[R]e[n]ame') nmap('ca', function() vim.lsp.buf.code_action { context = { only = { 'quickfix', 'refactor', 'source' } } } end, '[C]ode [A]ction') nmap('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition') nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences') nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation') nmap('D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition') nmap('ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols') nmap('ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols') -- See `:help K` for why this keymap nmap('K', vim.lsp.buf.hover, 'Hover Documentation') nmap('', vim.lsp.buf.signature_help, 'Signature Documentation') -- Lesser used LSP functionality nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') nmap('wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') nmap('wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') nmap('wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, '[W]orkspace [L]ist Folders') -- Create a command `:Format` local to the LSP buffer vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) vim.lsp.buf.format() end, { desc = 'Format current buffer with LSP' }) end vim.g.slime_target = 'tmux' vim.keymap.set('n', '', 'SlimeLineSend') vim.keymap.set('v', '', "SlimeRegionSend'>") vim.keymap.set('n', 'v', ':SlimeConfig') vim.api.nvim_create_autocmd('FileType', { pattern = 'python', callback = function() vim.g.slime_python_ipython = 1 end, }) vim.api.nvim_create_autocmd('FileType', { pattern = '*', callback = function() vim.treesitter.start() end, }) vim.keymap.set('n', 'ga', ':EasyAlign') vim.keymap.set('v', 'ga', ':EasyAlign') local builtin = require 'telescope.builtin' vim.keymap.set('n', 'ff', builtin.find_files, {}) vim.keymap.set('n', '', builtin.git_files, {}) vim.keymap.set('n', 'ps', function() builtin.grep_string { search = vim.fn.input 'Grep > ' } end) -- Define a function to highlight the current search term local highlight_search_term = function(label) local search_term = vim.fn.getreg '/' if search_term ~= '' then -- local matches = vim.fn.matchadd(label, search_term) -- for match_id in matches do -- vim.api.nvim_buf_add_highlight(0, -1, label, 0, match_id[1] - 1, match_id[2]) -- end end end vim.keymap.set('n', 'm1', function() highlight_search_term 'Matchadd_1' end) vim.keymap.set('n', 'm2', function() highlight_search_term 'Matchadd_2' end) vim.keymap.set('n', 'm3', function() highlight_search_term 'Matchadd_3' end) vim.keymap.set('n', 'm4', function() highlight_search_term 'Matchadd_4' end) vim.keymap.set('n', 'm5', function() highlight_search_term 'Matchadd_5' end) vim.keymap.set('n', 'mc', function() vim.fn.clearmatches() end) local colors = { base03 = '#002b36', base02 = '#073642', base01 = '#586e75', base00 = '#657b83', base0 = '#839496', base1 = '#93a1a1', base2 = '#eee8d5', base3 = '#fdf6e3', yellow = '#b58900', orange = '#cb4b16', red = '#dc322f', magenta = '#d33682', violet = '#6c71c4', blue = '#268bd2', cyan = '#2aa198', green = '#859900', } vim.api.nvim_set_hl(0, 'Matchadd_1', { bg = colors.blue, fg = 0 }) vim.api.nvim_set_hl(0, 'Matchadd_2', { bg = colors.violet, fg = 0 }) vim.api.nvim_set_hl(0, 'Matchadd_3', { bg = colors.cyan, fg = 0 }) vim.api.nvim_set_hl(0, 'Matchadd_4', { bg = colors.red, fg = 0 }) vim.api.nvim_set_hl(0, 'Matchadd_5', { bg = colors.orange, fg = 0 }) vim.api.nvim_set_hl(0, 'Matchadd_6', { bg = colors.yellow, fg = 0 }) -- Because nvim filetypes are stupid ... need to disable formatoptions for all file types vim.api.nvim_create_autocmd('FileType', { pattern = '*', callback = function() vim.opt.formatoptions:remove 'r' vim.opt.formatoptions:remove 'o' end, }) -- Disable change-detection if in diff mode. Allows for diffs involving redirects -- eg nvim -d <(cmd 1) <(cmd 2) if vim.diff then vim.opt.autoread = false end