-- -- Copyright (c) 2026 Jan Fooken -- -- 20_mappings.lua -- -- Add user defined key mappings and override some of Neovim's built-in key -- mappings to make the commands more intuitive -- -- For an overview of the predefined mappings refer to the relevant sections in -- |index| (type :help index). -- For information on how to define custom mappings refer to |mapping| (type -- :help mapping) and the Lua module |vim.keymap| (type :help vim.keymap). -- local map = vim.keymap.set -- Navigation ================================================================== -- In Normal and Visual, but not Select mode, treat a wrapped line as new a line -- while navigating down. map( { 'n', 'x' }, 'j', function() return vim.v.count == 0 and 'gj' or 'j' end, { expr = true } ) map( { 'n', 'x' }, '', function() return vim.v.count == 0 and 'g' or '' end, { expr = true } ) -- In Normal and Visual, but not Select mode, treat a wrapped line as new a line -- while navigating up. map( { 'n', 'x' }, 'k', function() return vim.v.count == 0 and 'gk' or 'k' end, { expr = true } ) map( { 'n', 'x' }, '', function() return vim.v.count == 0 and 'g' or '' end, { expr = true } ) -- Open the netrw local-directory on the current file's directory in the current -- buffer. If you don't wish to use netrw, then you need to avoid loading both -- the plugin and the autoload portions of netrw by placing the following code -- snippet in your configuration. And delete this key mapping using -- |vim.keymap.del()|. -- >lua -- vim.g.loaded_netrw = 1 -- vim.g.loaded_netrwPlugin = 1 -- < map('n', 'e', 'Explore', { desc = 'Open netrw' }) -- Clipboard =================================================================== -- Yank {motion} text into register "+" (system clipboard). map({ 'n', 'x' }, 'gy', '"+y', { desc = 'Yank to system clipboard' }) -- Yank [count] lines into register "+" (system clipboard) without trailing -- newline. map({ 'n', 'x' }, 'gY', '"+y$', { desc = 'Yank line to system clipboard' }) -- Put the text from register "+" (system clipboard) after the cursor [count] -- times. This mapping overrides |gp|. map('n', 'gp', '"+p', { desc = 'Paste from system clipboard' }) -- Put the text from register "+" (system clipboard) after the cursor [count] -- times without setting the defined register. See |v_P|. map('x', 'gp', '"+P', { desc = 'Paste from system clipboard' }) -- Miscellaneous =============================================================== -- Shift the highlighted lines [count] 'shiftwidth' leftwards without leaving -- Visual mode. map('x', '<', '', '>gv') -- Open a window to show the location list for the current window. map('n', 'l', 'lopen', { desc = 'Open location list' }) -- Open a window to show the current list of errors. map('n', 'q', 'copen', { desc = 'Open quickfix list' }) -- Start Visual mode with the same area as the previous area and the same mode -- as the previously changed or yanked text. This mapping overrides |gV|. map('n', 'gV', '"`[" . strpart(getregtype(), 0, 1) . "`]"', { desc = 'Visually reselect changed or yanked text', expr = true, replace_keycodes = false, }) -- Search ====================================================================== -- Search forward for the [count]'th occurrence of the word nearest to the -- cursor. When searching, don't immediately jump to the next occurrence. map({ 'n', 'x' }, '*', '*N') -- Same as "*", but search backward. map({ 'n', 'x' }, '#', '#N') -- Like "*", but don't put "\<" and "\>" around the word. This makes the search -- also find matches that are not a whole word. map('n', 'g*', 'g*N') -- Like "#", but don't put "\<" and "\>" around the word. This makes the search -- also find matches that are not a whole word. map('n', 'g#', 'g#N') -- Comment in to repeat the latest "/" or "?" [count] times, while keeping the -- cursor centred. --map('n', 'n', 'nzz') -- Comment in to repeat the latest "/" or "?" [count] times in opposite -- direction, while keeping the cursor centred. --map('n', 'N', 'Nzz') -- Search forward inside the Visual area for the [count]'th occurrence of -- {pattern} |exclusive|. This mapping overrides |/|. map( 'x', '/', '/\\%V', { desc = 'Search forward inside selection', silent = false } ) -- Search backward inside the Visual area for the [count]'th previous occurrence -- of {pattern} |exclusive|. This mapping overrides |?|. map( 'x', '?', '?\\%V', { desc = 'Search backward inside selection', silent = false } ) -- Abbreviations =============================================================== -- Quit the current window. map('ca', 'Q', 'q') -- Quit without writing, also when the current buffer has changes. map('ca', 'Q!', 'q!') -- Write the whole buffer to the current file. map('ca', 'W', 'w') -- Write the whole buffer to the current file and quit the current window -- afterwards. map('ca', 'Wq', 'wq')