b0o / mapx.nvim Goto Github PK
View Code? Open in Web Editor NEW๐บ A better way to create key mappings in Neovim
License: MIT License
๐บ A better way to create key mappings in Neovim
License: MIT License
I'm looking for a quick way to execute vim commands without writing functions, like so.
nnoremap([[keymap]], {Command1, Command2, Command3}, silent)
So if the second function in nnoremap
is a table, each command in the table can be called using vim.fn.execute
or similar.
Is something like this feasible or available, without using Lua functions?
For example, this currently does not work:
require'mapx'.map("<leader>t", setmetatable({}, { __call = function() print"test" end }))
This would dramatically simplify the code by obviating the need for direct coupling with WhichKey.
This can be partially implemented immediately by passing the desc
option to nvim_set_keymap()
, but the removal of WhichKey-specific code depends on folke/which-key.nvim#253 / folke/which-key.nvim#242.
This should simplify the code a bit and likely improve performance.
Prevent packer.nvim
from PackerCompile
twice.
packer.compile: Complete
packer.nvim: Error running config for mapx.nvim: [string "..."]:0: attempt to call field 'setup' (a boolean value)
It's annoying as I have to delete the plugin/packer_compiled.lua
manually.
Have you thought about contributing this to nvim ?
I would love to see something like this be default!
map({"i", "s"}, '<c-e>', function() ls.expand_or_jump() end())
Important information will be announced here, such as new major versions and breaking changes. Please subscribe to this issue to receive a notification when there's a new announcement.
Hello, I really like the idea of this plugin and have something similar implemented in my dotfiles:
local escape_cmd = function(string)
return string:gsub("'", "\\'"):gsub('"', '\\"'):gsub('\\[', '\\['):gsub('\\]', '\\]'):gsub('<', '<lt>')
end
local bounded_funcs = 'global_bounded_funcs_namespace'
_G[bounded_funcs] = {}
for _, mode in ipairs { '', 'n', 'i', 'v', 't', 's' } do
_G[mode .. 'noremap'] = function(input, output)
if type(output) == 'function' then
local func_name = mode .. '_' .. input
local func_name_escaped = escape_cmd(func_name)
_G[bounded_funcs][func_name] = output
local lua_cmd = ':lua ' .. bounded_funcs .. "['" .. func_name_escaped .. "']()<cr>"
vim.api.nvim_set_keymap(mode, input, lua_cmd, { noremap = true, silent = true })
elseif type(output) == 'string' then
vim.api.nvim_set_keymap(mode, input, output, { noremap = true, silent = true })
else
error(mode .. 'noremap' .. ' expects a string or callback', 2)
end
end
end
This generates the [nivts]noremap family of functions which have two arguments. The first argument is the input keybinding and the second is either a string which can be a vim command OR it can be lua callback. This allows me to make the following types of mappings:
nnoremap('<leader>a', ':echo "using lua string"')
nnoremap('<leader>b', function() print('using anonymous function') end)
my_func = function() print('using named function') end
nnoremap('<leader>c', my_func)
I tried switching to mapx.nvim, mainly for the which-key.nvim integration and while example 1 and 2 work I'm not able to bind named functions. I know I can wrap the functions in an anonymous function but I'm just wondering why it doesn't work and what mistake I'm making.
I have the following code to be converted.
local escape = function(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
-- HACK
function _G.enhance_align()
if not packer_plugins["vim-easy-align"].loaded then
vim.cmd [[packadd vim-easy-align]]
end
return escape "<Plug>(EasyAlign)"
end
function M.setup_easy_align()
vim.api.nvim_set_keymap("n", "ga", "v:lua.enhance_align()", { expr = true })
vim.api.nvim_set_keymap("x", "ga", "v:lua.enhance_align()", { expr = true })
end
First I tried the following, which doesn't work
mapx.nmap("ga", "v:lua.enhance_align()", mapx.expr)
mapx.xmap("ga", "v:lua.enhance_align()", mapx.expr)
Then I make it work using the following
mapx.nmap("ga", enhance_align, mapx.expr)
mapx.xmap("ga", enhance_align, mapx.expr)
I am not sure whether this is a bug.
Hi, when enable the whichkey integration, the following works
mapx.nmap("]t", '<cmd>lua require("trouble").next({skip_groups = true, jump = true})<CR>')
mapx.nmap("[t", '<cmd>lua require("trouble").previous({skip_groups = true, jump = true})<CR>')
while the following doesn't
mapx.nmap("]t", '<cmd>lua require("trouble").next({skip_groups = true, jump = true})<CR>', "Next Trouble")
mapx.nmap("[t", '<cmd>lua require("trouble").previous({skip_groups = true, jump = true})<CR>', "Previous Trouble" )
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.