In emacs, I want to bind C-k to kill-region if region selected; else kill-line as normal. How to configure it?
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With