1

I would like to do the equivalent of this

vnoremap p "_dP

in evil-mode. (i.e. I would like paste-replacing to not yank). I want to do the same for c C and P.

I tried this:

(define-key evil-visual-state-map (kbd "p") (kbd "\"_dP"))

but it just deletes the selection without pasting.

Is there any way to do this?

4

Emacs has a different concept than Vim. The "emacs way" is to create functions and bind them, which is generally more flexible and readable than vim's map approach. You can create the command for what you need and bind it:

(defun delete-selection-and-paste ()
  (interactive)
  (delete-region (region-beginning) (region-end))
  (yank))

(define-key evil-visual-state-map (kbd "p") 'delete-selection-and-paste)
1

In the end, I just looked at the evil source and modified some functions to always pass in ?_ (the black hole register). My .emacs looks like this now:

(use-package evil
  :ensure t
  :config
  (evil-mode 1)
  (key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
  (defun delete-selection-and-paste ()
    (interactive)
    (delete-region (region-beginning) (region-end))
    (yank))
  (evil-define-operator evil-change-line-no-yank (beg end type register yank-handler)
    "Change to end of line without yanking."
    :motion evil-end-of-line
    (interactive "<R><x><y>")
    (evil-change beg end type ?_ yank-handler #'evil-delete-line))
  (evil-define-operator evil-change-no-yank (beg end type register yank-handler)
    "Change without yanking."
    (evil-change beg end type ?_ yank-handler))
  (evil-define-operator evil-change-whole-line-no-yank (beg end type register yank-handler)
    :motion evil-line
    (interactive "<R><x>")
    (evil-change beg end type ?_ yank-handler #'evil-delete-whole-line))

  (define-key evil-visual-state-map (kbd "p") 'delete-selection-and-paste)
  (define-key evil-normal-state-map (kbd "C") 'evil-change-line-no-yank)
  (define-key evil-normal-state-map (kbd "c") 'evil-change-no-yank)
  (define-key evil-visual-state-map (kbd "c") 'evil-change-no-yank)
  (define-key evil-visual-state-map (kbd "S") 'evil-change-whole-line-no-yank)
  (modify-syntax-entry ?_ "w"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.