Search code examples
intellij-ideaemacsbackspace

Emacs backspace at beginning of tabbed line similar to intellij?


In intellij, when you're at a tab indent of let's say 4, at the beginning of a new line, and you press backspace, it backspaces through all of the preceding whitespace and goes to the tab indent of 4 at the previous line.

In emacs, in the same situation, it backspace through a single space.

How do I make emacs behave like intelij's backspace system? Is there a plugin for this?

Here's an example of what happens when you hit backspace in intelij: enter image description here

enter image description here


Solution

  • I believe this is close to what you want:

    (defun my-backspace ()
      (interactive)
      (let* ((end (save-excursion
                    (end-of-line)
                    (point)))
             (beginning (save-excursion
                          (beginning-of-line)
                          (point))))
        (if (string-match "^[ \t]*$" (buffer-substring beginning end))
            (progn
              (beginning-of-line)
              (kill-line)
              (previous-line)
              (indent-for-tab-command)
              (end-of-line))
          (c-electric-backspace))))
    
     (define-key c-mode-map (kbd "DEL") 'my-backspace)