|
| 1 | +-- [[ Basic Keymaps ]] |
| 2 | +-- See `:help vim.keymap.set()` |
| 3 | + |
| 4 | +-- Clear highlights on search when pressing <Esc> in normal mode |
| 5 | +-- See `:help hlsearch` |
| 6 | +vim.keymap.set('n', '<Esc>', '<cmd>nohlsearch<CR>') |
| 7 | + |
| 8 | +-- Diagnostic keymaps |
| 9 | +vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostic [Q]uickfix list' }) |
| 10 | + |
| 11 | +-- Exit terminal mode in the builtin terminal with a shortcut that is a bit easier |
| 12 | +-- for people to discover. Otherwise, you normally need to press <C-\><C-n>, which |
| 13 | +-- is not what someone will guess without a bit more experience. |
| 14 | +-- |
| 15 | +-- NOTE: This won't work in all terminal emulators/tmux/etc. Try your own mapping |
| 16 | +-- or just use <C-\><C-n> to exit terminal mode |
| 17 | +vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' }) |
| 18 | + |
| 19 | +-- Keybinds to make split navigation easier. |
| 20 | +-- Use CTRL+<hjkl> to switch between windows |
| 21 | +-- |
| 22 | +-- See `:help wincmd` for a list of all window commands |
| 23 | +vim.keymap.set('n', '<C-h>', '<C-w><C-h>', { desc = 'Move focus to the left window' }) |
| 24 | +vim.keymap.set('n', '<C-l>', '<C-w><C-l>', { desc = 'Move focus to the right window' }) |
| 25 | +vim.keymap.set('n', '<C-j>', '<C-w><C-j>', { desc = 'Move focus to the lower window' }) |
| 26 | +vim.keymap.set('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper window' }) |
| 27 | + |
| 28 | +-- [[ Basic Autocommands ]] |
| 29 | +-- See `:help lua-guide-autocommands` |
| 30 | + |
| 31 | +-- Highlight when yanking (copying) text |
| 32 | +-- Try it with `yap` in normal mode |
| 33 | +-- See `:help vim.highlight.on_yank()` |
| 34 | +vim.api.nvim_create_autocmd('TextYankPost', { |
| 35 | + desc = 'Highlight when yanking (copying) text', |
| 36 | + group = vim.api.nvim_create_augroup('kickstart-highlight-yank', { clear = true }), |
| 37 | + callback = function() |
| 38 | + vim.highlight.on_yank() |
| 39 | + end, |
| 40 | +}) |
| 41 | + |
| 42 | +-- Disables virtual_text and turns virtual_line on |
| 43 | +-- when there is a diagnostic on the current line |
| 44 | +-- Taken from : |
| 45 | +-- https://www.reddit.com/r/neovim/comments/1jpbc7s/disable_virtual_text_if_there_is_diagnostic_in/ |
| 46 | +local og_virt_text |
| 47 | +local og_virt_line |
| 48 | +vim.api.nvim_create_autocmd({ 'CursorMoved', 'DiagnosticChanged' }, { |
| 49 | + group = vim.api.nvim_create_augroup('diagnostic_only_virtlines', {}), |
| 50 | + callback = function() |
| 51 | + if og_virt_line == nil then |
| 52 | + og_virt_line = vim.diagnostic.config().virtual_lines |
| 53 | + end |
| 54 | + |
| 55 | + -- ignore if virtual_lines.current_line is disabled |
| 56 | + if not (og_virt_line and og_virt_line.current_line) then |
| 57 | + if og_virt_text then |
| 58 | + vim.diagnostic.config { virtual_text = og_virt_text } |
| 59 | + og_virt_text = nil |
| 60 | + end |
| 61 | + return |
| 62 | + end |
| 63 | + |
| 64 | + if og_virt_text == nil then |
| 65 | + og_virt_text = vim.diagnostic.config().virtual_text |
| 66 | + end |
| 67 | + |
| 68 | + local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1 |
| 69 | + |
| 70 | + if vim.tbl_isempty(vim.diagnostic.get(0, { lnum = lnum })) then |
| 71 | + vim.diagnostic.config { virtual_text = og_virt_text } |
| 72 | + else |
| 73 | + vim.diagnostic.config { virtual_text = false } |
| 74 | + end |
| 75 | + end, |
| 76 | +}) |
| 77 | + |
| 78 | +-- immediately redraw the diagnostics when the mode changes |
| 79 | +-- Taken from : |
| 80 | +-- https://www.reddit.com/r/neovim/comments/1jpbc7s/disable_virtual_text_if_there_is_diagnostic_in/ |
| 81 | +vim.api.nvim_create_autocmd('ModeChanged', { |
| 82 | + group = vim.api.nvim_create_augroup('diagnostic_redraw', {}), |
| 83 | + callback = function() |
| 84 | + pcall(vim.diagnostic.show) |
| 85 | + end, |
| 86 | +}) |
| 87 | + |
| 88 | +-- Auto import for gopls |
| 89 | +vim.api.nvim_create_autocmd('BufWritePre', { |
| 90 | + pattern = '*.go', |
| 91 | + callback = function() |
| 92 | + local params = vim.lsp.util.make_range_params() |
| 93 | + params.context = { only = { 'source.organizeImports' } } |
| 94 | + -- buf_request_sync defaults to a 1000ms timeout. Depending on your |
| 95 | + -- machine and codebase, you may want longer. Add an additional |
| 96 | + -- argument after params if you find that you have to write the file |
| 97 | + -- twice for changes to be saved. |
| 98 | + -- E.g., vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000) |
| 99 | + local result = vim.lsp.buf_request_sync(0, 'textDocument/codeAction', params) |
| 100 | + for cid, res in pairs(result or {}) do |
| 101 | + for _, r in pairs(res.result or {}) do |
| 102 | + if r.edit then |
| 103 | + local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or 'utf-16' |
| 104 | + vim.lsp.util.apply_workspace_edit(r.edit, enc) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + vim.lsp.buf.format { async = false } |
| 109 | + end, |
| 110 | +}) |
| 111 | + |
| 112 | +-- adding the .slint filetype to the list of filetypes that neovim can recognize |
| 113 | +vim.cmd [[ |
| 114 | + augroup _slint |
| 115 | + autocmd! |
| 116 | + autocmd BufRead,BufEnter *.slint set filetype=slint |
| 117 | + augroup end |
| 118 | +]] |
0 commit comments