Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind C-k to kill-region if region selected; else kill-line

Tags:

emacs

In emacs, I want to bind C-k to kill-region if region selected; else kill-line as normal. How to configure it?

like image 347
Mingo Avatar asked Jan 21 '12 19:01

Mingo


Video Answer


2 Answers

Put this in your .emacs

(defun kill-line-or-region () 
 "kill region if active only or kill line normally"
  (interactive)
  (if (region-active-p)
      (call-interactively 'kill-region)
    (call-interactively 'kill-line)))

  (global-set-key (kbd "C-k") 'kill-line-or-region)
like image 77
kindahero Avatar answered Oct 06 '22 01:10

kindahero


This sounds like a job for advice!

(defadvice kill-line (around kill-region-if-active activate)
  (if (and (called-interactively-p) (region-active-p))
      (kill-region (region-beginning) (region-end))
    ad-do-it))

EDIT: Added called-interactively-p check.

like image 35
Sean Avatar answered Oct 06 '22 00:10

Sean