Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ctrl-backspace in emacs deletes too much

Tags:

emacs

It's good that ctrl-backspace in emacs will delete all whitespace. However, it doesn't stop there! It seems to only stop after it's deleted at least one word. This means, for example, that using it here:

foo(bar)
    <cursor>

Results in

foo(<cursor>

Which is really dumb (IMHO)! The behavior I would want is something akin to the following:

  1. if there's whitespace preceding the cursor, delete all of the whitespace (then stop!).
  2. If there's a word preceding the cursor, delete the word.
  3. Otherwise, delete all adjacent repetitions of whatever character precedes the cursor.

This seems like a much more reasonable Ctrl-Backspace, but honestly, if I could just get (1), it would be a huge improvement. Is there a package for this, or a setting? I don't really know emacs lisp but maybe pointing me to where the relevant APIs are...

like image 962
limp_chimp Avatar asked Jan 29 '15 17:01

limp_chimp


1 Answers

I use following intellij-style smart backward-kill-word

(defun aborn/backward-kill-word ()
  "Customize/Smart backward-kill-word."
  (interactive)
  (let* ((cp (point))
         (backword)
         (end)
         (space-pos)
         (backword-char (if (bobp)
                            ""           ;; cursor in begin of buffer
                          (buffer-substring cp (- cp 1)))))
    (if (equal (length backword-char) (string-width backword-char))
        (progn
          (save-excursion
            (setq backword (buffer-substring (point) (progn (forward-word -1) (point)))))
          (setq ab/debug backword)
          (save-excursion
            (when (and backword          ;; when backword contains space
                       (s-contains? " " backword))
              (setq space-pos (ignore-errors (search-backward " ")))))
          (save-excursion
            (let* ((pos (ignore-errors (search-backward-regexp "\n")))
                   (substr (when pos (buffer-substring pos cp))))
              (when (or (and substr (s-blank? (s-trim substr)))
                        (s-contains? "\n" backword))
                (setq end pos))))
          (if end
              (kill-region cp end)
            (if space-pos
                (kill-region cp space-pos)
              (backward-kill-word 1))))
      (kill-region cp (- cp 1)))         ;; word is non-english word
    ))

(global-set-key  [C-backspace]
            'aborn/backward-kill-word)
like image 173
Aborn Jiang Avatar answered Sep 29 '22 01:09

Aborn Jiang