noctuid/general.el

Same <leader> key in normal and insert states

Closed this issue · 4 comments

For now we can give a leader key a modified insert state version, like this:

(general-create-definer tyrant-def
  :states '(normal insert emacs)
  :prefix "SPC"
  :non-normal-prefix "M-SPC")

But I really want to use the same leader key in both normal and insert state, so I tried

(general-create-definer localleader
  :states '(normal insert)
  :prefix "SPC"
  :non-normal-prefix "SPC")
  (localleader
    "y" 'counsel-yank-pop)

which wont work, in insert state, it just input a space and a ascii letter y, so i added this:

  (general-def 'insert
    :keymaps 'global
    "SPC" (general-key-dispatch 'self-insert-command
            :timeout 0.2
            "y" 'counsel-yank-pop))

for :keymap which is not 'global, it worked. for :keymap is 'global, it still wont work.
BUT, after i mannually C-x C-e to eval those code, it worked, so i change 'global to 'override, which solved the problem...
also tried this method:

(general-nmap :prefix "SPC"
              :prefix-command 'killring
            "y" 'counsel-yank-pop)

(general-imap "SPC"
              (general-key-dispatch 'self-insert-command
                :timeout 0.15
                :inherit-keymap killring))

similar problem, if keymap is global, in insert it will just input what i type, in normal state it will warn me "SPC y" is undifined, if keymap is some specific mode map, it work.

So , using 'override for gloabl keybing is the anwser, but I wonder if there are any more consistent solution.

I think I find my anser.

For the global key bidings require a leader key , that leader key shouldnt be used for a specific keymap, then bind the insert mirror with general-key-dispatch and no 'override needed.

That should be enough for most sitiuation, but i still dont understand why a leader key used in a specific keymap wont work properly in global key map...

I give up, 🤣 .
After install perspective.el, without even touch the key binding config , the leader key in insert mode wont work anymore...
All my leader key related config is here

(general-create-definer leader
:states 'normal
:prefix ",")

(general-create-definer localleader
:states 'normal
:prefix "SPC"
)
(leader
   "f" 'counsel-find-file
   "w" 'save-buffer
   "b" 'ibuffer
   "y" 'counsel-yank-pop)
  (general-def 'insert
    "," (general-key-dispatch 'self-insert-command
          :timeout 0.15
          "y" 'counsel-yank-pop))
(localleader
    :states 'normal
    :keymaps 'org-mode-map
    "s" 'org-edit-special
    )
  (general-def 'insert
    :keymaps 'org-mode-map
    "SPC" (general-key-dispatch 'self-insert-command
            "s" 'org-edit-special))
(localleader
  :states 'normal
  :keymaps 'LaTeX-mode-map
  "c" 'TeX-command-run-all
  "v" 'TeX-view
  )

In your example

(general-def 'insert
  :keymaps 'global
  "SPC" (general-key-dispatch 'self-insert-command
          :timeout 0.2
          "y" 'counsel-yank-pop))

You are setting the :keymaps argument twice (to insert and then to global). If there is only one positional argument to general-def, it is considered a keymap. This works fine for me ('insert 'global is the same as just 'insert):

(general-def 'insert
  "SPC" (general-key-dispatch 'self-insert-command
          :timeout 0.2
          "y" 'counsel-yank-pop))

And if you want to bind a key for a specific mode:

(general-def 'insert org-mode-map
  "SPC" (general-key-dispatch 'self-insert-command
          :timeout 0.2
          "s" 'org-edit-special))

Does this solve your issue?

Thanks! that solve all the problem. Didn't realized 'insert is a keymap, just switch to emacs a week ago , should look into the manual more carefully 🤣