Search code examples
c++emacstabskey-bindings

Emacs c++ tabs writing spaces removing


I have seen a lot of questions about emacs and tabs/spaces topic but no one talking about this:

I have emacs in C++ mode and tabs as indent character, if i press enter for a newline emacs auto-indent with tab but if i try to remove that "tab" i remove only one space.

I understand that emacs create tabs using spaces but if i want to remove that "tab" i would want to remove the 4 (or 8) spaces directly not one by one, in python-mode works as intended but in c++ mode no. There is a variable for that or any work around?


Solution

  • Your Backspace key (<DEL>) is probably bound to command backward-delete-char-untabify. Bind it to something else, e.g. delete-backward-char.

    From the Elisp manual, node Program Modes:

    In most programming languages, indentation should vary from line to line to illustrate the structure of the program. Therefore, in most programming language modes, typing <TAB> updates the indentation of the current line.

    Furthermore, <DEL> is usually bound to backward-delete-char-untabify, which deletes backward treating each tab as if it were the equivalent number of spaces, so that you can delete one column of indentation without worrying whether the whitespace consists of spaces or tabs.

    IOW, the behavior you don't like is provided by default in the programming mode you are using.

    Actually, if you are in c++-mode, <DEL> is bound to command c-electric-backspace. The doc for that function (C-h f c-electric-backspace) tells you:

    c-electric-backspace is an interactive compiled Lisp function in cc-cmds.el.

    (c-electric-backspace ARG)

    Delete the preceding character or whitespace.

    If c-hungry-delete-key is non-nil (indicated by "/h" on the mode line) then all preceding whitespace is consumed. If however a prefix argument is supplied, or c-hungry-delete-key is nil, or point is inside a literal then the function in the variable c-backspace-function is called.

    And by default it seems that the value of user option c-backspace-function (C-h v) is ... backward-delete-char-untabify. So you can just customize that option to set it to some other function, such as delete-backward-char. That will change the behavior only for C++ and C modes.