views:

236

answers:

4

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.

+1  A: 

Press C-Space (set-mark-command) just before C-w to set the mark at the cursor position so that there is no text between the mark and cursor position to kill.

Steven
So before accidentally pressing C-w, one should accidentally press C-space?
Beta
+5  A: 
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.

Harold L
+8  A: 

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)
scottfrazer
+5  A: 

Set the variable mark-even-if-inactive to nil. That way, you can only kill text if it's actually highlighted.

legoscia