How to associate Ck with a kill region if a region is selected; else kill-line

In emacs, I want to associate Ck with a kill region if a region is selected; otherwise kill-line as usual. How to configure it?

+7
source share
3 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 "Ck") 'kill-line-or-region) 
+12
source

It seems to work 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 check called-interactively-p .

+5
source

This is not a direct answer, but a few tips.

You can enable (delete-selection-mode 1) with (delete-selection-mode 1) . Then, if the area is marked, when you start typing, the region will be deleted. But in your case, you can just use Cw to cut out the current region, this will add the region to kill-ring, but kill-line will do it too.

0
source

All Articles