Question

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,

Was it helpful?

Solution

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!

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top