4

I am working in cc-mode in Emacs and I find the indentation very very annoying.

As a VIM user, I'm pretty much used to longer indents, as well as having the luxury of hitting tab to tab as much as I want.

In Emacs, my TAB is mapped to goto-line. First, which function is designed for indenting the current line (or what's after the point)? indent-region is annoying since you need to highlight the region.

Second, what's the best way to fix the indentation?

Thank you,

darksky
  • 20,411
  • 61
  • 165
  • 254
  • 1
    Normally TAB is bound to c-indent-line-or-region which indents the current line; it can be hit multiple times as you state (only the first has any effect). Are you using an init file that re-binds the TAB key? Note that c-c, c-q indents the current function. Also alt-x, describe-bindings shows the current mode key bindings. – William Morris Mar 23 '12 at 02:00

3 Answers3

8

You can see what kind of tabbing is currently in use with M-x describe-variable RET c-indentation-style (but as the documentation states, don't set this variable directly, instead use M-x c-set-style).

The variable c-basic-offset is what controls the tabbing in cc-mode and its default is set-from-style which means the tabbing will be inherited from what C style you've set with M-x set-c-style which will let you choose from a set of built-in styles (see below) or you can create your own style. You can see how the styles are defined with M-x describe-variable RET c-style-alist and then you can use one of those as a template with M-x c-add-style.

  • gnu - Coding style blessed by the Free Software Foundation for C code in GNU programs.
  • k&r - The classic Kernighan and Ritchie style for C code.
  • bsd - Also known as “Allman style” after Eric Allman.
  • whitesmith - Popularized by the examples that came with Whitesmiths C, an early commercial C compiler.
  • stroustrup - The classic Stroustrup style for C++ code.
  • ellemtel - Popular C++ coding standards as defined by “Programming in C++, Rules and Recommendations,” Erik Nyquist and Mats Henricson, Ellemtel^1.
  • linux - C coding standard for Linux (the kernel).
  • python - C coding standard for Python extension modules^2.
  • java - The style for editing Java code. Note that the default value for c-default-style installs this style when you enter java-mode.
  • awk - The style for editing AWK code. Note that the default value for c-default-style installs this style when you enter awk-mode.
  • user - This is a special style created by you. It consists of the factory defaults for all the style variables as modified by the customizations you do either with the Customization interface or by writing setqs and c-set-offsets at the top level of your .emacs file (see Config Basics). The style system creates this style as part of its initialization and doesn't modify it afterwards.

UPDATE:

Others have suggested using the tab key to insert the \t tab character, but please don't force insertion of the tab character! As one of the creators of StackOverflow says "only a moron would use tabs to format their code". Now that's a bit harsh, but it's worth noting that even the two most giant rivals, Google and Microsoft, agree on this point (even though they recommend a different number of spaces by default).

Google says:

Use only spaces, and indent 2 spaces at a time.

Microsoft says:

Tab characters (\0x09) should not be used in code. All indentation should be done with 4 space characters.

Also, the emacswiki has a section on Tabs are Evil.

So, go forth and untabify!

aculich
  • 14,545
  • 9
  • 64
  • 71
0

Answer is: (setq-default c-basic-offset <value>)

darksky
  • 20,411
  • 61
  • 165
  • 254
-2

I too like emacs but can't stand its attempts to manage tabs for me. So I use the following in my .emacs:

(global-set-key "\r" 'newline-and-indent)
(global-set-key "\C-m" 'newline-and-indent)
(global-set-key "\C-j" 'newline)

(defalias 'backward-delete-char-untabify 'backward-delete-char)
(defun indent-according-to-mode () (interactive)
  (save-excursion
    (goto-char (- (line-beginning-position) 1))
    (search-backward-regexp "^[         ]*")
  )
  (if (eq (point) (line-beginning-position))
    (insert (match-string 0))
    (save-excursion
      (goto-char (line-beginning-position))
      (insert (match-string 0))
    )
  )
)

(defun newline-and-indent () (interactive) (newline) (indent-according-to-mode))
(defun lisp-indent-line () (interactive) (insert "      "))
; Is there a way to fix this without a hook?
(defun my-c-hook ()
  (setq c-electric-flag nil)
  (defun c-indent-command (n) (interactive "*") (insert "       ")))
(add-hook 'c-mode-common-hook 'my-c-hook)
(defun my-perl-hook ()
  (defun perl-electric-terminator () (interactive "*") (self-insert-command 1))
  (defun perl-indent-command () (interactive "*") (insert "     ")))
(add-hook 'perl-mode-hook 'my-perl-hook)
(defun indent-for-tab-command () (interactive "*") (insert "    "))

The resulting behavior: The tab key is purely a tab character to be inserted, pressing enter copies the exact leading whitespace (spaces or tabs) from the current line onto the new line, and all the special indention behavior in these modes is disabled. If you use other languages you might have to extend/adapt the above to add hooks for them.

Note: In the above, most of the whitespace in quotes is actually literal tabs. If it doesn't make it through SO and copy/paste right, you might have to manually fix it yourself.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • This sounds great! But before I use it, I don't understand this last note of yours. Could you please explain before I actually use it? Furthermore, does this keep the size 4 indentation invoked by `setq-default c-basic-offset`? What about braces? Does this handle them? – darksky Mar 23 '12 at 01:47
  • This code disables *all* automatic indention except copying the leading whitespace from the previous line. You have to press the tab or space key to get the indention you want. My note was that rather than using `\t` or whatever the escape for tabs is, I wrote literal tabs (inside the quotes) in the above code and I'm not sure if they'll survive the copy-and-paste... – R.. GitHub STOP HELPING ICE Mar 23 '12 at 02:39
  • @R.. Please don't force insertion of the tab character! As one of the creators of StackOverflow says ["only a moron would use tabs to format their code"](http://www.codinghorror.com/blog/2009/04/death-to-the-space-infidels.html). Now that's a bit harsh, but it's worth noting that even the two most giant rivals, [Google](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Spaces_vs._Tabs) and [Microsoft](http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx), agree on this point (even though they recommend a different number of spaces by default). – aculich Mar 23 '12 at 14:46
  • @aculich: There are plenty of religious arguments on both sides. Personally, I think only a moron would use spaces to indent (where "indent" has a very specific meaning that does not include aligning lines), but my answer is there to answer the question not start a religious debate (which, if you want to appeal to authority, I believe **all** of the SO creators and mods would agree is not wanted here). – R.. GitHub STOP HELPING ICE Mar 24 '12 at 00:13
  • 1
    @R.. Even if you think inserting a tab character is a good idea, this is still not a good answer for at least two reasons: 1) as you noted, SO stripped your literal tabs, so the example code you posted doesn't actually do what you intended; 2) even if you fixed SO's filtering to insert a literal tab in place of the spaces, the code is still not a good way of (ab)using cc-mode; you should read up on the [Indentation Commands](http://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#Indentation-Commands), in particular the variables `c-insert-tab-function` and `c-tab-always-indent`. – aculich Mar 24 '12 at 00:27
  • It's the only way to do exactly what I want (100% user-controlled indention with no automation) and it's one solution OP might be happy with. As for the limitation (1), I warned about it in my answer. I think everybody can tell your -1 is about hating tabs and not about anything wrong with my answer. – R.. GitHub STOP HELPING ICE Mar 24 '12 at 02:20
  • -1 For overwriting default builtin functions instead of defining your own. Your use of `defalias` and `defun` overwrites built-in functions that other packages may depend on. Please PLEASE don't share code that will break new users' Emac and create hard to debug problems. – event_jr Mar 24 '12 at 03:24
  • @R.. My -1 is because you've written poor elisp code that doesn't actually answer either of the questions posed. It outright ignores the question "which function is designed for indenting the current line?" and it certainly does not even come close to suggesting "the best way to fix the indentation?" Your answer is about hating spaces and hacking a way to avoid using them. Also, what happens when you edit someone else's file that already uses spaces? It mixes tabs and spaces which is certainly the greatest invisible evil of all! – aculich Mar 24 '12 at 05:10
  • No, the whole point of the way my code works (*copying* the existing spacing) is that you can edit files that mix spacing techniques. – R.. GitHub STOP HELPING ICE Mar 24 '12 at 05:29