這些是我要定義的一些功能:
local function nmap(lhs, rhs, opts)
keymap.set("n", lhs, rhs, opts)
end
local function imap(lhs, rhs, opts)
keymap.set("i", lhs, rhs, opts)
end
local function vmap(lhs, rhs, opts)
keymap.set("v", lhs, rhs, opts)
end
local function cmap(lhs, rhs, opts)
keymap.set("c", lhs, rhs, opts)
end
local function omap(lhs, rhs, opts)
keymap.set("o", lhs, rhs, opts)
end
雖然是重復的。有沒有更有效的方法來定義這些函式?這些函式中唯一不同的是一個字母(n、i、v、c、o)。我可以使用 for 回圈來自動定義每個回圈嗎?
uj5u.com熱心網友回復:
使用 _G (全域環境表)你可以做到這一點。
local letters = {'n', 'i', 'v', 'c', 'o'}
for _, c in ipairs(letters) do
_G[c..'map'] = function(...)
keymap.set(c, ...)
end
end
-- cmap(insert, arguments, here)
這實際上在本指南下也有說明
https://github.com/nanotee/nvim-lua-guide
請參閱該The vim namespace
部分
請注意不要覆寫任何重要變數(例如表格或數學庫哈哈):)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/505812.html