0

kill-word and backward-kill-word deletes too much text:

foo(bar)
    <point>

Now if I press C-<backspace>:

foo(<point>

What I want is:

foo(bar)<point>

after pressing C-<backspace>, just like how the other editors do.

EDIT: Based on the answer from @gigiair, I'm trying to implement the command sequency in elisp, but I'm getting "Wrong number of arguments" when executing the code:

(defun leo/backward-kill-word ()
  "Better backward-kill-word."
  (interactive)
  (set-mark-command)
  (backward-sexp)
  (forward-sexp)
  (backward-delete-char-untabify))

I'm new to elisp, know nothing about.

4
  • What other editors? E.g. vim does not do that when I use C-<backspace> either in insert or in edit mode.
    – NickD
    Oct 26 '21 at 18:45
  • set-mark-command takes an argument (see its doc string with C-h f set-mark-command). *BUT* you should *not* use set-mark-command` (or set-mark) from lisp: it's supposed to be used interactively. See the doc string of set-mark with C-h f set-mark.
    – NickD
    Oct 26 '21 at 22:21
  • And BTW, backward-delete-char-untabify also takes an argument, but you probably shouldn't be using that either. Using delete-region (or kill-region depending on whether you do not or you do want to keep the contents of the region in the kill-ring) as in the answer below is a much better way. But I don't think that the sexp motion commands are appropriate for this problem.
    – NickD
    Oct 26 '21 at 23:52
0

This sequence does it :

 C-SPC                      ;; set-mark-command
 C-M-b                      ;; backward-sexp
 C-M-f                      ;; forward-sexp
 <backspace>                ;; backward-delete-char-untabify

This function executes this code :

(defun leo/backward-kill-word ()
  "Better backward-kill-word."
  (interactive)
  (let((beg (point)))
      (backward-sexp)
      (forward-sexp)
      (delete-region beg (point))))

This function is probably better

(defun leo/backward-kill-word ()
      "Better backward-kill-word."
      (interactive)
      (fixup-whitespace)
      (backward-delete-char-untabify 1))
3
  • I'm trying to create a command for this, but I'm getting "Wrong number of arguments" when I execute. See the elisp code I added to my question. I'm new to elisp. Oct 26 '21 at 20:17
  • The sexp commands are probably a bad idea: they move the cursor across a "balanced" expression. Try getting rid of the opening paren in the OP's test and see what happens. It is better to acknowledge first that what the OP wants is not a "better" backward-kill-word but a different function altogether, one that deletes (or perhaps kills) whitspace backwards until something happens (the OP has not made clear what that is: the example seems to indicate that he wants to reach the first non-whitespace character, but the title of the question says differently).
    – NickD
    Oct 26 '21 at 22:37
  • @maverick1013 read the recommendation for novice in (describe-function 'set-mark-command)
    – gigiair
    Oct 26 '21 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.