Fork me on GitHub
#spacemacs
<
2019-10-31
>
johanatan17:10:40

hi, i'd like to bind the key K in evil mode to be equivalent to the key sequence: k J. what's the easy way to accomplish that?

johanatan17:10:19

[this completes the symmetry: J:j::K:k]

Jcaw18:10:07

@johanatan Would you like to bind to the target of k J directly or do you want it to emulate the keypress?

Jcaw18:10:50

You can bind sequences with (spacemacs/set-leader-keys <key> <mapping> [<key> <mapping>...])

(spacemacs/set-leader-keys 
  "a" some-command)

(spacemacs/set-leader-keys
  "b" another-command
  "c" third-command)

Jcaw18:10:21

If you want to emulate a keypress, you can actually pass (kbd <sequence>) as the target command, and it will be treated as a keyboard macro which emulates that keypress. So for example:

(spacemacs/set-leader-keys
  "K" (kbd "SPC k J"))
(This is probably not the correct key sequence.)

Jcaw18:10:58

There are other macros for mode-specific bindings. Three total:

spacemacs/set-leader-keys
spacemacs/set-leader-keys-for-minor-mode
spacemacs/set-leader-keys-for-major-mode

johanatan20:10:17

@jack.crawley92 i think emulating the keypress is fine. but is there no way to create an independent key binding without making it a "leader key". I think "leader key" in this context is a bit off the mark as a) there are no follower keys and b) we are talking about editing text in evil mode (vim commands do not require a "SPC X ..." prefix-- e.g., to "join" the current line with the next one you simply press the single key "J". I want to do the same for the single key "K" to join the current line onto the previous line.

Jcaw20:10:15

You could certainly bind it to the evil mode map too - treating it as a macro is default Emacs behaviour, it's not handled by the spacemacs macro

Jcaw20:10:37

(define-key evil-<state>-state-map (kbd "K") (kbd "k J"))

Jcaw20:10:19

I use Emacs mode, so I don't know the evil states off the top of my head.

Jcaw20:10:02

In general I think emulating is smelly code, but I use it in my config in a few places.

Jcaw20:10:08

I think there's another macro (might be a function), evil-define-key, which acts on the edit state by default - not sure though.

Jcaw21:10:50

No worries! : )

johanatan21:10:47

undo doesn't quite understand it tho. any ideas how to rectify that?

johanatan21:10:04

i.e., after pressing "K" and then "u" the cursor is left on the top line instead of the bottom one

Jcaw21:10:48

Ah, keyboard macros execute each step as an atomic command. It's probably doing that.

Jcaw21:10:09

In that case I wouldn't emulate - wrap the two commands you want to run in another command

johanatan21:10:23

ok, makes sense

Jcaw21:10:15

(defun join-to-previous (&optional prefix-arg)
  (interactive "P")
  (first-command prefix-arg)
  (second-command prefix-arg))

Jcaw21:10:37

You might have to call the individual commands interactively for them to behave properly.

johanatan21:10:38

what's the "P" argument to "interactive" ?

Jcaw21:10:49

It defines the type of prefix argument the command demands. In this case you probably actually want lowercase "p" - that's the numeric prefix argument.

Jcaw21:10:19

It's a pretty obtuse interface to be honest, I don't like it

johanatan21:10:26

oh, that's right. i never pass a prefix to "J" so didn't think of that

Jcaw21:10:09

I actually don't even know if the evil commands take prefix arguments, just assuming. 😁

johanatan21:10:16

yea, it does. i just tried it

Jcaw21:10:24

Nice 👍