aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjdhao <jdhao@hotmail.com>2022-09-06 21:26:12 +0800
committerGitHub <noreply@github.com>2022-09-06 21:26:12 +0800
commit5a69d43cab69935d05933f413f57691cd690e937 (patch)
tree67dcf63aba6effc83c784778b0d7d62fa8b584d4
parent858848128f17121c9b00edb5ef4b89f4cb47ab09 (diff)
parent11e6797cb5cc5cadbd148e20308818c68aabd335 (diff)
Merge pull request #92 from jdhao/lua-map
refactor: convert mapping from viml to lua
-rw-r--r--core/mappings.lua294
-rw-r--r--core/mappings.vim195
-rw-r--r--init.lua2
-rw-r--r--lua/custom-map.lua11
4 files changed, 295 insertions, 207 deletions
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 <c-u> <Esc>viwUea
+keymap.set("i", "<c-u>", "<Esc>viwUea")
+
+-- Turn the current word into title case
+-- inoremap <c-t> <Esc>b~lea
+keymap.set("i", "<c-u>", "<Esc>b~lea")
+
+-- Paste non-linewise text above or below current line, see https://stackoverflow.com/a/1346777/6064933
+keymap.set("n", "<leader>p", "m`o<ESC>p``", {
+ desc = "paste below current line",
+})
+keymap.set("n", "<leader>P", "m`O<ESC>p``", {
+ desc = "paste above current line",
+})
+
+-- Shortcut for faster save and quit
+keymap.set("n", "<leader>w", "<cmd>update<cr>", {
+ silent = true,
+ desc = "save buffer",
+})
+
+-- Saves the file if modified and quit
+keymap.set("n", "<leader>q", "<cmd>x<cr>", {
+ silent = true,
+ desc = "quit current window",
+})
+
+-- Quit all opened buffers
+keymap.set("n", "<leader>Q", "<cmd>qa!<cr>", {
+ silent = true,
+ desc = "quit nvim",
+})
+
+-- Navigation in the location and quickfix list
+keymap.set("n", "[l", "<cmd>lprevious<cr>zv", {
+ silent = true,
+ desc = "previous location item",
+})
+keymap.set("n", "]l", "<cmd>lnext<cr>zv", {
+ silent = true,
+ desc = "next location item",
+})
+
+keymap.set("n", "[L", "<cmd>lfirst<cr>zv", {
+ silent = true,
+ desc = "first location item",
+})
+keymap.set("n", "]L", "<cmd>llast<cr>zv", {
+ silent = true,
+ desc = "last location item",
+})
+
+keymap.set("n", "[q", "<cmd>cprevious<cr>zv", {
+ silent = true,
+ desc = "previous qf item",
+})
+keymap.set("n", "]q", "<cmd>cnext<cr>zv", {
+ silent = true,
+ desc = "next qf item",
+})
+
+keymap.set("n", "[Q", "<cmd>cfirst<cr>zv", {
+ silent = true,
+ desc = "first qf item",
+})
+keymap.set("n", "]Q", "<cmd>clast<cr>zv", {
+ 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]], "<cmd>windo lclose <bar> cclose <cr>", {
+ 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]], "<cmd>bprevious <bar> bdelete #<cr>", {
+ 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", "<space>o", "printf('m`%so<ESC>``', v:count1)", {
+ expr = true,
+ desc = "insert line below",
+})
+
+keymap.set("n", "<space>O", "printf('m`%sO<ESC>``', 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")
+keymap.set("x", ">", ">gv")
+
+-- Edit and reload nvim config file quickly
+keymap.set("n", "<leader>ev", "<cmd>tabnew $MYVIMRC <bar> tcd %:h<cr>", {
+ silent = true,
+ desc = "open init.lua",
+})
+
+keymap.set("n", "<leader>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 <expr> <leader>v
+keymap.set("n", "<leader>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 / :<C-U>call feedkeys('/\%>'.(line("'<")-1).'l\%<'.(line("'>")+1)."l")<CR>
+
+-- 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", "<leader>cd", "<cmd>lcd %:p:h<cr><cmd>pwd<cr>", {
+ desc = "change cwd",
+})
+
+-- Use Esc to quit builtin terminal
+keymap.set("t", "<Esc>", [[<c-\><c-n>]])
+
+-- Toggle spell checking
+keymap.set("n", "<F11>", "<cmd>set spell!<cr>", {
+ desc = "toggle spell",
+})
+keymap.set("i", "<F11>", "<c-o><cmd>set spell!<cr>", {
+ 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", "<leader><space>", "<cmd>StripTrailingWhitespace<cr>", {
+ desc = "remove trailing space",
+})
+
+-- check the syntax group of current cursor position
+keymap.set("n", "<leader>st", "<cmd>call utils#SynGroup()<cr>", {
+ desc = "check syntax group",
+})
+
+-- Copy entire buffer.
+keymap.set("n", "<leader>y", "<cmd>%yank<cr>", {
+ desc = "yank entire buffer",
+})
+
+-- Toggle cursor column
+keymap.set("n", "<leader>cl", "<cmd>call utils#ToggleCursorCol()<cr>", {
+ desc = "toggle cursor column",
+})
+
+-- Move current line up and down
+keymap.set("n", "<A-k>", '<cmd>call utils#SwitchLine(line("."), "up")<cr>', {
+ desc = "move line up",
+})
+keymap.set("n", "<A-j>", '<cmd>call utils#SwitchLine(line("."), "down")<cr>', {
+ desc = "move line down",
+})
+
+-- Move current visual-line selection up and down
+keymap.set("x", "<A-k>", '<cmd>call utils#MoveSelection("up")<cr>', {
+ desc = "move selection up",
+})
+
+keymap.set("x", "<A-j>", '<cmd>call utils#MoveSelection("down")<cr>', {
+ 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", '"_c<Esc>p')
+
+-- Go to a certain buffer
+keymap.set("n", "gb", '<cmd>call buf_utils#GoToBuffer(v:count, "forward")<cr>', {
+ desc = "go to buffer (forward)",
+})
+keymap.set("n", "gB", '<cmd>call buf_utils#GoToBuffer(v:count, "backward")<cr>', {
+ desc = "go to buffer (backward)",
+})
+
+-- Switch windows
+keymap.set("n", "<left>", "<c-w>h")
+keymap.set("n", "<Right>", "<C-W>l")
+keymap.set("n", "<Up>", "<C-W>k")
+keymap.set("n", "<Down>", "<C-W>j")
+
+-- Text objects for URL
+keymap.set({ "x", "o" }, "iu", "<cmd>call text_obj#URL()<cr>", {
+ desc = "URL text object",
+})
+
+-- Text objects for entire buffer
+keymap.set({ "x", "o" }, "iB", "<cmd>call text_obj#Buffer()<cr>", {
+ 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 .. '<c-g>u')
+end
+
+-- insert semicolon in the end
+keymap.set('i', '<A-;>', '<Esc>miA;<Esc>`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', '<C-A>', '<HOME>')
+keymap.set('i', '<C-E>', '<END>')
+
+-- Go to beginning of command in command-line mode
+keymap.set('c', '<C-A>', '<HOME>')
+
+-- Delete the character to the right of the cursor
+keymap.set('i', '<C-D>', '<DEL>')
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 <c-u> <Esc>viwUea
-
-" Turn the current word into title case
-inoremap <c-t> <Esc>b~lea
-
-" Paste non-linewise text above or below current cursor,
-" see https://stackoverflow.com/a/1346777/6064933
-nnoremap <leader>p m`o<ESC>p``
-nnoremap <leader>P m`O<ESC>p``
-
-" Shortcut for faster save and quit
-nnoremap <silent> <leader>w :<C-U>update<CR>
-" Saves the file if modified and quit
-nnoremap <silent> <leader>q :<C-U>x<CR>
-" Quit all opened buffers
-nnoremap <silent> <leader>Q :<C-U>qa!<CR>
-
-" Navigation in the location and quickfix list
-nnoremap <silent> [l :<C-U>lprevious<CR>zv
-nnoremap <silent> ]l :<C-U>lnext<CR>zv
-nnoremap <silent> [L :<C-U>lfirst<CR>zv
-nnoremap <silent> ]L :<C-U>llast<CR>zv
-nnoremap <silent> [q :<C-U>cprevious<CR>zv
-nnoremap <silent> ]q :<C-U>cnext<CR>zv
-nnoremap <silent> [Q :<C-U>cfirst<CR>zv
-nnoremap <silent> ]Q :<C-U>clast<CR>zv
-
-" Close location list or quickfix list if they are present,
-" see https://superuser.com/q/355325/736190
-nnoremap<silent> \x :<C-U>windo lclose <bar> cclose<CR>
-
-" Close a buffer and switching to another buffer, do not close the
-" window, see https://stackoverflow.com/q/4465095/6064933
-nnoremap <silent> \d :<C-U>bprevious <bar> bdelete #<CR>
-
-" Insert a blank line below or above current line (do not move the cursor),
-" see https://stackoverflow.com/a/16136133/6064933
-nnoremap <expr> <Space>o printf('m`%so<ESC>``', v:count1)
-nnoremap <expr> <Space>O printf('m`%sO<ESC>``', v:count1)
-
-" Insert a space after current character
-nnoremap <Space><Space> a<Space><ESC>h
-
-" Move the cursor based on physical lines, not the actual lines.
-nnoremap <expr> j (v:count == 0 ? 'gj' : 'j')
-nnoremap <expr> 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 <Tab> %
-
-" 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
-xnoremap > >gv
-
-" When completion menu is shown, use <cr> to select an item and do not add an
-" annoying newline. Otherwise, <enter> is what it is. For more info , see
-" https://superuser.com/a/941082/736190 and
-" https://unix.stackexchange.com/q/162528/221410
-" inoremap <expr> <cr> ((pumvisible())?("\<C-Y>"):("\<cr>"))
-" Use <esc> to close auto-completion menu
-" inoremap <expr> <esc> ((pumvisible())?("\<C-e>"):("\<esc>"))
-
-" Tab-complete, see https://vi.stackexchange.com/q/19675/15292.
-inoremap <expr> <tab> pumvisible() ? "\<c-n>" : "\<tab>"
-inoremap <expr> <s-tab> pumvisible() ? "\<c-p>" : "\<s-tab>"
-
-" Edit and reload nvim config file quickly
-nnoremap <silent> <leader>ev :<C-U>tabnew $MYVIMRC <bar> tcd %:h<cr>
-nnoremap <silent> <leader>sv :<C-U>silent update $MYVIMRC <bar> source $MYVIMRC <bar>
- \ call v:lua.vim.notify("Nvim config successfully reloaded!", 2, {'title': 'nvim-config'})<cr>
-
-" Reselect the text that has just been pasted, see also https://stackoverflow.com/a/4317090/6064933.
-nnoremap <expr> <leader>v printf('`[%s`]', getregtype()[0])
-
-" Always use very magic mode for searching
-nnoremap / /\v
-
-" Search in selected region
-xnoremap / :<C-U>call feedkeys('/\%>'.(line("'<")-1).'l\%<'.(line("'>")+1)."l")<CR>
-
-" Change current working directory locally and print cwd after that,
-" see https://vim.fandom.com/wiki/Set_working_directory_to_the_current_file
-nnoremap <silent> <leader>cd :<C-U>lcd %:p:h<CR>:pwd<CR>
-
-" Use Esc to quit builtin terminal
-tnoremap <ESC> <C-\><C-n>
-
-" Toggle spell checking (autosave does not play well with z=, so we disable it
-" when we are doing spell checking)
-nnoremap <silent> <F11> :<C-U>set spell!<cr>
-inoremap <silent> <F11> <C-O>:<C-U>set spell!<cr>
-
-" 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 <silent> <leader><Space> :<C-U>StripTrailingWhitespace<CR>
-
-" check the syntax group of current cursor position
-nnoremap <silent> <leader>st :<C-U>call utils#SynGroup()<CR>
-
-" Clear highlighting
-if maparg('<C-L>', 'n') ==# ''
- nnoremap <silent> <C-L> :<C-U>nohlsearch<C-R>=has('diff')?'<Bar>diffupdate':''<CR><CR><C-L>
-endif
-
-" Copy entire buffer.
-nnoremap <silent> <leader>y :<C-U>%y<CR>
-
-" Toggle cursor column
-nnoremap <silent> <leader>cl :<C-U>call utils#ToggleCursorCol()<CR>
-
-" Move current line up and down
-nnoremap <silent> <A-k> <Cmd>call utils#SwitchLine(line('.'), 'up')<CR>
-nnoremap <silent> <A-j> <Cmd>call utils#SwitchLine(line('.'), 'down')<CR>
-
-" Move current visual-line selection up and down
-xnoremap <silent> <A-k> :<C-U>call utils#MoveSelection('up')<CR>
-xnoremap <silent> <A-j> :<C-U>call utils#MoveSelection('down')<CR>
-
-" Replace visual selection with text in register, but not contaminate the
-" register, see also https://stackoverflow.com/q/10723700/6064933.
-xnoremap p "_c<ESC>p
-
-nnoremap <silent> gb :<C-U>call buf_utils#GoToBuffer(v:count, 'forward')<CR>
-nnoremap <silent> gB :<C-U>call buf_utils#GoToBuffer(v:count, 'backward')<CR>
-
-nnoremap <Left> <C-W>h
-nnoremap <Right> <C-W>l
-nnoremap <Up> <C-W>k
-nnoremap <Down> <C-W>j
-
-" Text objects for URL
-xnoremap <silent> iu :<C-U>call text_obj#URL()<CR>
-onoremap <silent> iu :<C-U>call text_obj#URL()<CR>
-
-" Text objects for entire buffer
-xnoremap <silent> iB :<C-U>call text_obj#Buffer()<CR>
-onoremap <silent> iB :<C-U>call text_obj#Buffer()<CR>
-
-" 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 %s<C-g>u', ch, ch)
-endfor
-
-" insert semicolon in the end
-inoremap <A-;> <ESC>miA;<ESC>`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', '<C-A>', '<HOME>')
-keymap.set('i', '<C-E>', '<END>')
-
--- Go to beginning of command in command-line mode
-keymap.set('c', '<C-A>', '<HOME>')
-
--- Delete the character to the right of the cursor
-keymap.set('i', '<C-D>', '<DEL>')