From 11e6797cb5cc5cadbd148e20308818c68aabd335 Mon Sep 17 00:00:00 2001 From: jdhao Date: Tue, 6 Sep 2022 21:22:45 +0800 Subject: [PATCH] refactor: convert mapping from viml to lua --- core/mappings.lua | 294 +++++++++++++++++++++++++++++++++++++++++++++ core/mappings.vim | 195 ------------------------------ init.lua | 2 +- lua/custom-map.lua | 11 -- 4 files changed, 295 insertions(+), 207 deletions(-) create mode 100644 core/mappings.lua delete mode 100644 core/mappings.vim delete mode 100644 lua/custom-map.lua diff --git a/core/mappings.lua b/core/mappings.lua new file mode 100644 index 0000000..3bcc6fa --- /dev/null +++ b/core/mappings.lua @@ -0,0 +1,294 @@ +local keymap = vim.keymap +local api = vim.api + +-- Save key strokes (now we do not need to press shift to enter command mode). +keymap.set({ "n", "x" }, ";", ":") + +-- Turn the word under cursor to upper case +-- inoremap viwUea +keymap.set("i", "", "viwUea") + +-- Turn the current word into title case +-- inoremap b~lea +keymap.set("i", "", "b~lea") + +-- Paste non-linewise text above or below current line, see https://stackoverflow.com/a/1346777/6064933 +keymap.set("n", "p", "m`op``", { + desc = "paste below current line", +}) +keymap.set("n", "P", "m`Op``", { + desc = "paste above current line", +}) + +-- Shortcut for faster save and quit +keymap.set("n", "w", "update", { + silent = true, + desc = "save buffer", +}) + +-- Saves the file if modified and quit +keymap.set("n", "q", "x", { + silent = true, + desc = "quit current window", +}) + +-- Quit all opened buffers +keymap.set("n", "Q", "qa!", { + silent = true, + desc = "quit nvim", +}) + +-- Navigation in the location and quickfix list +keymap.set("n", "[l", "lpreviouszv", { + silent = true, + desc = "previous location item", +}) +keymap.set("n", "]l", "lnextzv", { + silent = true, + desc = "next location item", +}) + +keymap.set("n", "[L", "lfirstzv", { + silent = true, + desc = "first location item", +}) +keymap.set("n", "]L", "llastzv", { + silent = true, + desc = "last location item", +}) + +keymap.set("n", "[q", "cpreviouszv", { + silent = true, + desc = "previous qf item", +}) +keymap.set("n", "]q", "cnextzv", { + silent = true, + desc = "next qf item", +}) + +keymap.set("n", "[Q", "cfirstzv", { + silent = true, + desc = "first qf item", +}) +keymap.set("n", "]Q", "clastzv", { + silent = true, + desc = "last qf item", +}) + +-- Close location list or quickfix list if they are present, see https://superuser.com/q/355325/736190 +keymap.set("n", [[\x]], "windo lclose cclose ", { + silent = true, + desc = "close qf and location list", +}) + +-- Delete a buffer, without closing the window, see https://stackoverflow.com/q/4465095/6064933 +keymap.set("n", [[\d]], "bprevious bdelete #", { + silent = true, + desc = "delete buffer", +}) + +-- Insert a blank line below or above current line (do not move the cursor), +-- see https://stackoverflow.com/a/16136133/6064933 +keymap.set("n", "o", "printf('m`%so``', v:count1)", { + expr = true, + desc = "insert line below", +}) + +keymap.set("n", "O", "printf('m`%sO``', v:count1)", { + expr = true, + desc = "insert line above", +}) + +-- Move the cursor based on physical lines, not the actual lines. +keymap.set("n", "j", "v:count == 0 ? 'gj' : 'j'", { expr = true }) +keymap.set("n", "k", "v:count == 0 ? 'gk' : 'k'", { expr = true }) +keymap.set("n", "^", "g^") +keymap.set("n", "0", "g0") + +-- Do not include white space characters when using $ in visual mode, +-- see https://vi.stackexchange.com/q/12607/15292 +keymap.set("x", "$", "g_") + +-- Go to start or end of line easier +keymap.set({ "n", "x" }, "H", "^") +keymap.set({ "n", "x" }, "L", "g_") + +-- Continuous visual shifting (does not exit Visual mode), `gv` means +-- to reselect previous visual area, see https://superuser.com/q/310417/736190 +keymap.set("x", "<", "", ">gv") + +-- Edit and reload nvim config file quickly +keymap.set("n", "ev", "tabnew $MYVIMRC tcd %:h", { + silent = true, + desc = "open init.lua", +}) + +keymap.set("n", "sv", "", { + silent = true, + desc = "reload init.lua", + callback = function() + vim.cmd [[ + update $MYVIMRC + source $MYVIMRC + ]] + vim.notify("Nvim config successfully reloaded!", vim.log.levels.INFO, { title = "nvim-config" }) + end, +}) + +-- Reselect the text that has just been pasted, see also https://stackoverflow.com/a/4317090/6064933. +-- nnoremap v +keymap.set("n", "v", "printf('`[%s`]', getregtype()[0])", { + expr = true, + desc = "reselect last pasted area", +}) + +-- Always use very magic mode for searching +keymap.set("n", "/", [[/\v]]) + +-- Search in selected region +-- xnoremap / :call feedkeys('/\%>'.(line("'<")-1).'l\%<'.(line("'>")+1)."l") + +-- Change current working directory locally and print cwd after that, +-- see https://vim.fandom.com/wiki/Set_working_directory_to_the_current_file +keymap.set("n", "cd", "lcd %:p:hpwd", { + desc = "change cwd", +}) + +-- Use Esc to quit builtin terminal +keymap.set("t", "", [[]]) + +-- Toggle spell checking +keymap.set("n", "", "set spell!", { + desc = "toggle spell", +}) +keymap.set("i", "", "set spell!", { + desc = "toggle spell", +}) + +-- Change text without putting it into the vim register, +-- see https://stackoverflow.com/q/54255/6064933 +keymap.set("n", "c", '"_c') +keymap.set("n", "C", '"_C') +keymap.set("n", "cc", '"_cc') +keymap.set("x", "c", '"_c') + +-- Remove trailing whitespace characters +keymap.set("n", "", "StripTrailingWhitespace", { + desc = "remove trailing space", +}) + +-- check the syntax group of current cursor position +keymap.set("n", "st", "call utils#SynGroup()", { + desc = "check syntax group", +}) + +-- Copy entire buffer. +keymap.set("n", "y", "%yank", { + desc = "yank entire buffer", +}) + +-- Toggle cursor column +keymap.set("n", "cl", "call utils#ToggleCursorCol()", { + desc = "toggle cursor column", +}) + +-- Move current line up and down +keymap.set("n", "", 'call utils#SwitchLine(line("."), "up")', { + desc = "move line up", +}) +keymap.set("n", "", 'call utils#SwitchLine(line("."), "down")', { + desc = "move line down", +}) + +-- Move current visual-line selection up and down +keymap.set("x", "", 'call utils#MoveSelection("up")', { + desc = "move selection up", +}) + +keymap.set("x", "", 'call utils#MoveSelection("down")', { + desc = "move selection down", +}) + +-- Replace visual selection with text in register, but not contaminate the register, +-- see also https://stackoverflow.com/q/10723700/6064933. +keymap.set("x", "p", '"_cp') + +-- Go to a certain buffer +keymap.set("n", "gb", 'call buf_utils#GoToBuffer(v:count, "forward")', { + desc = "go to buffer (forward)", +}) +keymap.set("n", "gB", 'call buf_utils#GoToBuffer(v:count, "backward")', { + desc = "go to buffer (backward)", +}) + +-- Switch windows +keymap.set("n", "", "h") +keymap.set("n", "", "l") +keymap.set("n", "", "k") +keymap.set("n", "", "j") + +-- Text objects for URL +keymap.set({ "x", "o" }, "iu", "call text_obj#URL()", { + desc = "URL text object", +}) + +-- Text objects for entire buffer +keymap.set({ "x", "o" }, "iB", "call text_obj#Buffer()", { + desc = "buffer text object", +}) + +-- Do not move my cursor when joining lines. +keymap.set("n", "J", "", { + desc = "join line", + callback = function() + vim.cmd [[ + normal! mzJ`z + delmarks z + ]] + end, +}) + +keymap.set("n", "gJ", "mzgJ`z", { + desc = "join visual lines", + callback = function() + -- we must use `normal!`, otherwise it will trigger recursive mapping + vim.cmd [[ + normal! zmgJ`z + delmarks z + ]] + end +}) + +-- Break inserted text into smaller undo units when we insert some punctuation chars. +local undo_ch = {',', '.', '!', '?', ';', ':'} +for _, ch in ipairs(undo_ch) do + keymap.set('i', ch, ch .. 'u') +end + +-- insert semicolon in the end +keymap.set('i', '', 'miA;`ii') + +-- Keep cursor position after yanking +keymap.set('n', 'y', 'myy') + +api.nvim_create_autocmd('TextYankPost', { + pattern = '*', + group = api.nvim_create_augroup("restore_after_yank", { clear = true }), + callback = function() + vim.cmd [[ + silent! normal! `y + silent! delmarks y + ]] + end +}) + +-- Go to the beginning and end of current line in insert mode quickly +keymap.set('i', '', '') +keymap.set('i', '', '') + +-- Go to beginning of command in command-line mode +keymap.set('c', '', '') + +-- Delete the character to the right of the cursor +keymap.set('i', '', '') diff --git a/core/mappings.vim b/core/mappings.vim deleted file mode 100644 index 23fe1ee..0000000 --- a/core/mappings.vim +++ /dev/null @@ -1,195 +0,0 @@ -" Save key strokes (now we do not need to press shift to enter command mode). -" Vim-sneak has also mapped `;`, so using the below mapping will break the map -" used by vim-sneak -nnoremap ; : -xnoremap ; : - -" Quicker way to open command window -nnoremap q; q: - -" Turn the word under cursor to upper case -inoremap viwUea - -" Turn the current word into title case -inoremap b~lea - -" Paste non-linewise text above or below current cursor, -" see https://stackoverflow.com/a/1346777/6064933 -nnoremap p m`op`` -nnoremap P m`Op`` - -" Shortcut for faster save and quit -nnoremap w :update -" Saves the file if modified and quit -nnoremap q :x -" Quit all opened buffers -nnoremap Q :qa! - -" Navigation in the location and quickfix list -nnoremap [l :lpreviouszv -nnoremap ]l :lnextzv -nnoremap [L :lfirstzv -nnoremap ]L :llastzv -nnoremap [q :cpreviouszv -nnoremap ]q :cnextzv -nnoremap [Q :cfirstzv -nnoremap ]Q :clastzv - -" Close location list or quickfix list if they are present, -" see https://superuser.com/q/355325/736190 -nnoremap \x :windo lclose cclose - -" Close a buffer and switching to another buffer, do not close the -" window, see https://stackoverflow.com/q/4465095/6064933 -nnoremap \d :bprevious bdelete # - -" Insert a blank line below or above current line (do not move the cursor), -" see https://stackoverflow.com/a/16136133/6064933 -nnoremap o printf('m`%so``', v:count1) -nnoremap O printf('m`%sO``', v:count1) - -" Insert a space after current character -nnoremap ah - -" Move the cursor based on physical lines, not the actual lines. -nnoremap j (v:count == 0 ? 'gj' : 'j') -nnoremap k (v:count == 0 ? 'gk' : 'k') -nnoremap ^ g^ -nnoremap 0 g0 - -" Do not include white space characters when using $ in visual mode, -" see https://vi.stackexchange.com/q/12607/15292 -xnoremap $ g_ - -" Jump to matching pairs easily in normal mode -nnoremap % - -" Go to start or end of line easier -nnoremap H ^ -xnoremap H ^ -nnoremap L g_ -xnoremap L g_ - -" Continuous visual shifting (does not exit Visual mode), `gv` means -" to reselect previous visual area, see https://superuser.com/q/310417/736190 -xnoremap < >gv - -" When completion menu is shown, use to select an item and do not add an -" annoying newline. Otherwise, is what it is. For more info , see -" https://superuser.com/a/941082/736190 and -" https://unix.stackexchange.com/q/162528/221410 -" inoremap ((pumvisible())?("\"):("\")) -" Use to close auto-completion menu -" inoremap ((pumvisible())?("\"):("\")) - -" Tab-complete, see https://vi.stackexchange.com/q/19675/15292. -inoremap pumvisible() ? "\" : "\" -inoremap pumvisible() ? "\" : "\" - -" Edit and reload nvim config file quickly -nnoremap ev :tabnew $MYVIMRC tcd %:h -nnoremap sv :silent update $MYVIMRC source $MYVIMRC - \ call v:lua.vim.notify("Nvim config successfully reloaded!", 2, {'title': 'nvim-config'}) - -" Reselect the text that has just been pasted, see also https://stackoverflow.com/a/4317090/6064933. -nnoremap v printf('`[%s`]', getregtype()[0]) - -" Always use very magic mode for searching -nnoremap / /\v - -" Search in selected region -xnoremap / :call feedkeys('/\%>'.(line("'<")-1).'l\%<'.(line("'>")+1)."l") - -" Change current working directory locally and print cwd after that, -" see https://vim.fandom.com/wiki/Set_working_directory_to_the_current_file -nnoremap cd :lcd %:p:h:pwd - -" Use Esc to quit builtin terminal -tnoremap - -" Toggle spell checking (autosave does not play well with z=, so we disable it -" when we are doing spell checking) -nnoremap :set spell! -inoremap :set spell! - -" Change text without putting it into the vim register, -" see https://stackoverflow.com/q/54255/6064933 -nnoremap c "_c -nnoremap C "_C -nnoremap cc "_cc -xnoremap c "_c - -" Remove trailing whitespace characters -nnoremap :StripTrailingWhitespace - -" check the syntax group of current cursor position -nnoremap st :call utils#SynGroup() - -" Clear highlighting -if maparg('', 'n') ==# '' - nnoremap :nohlsearch=has('diff')?'diffupdate':'' -endif - -" Copy entire buffer. -nnoremap y :%y - -" Toggle cursor column -nnoremap cl :call utils#ToggleCursorCol() - -" Move current line up and down -nnoremap call utils#SwitchLine(line('.'), 'up') -nnoremap call utils#SwitchLine(line('.'), 'down') - -" Move current visual-line selection up and down -xnoremap :call utils#MoveSelection('up') -xnoremap :call utils#MoveSelection('down') - -" Replace visual selection with text in register, but not contaminate the -" register, see also https://stackoverflow.com/q/10723700/6064933. -xnoremap p "_cp - -nnoremap gb :call buf_utils#GoToBuffer(v:count, 'forward') -nnoremap gB :call buf_utils#GoToBuffer(v:count, 'backward') - -nnoremap h -nnoremap l -nnoremap k -nnoremap j - -" Text objects for URL -xnoremap iu :call text_obj#URL() -onoremap iu :call text_obj#URL() - -" Text objects for entire buffer -xnoremap iB :call text_obj#Buffer() -onoremap iB :call text_obj#Buffer() - -" Do not move my cursor when joining lines. -nnoremap J mzJ`z -nnoremap gJ mzgJ`z - -" Break inserted text into smaller undo units. -for ch in [',', '.', '!', '?', ';', ':'] - execute printf('inoremap %s %su', ch, ch) -endfor - -" insert semicolon in the end -inoremap miA;`ii - -" Keep cursor position after yanking -nnoremap y myy -xnoremap y myy - -augroup restore_after_yank - autocmd! - autocmd TextYankPost * call s:restore_cursor() -augroup END - -function! s:restore_cursor() abort - silent! normal `y - silent! delmarks y -endfunction - -" for mappings defined in lua -lua require('custom-map') diff --git a/init.lua b/init.lua index eb79c1b..23449f9 100644 --- a/init.lua +++ b/init.lua @@ -24,7 +24,7 @@ local core_conf_files = { "globals.vim", -- some global settings "options.vim", -- setting options in nvim "autocommands.vim", -- various autocommands - "mappings.vim", -- all the user-defined mappings + "mappings.lua", -- all the user-defined mappings "plugins.vim", -- all the plugins installed and their configurations "colorschemes.lua", -- colorscheme settings } diff --git a/lua/custom-map.lua b/lua/custom-map.lua deleted file mode 100644 index 4d88548..0000000 --- a/lua/custom-map.lua +++ /dev/null @@ -1,11 +0,0 @@ -local keymap = vim.keymap - --- Go to the beginning and end of current line in insert mode quickly -keymap.set('i', '', '') -keymap.set('i', '', '') - --- Go to beginning of command in command-line mode -keymap.set('c', '', '') - --- Delete the character to the right of the cursor -keymap.set('i', '', '')