Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid accidentally killing text in emacs?

I have a highlighting mode (forget what it's called) that shows me, through highlighting the text, what region I have selected between mark and point. this comes in very handy when killing regions. However, sometimes, even when no text is visibly highlighted but i accidentally press C-w it still occasionally kills some text. My question is how do i stop this behaviour? I only want it to kill text IF i've highlighted it.

like image 381
horseyguy Avatar asked Nov 30 '22 19:11

horseyguy


2 Answers

Define your own function and override the key-binding:

(defun my-kill-region ()
  (interactive)
  (if (region-active-p)
      (call-interactively 'kill-region)
    (message "Region not active, didn't kill")))

(global-set-key (kbd "C-w") 'my-kill-region)
like image 197
scottfrazer Avatar answered Dec 06 '22 23:12

scottfrazer


M-x transient-mark-mode

will keep the region highlighted even after you start typing, and when you use C-Space to set mark. Then you'll see what C-w will kill.

like image 27
Harold L Avatar answered Dec 06 '22 23:12

Harold L