-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working example for neovim (custom text object with repeat) #92
Comments
Great, how to do this with custom key maping? |
A general recipe & usage for neovim (Lua)You can use the following wrapper to conveniently wrap a ---Register a global internal keymap that wraps `rhs` to be repeatable.
---@param mode string|table keymap mode, see vim.keymap.set()
---@param lhs string lhs of the internal keymap to be created, should be in the form `<Plug>(...)`
---@param rhs string|function rhs of the keymap, see vim.keymap.set()
---@return string The name of a registered internal `<Plug>(name)` keymap. Make sure you use { remap = true }.
local make_repeatable_keymap = function (mode, lhs, rhs)
vim.validate {
mode = { mode, { 'string', 'table' } },
rhs = { rhs, { 'string', 'function' },
lhs = { name = 'string' } }
}
if not vim.startswith(lhs, "<Plug>") then
error("`lhs` should start with `<Plug>`, given: " .. lhs)
end
vim.keymap.set(mode, lhs, function()
rhs()
vim.fn['repeat#set'](vim.api.nvim_replace_termcodes(lhs, true, true, true))
end)
return lhs
end (Note: this does not implement Example: If you want to make a keymap vim.keymap.set('n', '<leader>tc', make_repeatable_keymap('n', '<Plug>(ToggleComment)', function()
-- the actual body (rhs) goes here: some complex logic that you want to repeat
end, { remap = true }) This will:
To see a working example in the wild, please see my dotfiles: wookayin/dotfiles@3fe6e68. See how complicated it was to write things in vimscript with dirty string manipulation; in Lua, functional programming makes config more concise! More detailed explanation and breakdownSo in order to make vim-repeat work, in general one should have a keymap (or a command) like:
For keymap, it is very recommended (although not mandatory) to use |
I had trouble to make vim-repeat work in neovim with lua, but finally here it is:
t() function is just nvim helper:
All you have to do is to write your custom : get_big_word('i') - function - in my case I wanted to write custom big WORD text object.
I guess it should work for any custom lua functions, not just
o
.Hopefully people will find this useful.
The text was updated successfully, but these errors were encountered: