Skip to content

Instantly share code, notes, and snippets.

@shadmansaleh
Last active May 19, 2021 09:00
Show Gist options
  • Save shadmansaleh/e448402b980dbf2b68b226e0d708a66d to your computer and use it in GitHub Desktop.
Save shadmansaleh/e448402b980dbf2b68b226e0d708a66d to your computer and use it in GitHub Desktop.
-- Copyright (c) 2020-2021 shadmansaleh
-- LICENCE: MIT
-- To see what chord cancelation means checkout https://github.com/svermeulen/vimpeccable#chord-cancellation-maps
-- Uses
-- require'keychord'.cancel('<leader>')
-- Add more suffixes to suffixes table if needed
local M = {}
local keys = {}
-- Table of suffixes for which chord cancelation is applied
local suffixes = {'x','s','d','c', '<esc>', ''}
local function load_keys()
keys = {}
for _, map in pairs(vim.api.nvim_get_keymap('n')) do
keys[vim.api.nvim_replace_termcodes(map.lhs, true, false, true)] = true
end
end
local function create_map(key)
for _, suffix in pairs(suffixes) do
local raw_suffix = vim.api.nvim_replace_termcodes(suffix, true, false, true)
if keys[key..raw_suffix] == nil then
vim.api.nvim_set_keymap('n', key..raw_suffix, '<nop>', {noremap=true})
keys[key .. raw_suffix] = true
end
end
end
-- Applies chord cancelation to all keymaps currently set
-- starting with prefix. prefix is a string
-- It can be any keymap start like "<leader>" or "<space>t"
function M.cancel(prefix)
load_keys()
local prefix_raw = vim.api.nvim_replace_termcodes(prefix, true, false, true)
for key, _ in pairs(keys) do
if vim.startswith(key, prefix_raw) then
for i=#key-1,#prefix_raw,-1 do
local k = key:sub(1,i)
create_map(k)
end
end
end
end
return M
@shadmansaleh
Copy link
Author

shadmansaleh commented May 19, 2021

Do you want to make it a plugin?
Just possibility for user to setup suffix and cancel() to accept table of keys (prefixes). And it is complete.

I know a plugin can be made out of this really easily.
But I currently don't have time to maintain one. You can take this and make a plugin . Just give credits to this :)

@monkoose
Copy link

Ok. Thank you again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment