which-key.nvim icon indicating copy to clipboard operation
which-key.nvim copied to clipboard

Trigger marks list with remapped jump keys (`'` and '`')

Open ZeroKnight opened this issue 2 years ago • 4 comments

Hi there,

I have these two mappings that swap the "meaning" of the mark jump keys, as it makes more sense to me:

noremap ` '
noremap ' `

Understandably, this conflicts with WhichKey's mapping of these keys and so the marks list is not shown when I press ' or `. I tried to mimic what WhichKey sets (but reversed) like so:

noremap ' <Cmd>lua require("which-key").show("`", {mode = "n", auto = true})<CR>
noremap ` <Cmd>lua require("which-key").show("'", {mode = "n", auto = true})<CR>

While the marks list does appear, it doesn't fully work since both keys will perform the same action for some reason, i.e. both will behave like '.

What would be the best way to keep my mark jump keys reversed but still show WhichKey's marks list? Have I overlooked something, or is a workaround needed?

ZeroKnight avatar Sep 06 '21 01:09 ZeroKnight

I have the same issue. I tried the which-key show mappings above and it seems to work fine if I enter them manually after starting nvim up, but when I try to add them to the end of my which-key 'config' (packer key) or even add them to after/plugin/blah.vim, somehow, the normal mode mapping is being overridden by which key. I don't really understand that because ': scriptnames' clearly shows after/plugin/blah.vim being loaded AFTER which key. There must be some kind of lazy behavior I don't understand there. Anyone have any ideas?

cpakkala avatar Feb 21 '23 02:02 cpakkala

Ugg, I see, the call to show(), is remapping the keys. I don't see any good solution outside of making the two mark keys configurable in which-key.

cpakkala avatar Feb 21 '23 04:02 cpakkala

Remapping just ' works, swapping ' and ` is when things seem to break.

vim.api.nvim_set_keymap('n', "'", '<Cmd>lua require("which-key").show("`", { mode = "n" })<CR>', { silent = true })

net avatar Jan 08 '24 21:01 net

Remapping just ' works, swapping ' and ` is when things seem to break.

vim.api.nvim_set_keymap('n', "'", '<Cmd>lua require("which-key").show("`", { mode = "n" })<CR>', { silent = true })

As I said above, the "show" function is remapping the keys every time it's called; so your original mapping commands do work, but as soon as you press one of those keys, "show" remaps them again to their defaults. I guess it was assumed no one would want to override these. My solution was to fork the code and:

diff --git a/lua/which-key/plugins/marks.lua b/lua/which-key/plugins/marks.lua
index b8be1c8..4897463 100644
--- a/lua/which-key/plugins/marks.lua
+++ b/lua/which-key/plugins/marks.lua
@@ -16,8 +16,8 @@ local labels = {
   ["."] = "Last change in current buffer",
   ['"'] = "Last exited current buffer",
   ["0"] = "In last file edited",
-  ["'"] = "Back to line in current buffer where jumped from",
-  ["`"] = "Back to position in current buffer where jumped from",
+  ["`"] = "Back to line in current buffer where jumped from",
+  ["'"] = "Back to position in current buffer where jumped from",
   ["["] = "To beginning of previously changed or yanked text",
   ["]"] = "To end of previously changed or yanked text",
   ["<lt>"] = "To beginning of last visual selection",
@@ -42,6 +42,11 @@ function M.run(_trigger, _mode, buf)
     if key == "<" then
       key = "<lt>"
     end
+    if key == "'" then
+      key = "`"
+    elseif key == "`" then
+      key = "'"
+    end
     local lnum = mark.pos[2]

     local line

This solution is far from ideal, as I don't want to keep integrating upstream changes; so I'm just keeping with this old verison...

cpakkala avatar Jan 08 '24 22:01 cpakkala

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jul 07 '24 01:07 github-actions[bot]