emacs-evil/evil-cleverparens

Keybindings go crazy in terminal

Closed this issue ยท 10 comments

When running emacs in a terminal, evil-cleverparens does something extra-funky to the keybindings.

Steps:

  • run emacs in terminal mode (I've tried iTerm and terminal on OSX)
  • with evil and cleverparens mode on, move around the buffer using the arrow keys
  • feel slightly guilty for not using hjkl, but then shrug it off and move on
  • press v to enter visual mode
  • exit visual mode
  • press the up arrow

Result:

  • chaos, cats and dogs living together...
  • also, the up arrow appears to be interpreted now as M-O followd by A', with the result that you end up in insert mode, on a new line above the current function, with a capital letterA`.
  • some other bindings do weird things too - the arrow keys and the mouse-wheel are the ones I have noticed

Turning off cleverparens mode restores the normal functioning of the arrow keys. Also, as noted above, the weirdness does not start until you enter and then exit visual mode.

In case it matters, I see this behavior in Spacemacs.

snoe commented

I'm seeing this too, makes it unusable for me. It's too bad since it's a blessed spacemacs layer.

chaos, cats and dogs living together...

That dosen't help in debugging the issue at all. What exactly is happening?

I can't seem to reproduce. I'm getting a bit of weird behavior regarding things being wrapped in square brackets though (and getting dumped in insert state), which is seemed to be caused by these bindings. You could try to remove those bindings, perhaps, if you don't use them.

Hi,

Happens only when evil-cleverparens-use-additional-bindings is not set to nil, without that it's fine for me.

evil-cleverparens-use-additional-bindings is heavy on M- bindings, by default that's Alt on my system (OSX), some Alt-key escape sequences in terminals have the same codes as movement u/d/l/r arrow keys, I think that's where the problem lies.

Saw similar problems in Vim or Tmux also in the past, it's nothing Emacs or evil-cleverparens specific. Usually there is no clean and universal way around it but remapping to other keys.

I'll try to investigate it a bit more and get back if I figure-out, for the time being it's best to remap those keys if u need to use those movements.

Hope this helps.

Hmm, I still can't seem to reproduce. I tried M-d/M-l and they seemed to work the same both inside and outside of the terminal. I use urxvt though, and iterm isn't available on my system, so I can't test that.

The M-u/d/l/r/etc are provided by a library called 'readline' in bash, and isn't present when not typing into bash directly. I think that the problem isn't related to that directly.

That said, I don't use emacs in a terminal 'normally' so I might be missing a case that comes up when you use it more heavily.

Also, you can use window-system or display-graphic-p to determine if you are in a terminal or not, if you find a fix to this issue.

I fixed this in doom emacs (w/ emacs 27, assoc-delete-all is new) using the following code:

;; disable additional bindings so they aren't bound when the package loads
(setq evil-cleverparens-use-additional-bindings nil)

;; after the package loads we do the following:
(after! evil-cleverparens
  ;; turn on the "additional-bindings" so that when we call `evil-cp-set-additional-bindings` it will bind keys
  (setq evil-cleverparens-use-additional-bindings t)
  (unless window-system
    ;; when we're in the terminal, delete the bindings for M-[ and M-] from the alist of additional bindings
    (setq evil-cp-additional-bindings (assoc-delete-all "M-[" evil-cp-additional-bindings))
    (setq evil-cp-additional-bindings (assoc-delete-all "M-]" evil-cp-additional-bindings)))
  ;; bind all the keys listed in evil-cp-additional-bindings
  (evil-cp-set-additional-bindings))

You can find alternatives to assoc-delete-all here.

If you're using something like use-package it might look like:

(use-package evil-cleverparens
  :init
  (setq evil-cleverparens-use-additional-bindings nil)
  :config
  (setq evil-cleverparens-use-additional-bindings t)
  (unless window-system
    (setq evil-cp-additional-bindings (assoc-delete-all "M-[" evil-cp-additional-bindings))
    (setq evil-cp-additional-bindings (assoc-delete-all "M-]" evil-cp-additional-bindings)))
  (evil-cp-set-additional-bindings))

And you could also add them back in under different keys using something like this before you call evil-cp-set-additional-bindings:

(add-to-list 'evil-cp-additional-bindings '("M-b" . evil-cp-wrap-next-square))
(add-to-list 'evil-cp-additional-bindings '("M-B" . evil-cp-wrap-previous-square))

Thank you so much! It was driving me crazy!!!

gdw2 commented

Whenever my emacs terminal would loose focus, I'd get an extra set of square brackets around my next s-expr. Adding the following to my spacemacs config worked great: #58 (comment)

Thank you @corasaurus-hex !

Bit late, but I've implemented a fix. lmk here / reopen if you observe otherwise.