In vim, you can :set nojoinspaces
to prevent J
from adding a space when joining lines that end in a period. How do I set this in evil mode? I'm using Doom emacs, if that is relevant.
2 Answers
What you need is the function evil-join-whitespace
, and a key binding
for that. In normal Emacs + evil, this is g J
.
In your case, you need to add a suitable key, e.g C-c J
, with some code
in your ~/doom.d/config.el
or where the config.el
file is:
(evil-define-key* 'normal 'global
(kbd "C-c J" #'evil-join-whitespace))
;; or this:
(after! evil
(map! :n "C-c J" #'evil-join-whitespace))
See the files ~/.emacs.d/docs/faq.org
and ~/.emacs.d/docs/examples.org
.
NB: not tested.
-
Thanks! I've just mapped
J
toevil-join-whitespace
, and that works for me. Commented Oct 11, 2023 at 10:21
Although I am unable to reproduce the behavior (of evil-join
adding an extra space after a period), here is a solution that should emulate vim's nojoinspace
behavior.
Inspecting the function evil-join
we find that it uses the function join-line
without adding the option for 'nojoinspaces functionality'. Also, join-line
(which is an alias for delete-indentation
) does not offer such functionality. However, I would say such functionality can be added by advising either evil-join
or join-line
.
It is not straightforward to create a 'succesful' advice for evil-join
or join-line
when applying it to multiple lines. Luckily, when applying evil-join
to multiple lines, it simply applies join-line
to a single line multiple times. Therefore, it is easiest to simply advise join-line
e.g. as follows
(defun evil-nojoinspaces-ad (&rest _)
(when (looking-back "\\.")
(delete-char 1)))
(advice-add 'delete-indentation :after #'evil-nojoinspaces-ad)
-
I was not aware of the
evil-join-whitespace
command of @Ian's answer. Thanks @Ian Commented Oct 10, 2023 at 17:39