I am trying to use different keybindings for basic movement and deletion in emacs. What I would like to use are the following keybindings:
Which I managed to make work in most of the cases. Nevertheless I still face two problems:
So my question is, how can I create a minor mode, containing all my keybindings, that would precede all other mode's keybindings, and that would do something like:
C-n : "please bind C-k (my preference for down movement) to whatever command C-n was meant to be bound to in this mode"
I guess I have to create a minor mode for this, maybe have to load it through a hook in before each major mode, and use some emacs function that returns the function bound to a given keybinding.
Any idea on how to do that?
First, you should probably take a look at evil mode, which brings a lot of Vim-style keyboard shortcuts to Emacs, many of which mirror your own preferences.
If you still want a universal minor mode that will dynamically rebind functions from any given major or minor mode, you'll probably want to work with functions like find-function-on-key to sniff out bindings which you can then rebind as needed.
A less ambitious approach may be to define a personal keymap and bind that to one key. That saves you having to rebind keys everytime you switch modes. For example, I use the following:
(define-prefix-command 'ty-keymap)
(global-set-key "\M- " ty-keymap)
(define-key ty-keymap " " 'just-one-space)
This rebinds M-<space> for my own use, then rebinds just-one-space to M-<space><space>. This frees me up to do weird things like this:
(define-key ty-keymap "j" #'(lambda () (interactive) (ty-move-mode ?j)))
(define-key ty-keymap ";" #'(lambda () (interactive) (ty-move-mode ?\;)))
(define-key ty-keymap "k" #'(lambda () (interactive) (ty-move-mode ?k)))
(define-key ty-keymap "l" #'(lambda () (interactive) (ty-move-mode ?l)))
(defun ty-move-mode (mv)
"Move over windows with right homerow keys."
(interactive "k")
(case mv
(?j (windmove-left)
(ty-move-mode (read-event)))
(?; (windmove-right)
(ty-move-mode (read-event)))
(?k (windmove-down)
(ty-move-mode (read-event)))
(?l (windmove-up)
(ty-move-mode (read-event)))
(?\r (message "done!"))
(t (push last-input-event unread-command-events))))
This gives me access to j, k, l and ; for moving directionally around my windows. It would be easy enough to modify this to give you a mini-mode for movement by single characters:
(defun ty-move-mode (mv)
"Move over windows with right homerow keys."
(interactive "k")
(case mv
(?j (backward-char 1)
(ty-move-mode (read-event)))
(?; (forward-char 1)
(ty-move-mode (read-event)))
(?k (previous-line 1)
(ty-move-mode (read-event)))
(?l (next-line 1)
(ty-move-mode (read-event)))
(?\r (message "done!"))
(t (push last-input-event unread-command-events))))
Perhaps someone else will have a better idea of how to do exactly what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With