----[[ init.lua ]] ---- LEADER ---- these keybindings need to be defined before the first / ---- is called; otherwise, it will default to "\" --vim.g.mapleader = " " --vim.g.localleader = "\\" vim.g.mapleader = ' ' vim.g.maplocalleader = vim.g.mapleader .. vim.g.mapleader -- lua command mode vim.keymap.set('n', '', ':lua ') -- preserve flags for &, and add it in visual mode. vim.keymap.set({ 'n', 'x' }, '&', ':&&') vim.keymap.set('n', '', 'q') -- vim.keymap.set('n', 'q', 'q!') -- vim.keymap.set('n', '', 'q!') -- remove search result highlighting vim.keymap.set('n', '', ':nohlsearch') -- beginning and end of line in command mode vim.keymap.set('c', '', '', {}) vim.keymap.set('c', '', '', {}) -- map "x" to a Lua function: vim.keymap.set('n', 'x', function() print("real lua function") end) -- map "x" to multiple modes for the current buffer: vim.keymap.set({'n', 'v'}, 'x', vim.lsp.buf.references, { buffer = true }) -- map to an expression (|:map-|): vim.keymap.set('i', '', function() return vim.fn.pumvisible() == 1 and "" or "" end, { expr = true }) -- map "[%%" to a mapping: vim.keymap.set('n', '[%%', '(MatchitNormalMultiBackward)') -- mappings.lua local current_line = vim.api.nvim_get_current_line local g = vim.g local map = vim.keymap.set local expr_opts = { expr = true } local expr_noreplace_opts = { expr = true, replace_keycodes = false } local silent_opts = { silent = true } local v = vim.v ------------------------------- -- Leader ------------------------------- g.mapleader = "," ------------------------------- -- Window managment mappings ------------------------------- map("n", "s", "s") map("n", "v", "v") map("n", "-", ":botright new=", silent_opts) map("n", "|", ":botright vnew=", silent_opts) map("n", "q", "c") map("n", "c", ":cclose:lclose:pclose", silent_opts) map("n", "t", ":$tabnew", silent_opts) map("n", "T", ":tabclose", silent_opts) map("n", "1", "1gt") map("n", "2", "2gt") map("n", "3", "3gt") map("n", "4", "4gt") map("n", "5", "5gt") map("n", "6", "6gt") map("n", "7", "7gt") map("n", "8", "8gt") map("n", "9", "9gt") map("n", "=", "=") map("n", "r", "r") ------------------------------- -- Navigation mappings ------------------------------- map("n", "", "w") map("n", "", "k") map("n", "", "j") map("n", "", "h") map("n", "", "l") -- Vertical movements. map("n", "", "10j") -- Note, in my Alacritty terminal config I have 'Control-c' re-mapped to -- 'Control-k', hence this mapping actually is 'Control-k' (k for up). map("n", "", "10k") -- Move vertically by visual line unless preceded by a count. If a movement is -- greater than 5 then automatically add to the jumplist. map("n", "j", "v:count ? (v:count > 5 ? \"m'\" . v:count : '') . 'j' : 'gj'", expr_opts) map("n", "k", "v:count ? (v:count > 5 ? \"m'\" . v:count : '') . 'k' : 'gk'", expr_opts) -- Navigate buffers via 'wildmenu'. map("n", "", ":buffer") -- Alternate the last two files. map("n", "", "") ------------------------------- -- General mappings ------------------------------- -- Enter command mode via ';' map({ "n", "x" }, ";", ":") -- Make dot work on visual line selections. map("x", ".", ":normal .") -- U for redo, the opposite of u for undo. map("n", "U", "") -- 'qq' starts and stops a macro recording ('m' register). map("n", "qq", "len(reg_recording()) > 0 ? 'q' : 'qm'", expr_opts) -- Map 'p' to 'P' in visual mode since 'P' will never yank the pasted-over-text -- into the unnamed register. map("x", "p", "P") -- Confirm quit. map("n", "", ":confirm qall", silent_opts) -- Make escape work in the Neovim terminal. map("t", "", "") -- In visual mode only search within visual selection. map("x", "/", "/\\%V") -- Wild menu is set to popup menu, it is best to invert the meaning of -- up/down and left/right keys since this will be more natural map("c", "", "pumvisible() ? '' : ''", expr_noreplace_opts) map("c", "", "pumvisible() ? '' : ''", expr_noreplace_opts) map("c", "", "pumvisible() ? '' : ''", expr_noreplace_opts) map("c", "", "pumvisible() ? '' : ''", expr_noreplace_opts) -- Direct changes to the black hole register. map("n", "c", '"_c') map("n", "C", '"_C') -- Also direct deletions of empty lines to the black hole register. map("n", "dd", function() if #current_line() == 0 then return '"_dd' else return "dd" end end, expr_opts) -- Automatically indent the cursor when entering insert mode on an empty line. map("n", "i", function() -- For counted insert just execute a standard "insert" operation. if v.count > 0 then return "i" end if #current_line() == 0 then return [["_cc]] else return "i" end end, expr_opts) -- Disable arrow keys. map("n", "", "") map("n", "", "") map("n", "", "") map("n", "", "") ------------------------------- -- Helper mappings ------------------------------- -- Insert from the clipboard register. -- Note, use Control-q for virtual insertion (e.g insert a real tab). map("i", "", "+") -- Begin a new line above the current cursor position. map("i", "", "O") -- Fix syntax and redraw the screen. map("n", "", ":TSBufDisable highlight:TSBufEnable highlight:redraw") -- Clone line and comment it out. map("n", "ycc", "yygccp", { remap = true }) -- Clone paragraph. map("n", "cp", "yapp") -- Format current paragraph in normal mode. map("n", "qp", "gqip$") -- Format current line. map("n", "qj", "gqj") -- Toggle current fold. map("n", "z", "zazz") -- Expand all folds. map("n", "Z", ":set nofoldenable") -- Toggle crosshair, aka cusorcolumn & cursorline. map("n", "x", require("util.crosshair").toggle) -- Maximise the current buffer into a tab. map("n", "m", ":tab split", silent_opts) -- Maximize vertical height. map("n", "+", "_") -- Show introduction message. map("n", "i", ":intro") -- Preserve visual selection when shifting. map("x", "<", "", ">gv") -- Vertically move visual block. map("x", "J", ":m '>+1gv=gv") map("x", "K", ":m '<-2gv=gv") ------------------------------- -- Paired mappings ------------------------------- map("n", "{", "{zz") map("n", "}", "}zz") map("n", "n", "nzz") map("n", "N", "Nzz") map("n", "[c", "[czz") map("n", "]c", "]czz") map("n", "[j", "zz") map("n", "]j", "zz") map("n", "[p", "Op") map("n", "]p", "op") map("n", "[q", ":cpreviouszz", silent_opts) map("n", "]q", ":cnextzz", silent_opts) map("n", "[s", "[szz") map("n", "]s", "]szz") map("n", "[z", "zH") map("n", "]z", "zL") ------------------------------- -- Scroll mappings ------------------------------- map("n", "", "") map("n", "", "") map("i", "", "") map("i", "", "") ------------------------------- -- Completion mappings ------------------------------- -- Map TAB and SHIFT-TAB to forward and backwards completion. map("i", "", require("util.complete").tab) map("i", "", require("util.complete").shift_tab) -- Space - context aware omni completion (via 'omnifunc' setting) -- b - keyword completion from the current buffer ( to extend) -- d - dictionary completion (via 'dictionary' setting) -- f - file path completion -- l - line completion (repeat an existing line) map("i", "", "") map("i", "", "") map("i", "", "") map("i", "", "") map("i", "", "") -- s - snippets completion -- Refer to ~/dotfiles/nvim/lua/config/snippy.lua: ------------------------------- -- Function key mappings ------------------------------- map("n", "", function() vim.show_pos() end) map("n", "'1", function() vim.show_pos() end) map("n", "", ":w") map("n", "'2", ":w") map("n", "", ":%s/ /_") map("n", "'4", ":%s/ /_") map("n", "", require("util.spell").toggle) map("n", "'5", require("util.spell").toggle) map("n", "", require("util.diagnostic-visibility").toggle) map("n", "'6", require("util.diagnostic-visibility").toggle) map("n", "", ":set lazyredraw!:set autoread!:call AutoSaveToggle()") map("n", "'7", ":set lazyredraw!:set autoread!:call AutoSaveToggle()") map("n", "", ":set wrap!") map("n", "'8", ":set wrap!") map("n", "", ":set hlsearch!") map("n", "'9", ":set hlsearch!") map("n", "", require("util.listchars").toggle) map("n", "'0", require("util.listchars").toggle) ------------------------------- -- Increment and decrement mappings ------------------------------- map("n", "+", "") map("n", "-", "") map("x", "+", "g") map("x", "-", "g") ------------------------------- -- Command Readline-like mappings ------------------------------- -- Ctrl-a - go to the start of line -- Ctrl-e - go to the end of the line map("n", "", "0") map("n", "", "$") map("i", "", "0") map("i", "", "$") map("c", "", "") map("c", "", "") -- Alt-b - back a word -- Alt-f - forward a word -- Alt-BS - delete backward word -- Alt-d - delete forward word map("i", "", "") map("i", "", "") map("i", "", "") map("i", "", "dw") map("c", "", "") map("c", "", "") map("c", "", "") map("c", "", "") ------------------------------- -- Grep mappings ------------------------------- map("n", "/", ":silent grep") map("n", "gs", ":silent grep ") map("x", "gs", '"sy:silent grep s') ------------------------------- -- Find & replace helpers ------------------------------- -- Star search that does not move forward to the next match map("n", "g*", ":let @/='\\<'.expand('').'\\>'", silent_opts) map("x", "g*", '"sy:let @/=@s', silent_opts) -- Find that does an immediate replace on the match. map("n", "\\c", ":let @/='\\<'.expand('').'\\>'cgn") map("x", "\\c", '"sy:let @/=@scgn') -- Accept/reject helpers after completing a \c cgn-based find & replace -- operation. -- -- Go to the next find match and highlight it. map("n", "", "gnzz") -- Accept the change and go to the next match and highlight it. map("x", "", ".gnzz", { remap = true }) -- Reject the change and go to the next match and highlight it. map("x", "!", "ngnzz") -- Find and replace within the current file. map("n", "\\s", ":%s///") map("x", "\\s", '"sy:%s/s//') -- Project-wide find and replace using grep. map("n", "\\S", [[:let @s='\<'.expand('').'\>']] .. [[:let &grepprg=&grepprg . ' -w']] .. [[:silent grep ]] .. [[:let &grepprg='rg --vimgrep --smart-case']] .. [[:cfdo %s/s// | update]] .. [[]]) map("x", "\\S", [["sy|]] .. [[:silent grep s]] .. [[:cfdo %s/s// | update]] .. [[]]) -------------------------------------------------------------------------- -- local map = vim.keymap.set -- -- -- toggle color column -- map("n", "|", function() -- if tonumber(vim.o.cc) then -- vim.g._cc = vim.o.cc -- vim.o.cc = "" -- else -- assert(tonumber(vim.g._cc)) -- vim.o.cc = vim.g._cc -- end -- end, { silent = true, desc = "toggle color column on/off" }) -- -- -- run command and put output in new buffer -- map("n", "c", function() -- vim.ui.input({}, function(c) -- if c and c~="" then -- vim.cmd("noswapfile vnew") -- vim.bo.buftype = "nofile" -- vim.bo.bufhidden = "wipe" -- vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.fn.systemlist(c)) -- end -- end) -- end) -- Utility to allow matching up paired characters and intelligently -- handling moving forward. -- -- ## Examples -- Given `vim.keymap.set('i', ')', make_matcher(')', ')'), opts)`. -- -- Hello($) -- Typing `)` will move left resulting `Hello()$` -- -- In other cases `)` will be inserted like normal. -- function make_matcher(match, to_insert) -- return function() -- local x = vim.api.nvim_win_get_cursor(0) -- local row, col = unpack(x) -- local line = vim.api.nvim_get_current_line() -- -- if (line:len() >= col + 1) then -- local next_char = line:sub(col + 1, col + 1) -- -- if (next_char == match) then -- vim.api.nvim_win_set_cursor(0, { row, col +1 }) -- return -- end -- end -- -- vim.api.nvim_buf_set_text(0, row - 1, col, row - 1, col, {to_insert}) -- -- if (to_insert:len() == 2) then -- vim.api.nvim_win_set_cursor(0, { row, col + 1 }) -- else -- vim.api.nvim_win_set_cursor(0, { row, col + 2 }) -- end -- end -- end -- -- function make_simple_matcher(char) -- return make_matcher(char, char) -- end -- -- local opts = { noremap = true, silent = true } -- -- -- Match characters -- -- The i bit here has the effect of putting the -- -- insert mode cursor between the pair of characters. -- vim.keymap.set('i', '(', '()i', opts) -- vim.keymap.set('i', '[', '[]i', opts) -- vim.keymap.set('i', '{', '{}i', opts) -- -- -- map('i', "'", "''i", opts) This mapping is annoying with Rust lifetimes -- -- -- Intelligent matchers for second parts of pairs and pairs composed -- -- of the same character -- vim.keymap.set('i', ')', make_simple_matcher(')'), opts) -- vim.keymap.set('i', ']', make_simple_matcher(']'), opts) -- vim.keymap.set('i', '}', make_simple_matcher('}'), opts) -- vim.keymap.set('i', '"', make_matcher('"', '""'), opts) -- vim.keymap.set('i', '`', make_matcher('`', '``'), opts)