0

I'm switching over to Emacs as my C++ editor, and what I miss the most is when I use tab, it would write 8 spaces, I could use tab more than once and use it in the middle of code, when I pressed back space it would remove the whole tab area, and it wouldn't auto indent. How can I make this happen? Thanks.

1 Answer 1

1

First, backspace. If you want to delete all whitespaces before point, try hungry-delete mode. If you would like to delete some whitespaces according to some indentation level, you could try to add this in you init.el:

(defun get-deletion-count (arg)
  "Return the amount of spaces to be deleted, ARG is indentation border."
  (if (eq (current-column) 0) 0
    (let ((result (mod (current-column) arg)))
      (if (eq result 0) arg
        result))))

(defun backspace-some (arg)
  "Deletes some backspaces, ARG unused."
  (interactive "*P")
  (if (use-region-p) (backward-delete-char-untabify 1)
    (let ((here (point)))
      (if (eq 0 (skip-chars-backward " " (- (point) (get-deletion-count 4))))
          (backward-delete-char-untabify 1)
        (delete-region (point) here)))))

(global-set-key [backspace] 'backspace-some)

In my config I assume indentation level == 4. It will delete all whitespaces before point until next indentation level (..., 16, 12, 8, 4, 0).

Second, tab. By default emacs indents all code according to your c-default-style and c-basic-offset variables. Try to configure them via (setq c-basic-offset 8) and your code will be indented with 8 spaces. Also you can override TAB key with some function which will insert 8 spaces like that:

(defun insert-some-spaces ()
  "Inserts 8 spaces after point"
  (interactive)
  (insert "        ")
)

(global-set-key [tab] 'insert-some-spaces)
2
  • I added this to my .emacs, but using the tab key didn't work. Am I supposed to install some plugin? It displays something about yasnippet, maybe something is conflicting with that plugin, because I watched a video about turning Emacs into a C++ IDE and installed a lot of plugins
    – Anti.Josh
    Feb 7, 2016 at 13:59
  • Please see my edition about (interactive) and place this in yout init.el file: (setq yas-fallback-behavior 'call-other-command) This should work. Feb 7, 2016 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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