Change the behavior of the inverse and back-kill words

I have a little problem with the way the backward-word and backward-kill-word commands work. When I click backward-kill-word on a line that represents only white spaces (for example, the first char of the line to be indented), the command will kill all the white space, as well as the last word of the previous line.

This behavior is completely unintuitive for me. I prefer, for example, that it works in Eclipse, which will kill whitespace before the start of the line the first time you press "backward-kill-word", it will move to the end of the previous line the next time you press, and only then will the word wipe from the end of the line.

I am sure that this behavior is used by default in most applications (since it seems more intuitive, probably because I'm used to it, but I'm not sure), so I wonder if there is a way to configure Emacs for this behavior. Several searches on Google, unfortunately, showed nothing.

thanks

EDIT:

Thanks to everyone for the answers (including the elisp code that does what I requested).

The same problem arises, obviously, with the forward-kill-word command and move commands, and I was hoping that emacs has only some custom flag to change the behavior, but it seems like I'm just going to these elisp functions and reordering the default commands and killing teams for these.

+4
source share
6 answers

How about this:

(defun my-backward-kill-word () "Kill words backward my way." (interactive) (if (bolp) (backward-delete-char 1) (if (string-match "^\\s-+$" (buffer-substring (point-at-bol) (point))) (kill-region (point-at-bol) (point)) (backward-kill-word 1)))) 
+5
source

In this case, you can try M - \ (delete-horizontal-space), which does what you want for the first keystroke. And if you really need a function that combines the two, it should not be too difficult to write one that checks if there are any non-spatial characters in the current line before the cursor and name one or the other based on this.

+1
source

This is a pretty big question. You will have to think about emacs lisp programming and learn something about it. I suggest starting here: rattlesnake emacs tutorial and work your way through this.

For your information only, looking at my TAGS file, I say that the opposite word is defined in simple.el and has the following form:

(defun backward-word (& optional arg)

"Move back until you meet the beginning of the word. With argument, do it many times."

(interactive "p")

(forward word (- (or arg 1))))

so the next step is to add an if for this call.

+1
source

This should do it:

 (defun eclipse-kill-word (repeat) "Redefine `backward-kill-word' to work as Eclipse does. Now stops at the beginning of the line, deleting only whitespace." (interactive "p") (let (cnt) (dotimes (cnt repeat) (if (= (point) (save-excursion (beginning-of-line) (point))) (kill-region (point) (save-excursion (backward-word) (point))) (kill-region (point) (max (save-excursion (beginning-of-line) (point)) (save-excursion (backward-word) (point)))))))) 

Does repeating with the prefix key (Ctrl-U) have the added benefit.

+1
source

Please note that in cc-mode there is an option for starving deletion, and you can enable it like this:

(add-hook 'c-mode-common-hook (lambda () (c-toggle-auto-hungry-state 1)))

Obviously, this applies only to C, C ++, Java, ... files and applies to the backspace key (not the exception of words).

0
source

You can reuse some functions from the viper to get the desired behavior:

 (setq viper-mode nil) (require 'viper) (defun viper-delete-word (arg) "Delete word." (interactive "p") (save-excursion (push-mark nil t) (viper-forward-word arg) (delete-region (point) (mark t)) (pop-mark))) (defun viper-delete-backward-word (arg) "Delete previous word." (interactive "p") (save-excursion (push-mark nil t) (viper-backward-word arg) (delete-region (point) (mark t)) (pop-mark))) (global-set-key [C-left] 'viper-backward-word) (global-set-key [C-right] 'viper-forward-word) (global-set-key [C-delete] 'viper-delete-word) (global-set-key [C-backspace] 'viper-delete-backward-word) 
0
source

All Articles