From 2b331e7fa1ceb11eaa0734f294b58d30a441993b Mon Sep 17 00:00:00 2001 From: curtain <1410768946@qq.com> Date: Sat, 14 Aug 2021 15:56:54 +0800 Subject: [PATCH] add some useful keymap in insert/command mode with switch to toggle (#240) Co-authored-by: curtain --- lua/chadrc.lua | 7 +++++++ lua/mappings.lua | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lua/chadrc.lua b/lua/chadrc.lua index b04c9b9..221fb48 100644 --- a/lua/chadrc.lua +++ b/lua/chadrc.lua @@ -103,6 +103,13 @@ local M = { toggle_right = "v", toggle_bot = "h" }, + unix_keymap = { + toggle_unix_keymap = "k", + forward = "", + backward = "", + top_of_line = "", + end_of_line = "" + }, misc = { esc_Termmode = "jk", copywhole_file = "", diff --git a/lua/mappings.lua b/lua/mappings.lua index d32cdac..5d5ef4c 100644 --- a/lua/mappings.lua +++ b/lua/mappings.lua @@ -24,6 +24,45 @@ map("v", "x", [=[ "_x ]=], opt) this line too ]] -- +-- toggle unix readline's keymap +map("n", user_map.unix_keymap.toggle_unix_keymap,"lua require 'mappings'.unix_keymap()",{nowait = true}) + +local _cmap_containp = function (key) + local cmap_tab = vim.api.nvim_get_keymap("c") + + for _, value in ipairs(cmap_tab) do + if value['lhs'] == key then + return true + end + end + return false +end + +M.unix_keymap = function() + local m = user_map.unix_keymap + + if _cmap_containp("") then + vim.api.nvim_del_keymap("i", m.forward) + vim.api.nvim_del_keymap("i", m.backward) + vim.api.nvim_del_keymap("i", m.top_of_line) + vim.api.nvim_del_keymap("i", m.end_of_line) + vim.api.nvim_del_keymap("c", m.forward) + vim.api.nvim_del_keymap("c", m.backward) + vim.api.nvim_del_keymap("c", m.top_of_line) + vim.api.nvim_del_keymap("c", m.end_of_line) + else + map("i", m.forward, '', opt) + map("i", m.backward, '', opt) + map("i", m.top_of_line, '^i', opt) + map("i", m.end_of_line, '', opt) + vim.api.nvim_set_keymap("c", m.forward, '', {noremap = true}) + vim.api.nvim_set_keymap("c", m.backward, '', {noremap = true}) + vim.api.nvim_set_keymap("c", m.top_of_line, '', {noremap = true}) + vim.api.nvim_set_keymap("c", m.end_of_line, '', {noremap = true}) + end +end + + -- Don't copy the replaced text after pasting in visual mode map("v", "p", '"_dP', opt)