-- 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]] .. [[]])