emacs-evil / evil

The extensible vi layer for Emacs.
GNU General Public License v3.0
3.36k stars 281 forks source link

Key Binding using combination of pre existing commands #1563

Open asmodeus812 opened 2 years ago

asmodeus812 commented 2 years ago

Issue type

Environment

Emacs version: 27 (doom) Operating System: Ubuntu Evil version: latest Evil installation type: MELPA

Reproduction steps

I am trying to replicate a vimrc configuration by setting up the following key combos using already existing ones bu i am not successful. I receive the following error - Error in private config: config.el, (error Key sequence y Y starts with non-prefix key y)

nnoremap yY y^
nnoremap dD d^
nnoremap cC c^

I have tried multiple options as shown below

(define-key evil-normal-state-map (kbd "yY") (kbd "y^"))
(evil-define-key 'normal 'global "yY" "y^")

Expected behavior

Evil is capable of binding to key combos like vim.

Actual behavior

Observed error - Error in private config: config.el, (error Key sequence y Y starts with non-prefix key y)

tomdl89 commented 2 years ago

Bindings from multiple keys require the first key(s) to be prefix keys (e.g. C-x is almost always a prefix key in emacs). y d and c all seem a bit like prefix keys, but actually they are not. This is because evil needs finer control over a few things than emacs' basic prefix-key functionality. The downside is that you can't easily rebind keys like you are trying to for evil operators (that's what y, d, and c actually are). Operators can be followed by text objects or motions, so you could try:

(define-key evil-motion-state-map "Y" 'evil-first-non-blank)

(or the evil-define-key equivalent) to achieve yY becoming y^ but obviously this would mean Y will always act like ^ when used as a motion. If this isn't an issue, then go for it (and the D and C versions) but if you want Y to behave in this way only when used after y then you'll have to write some elisp. I'm happy to give feedback if you give that a go.