Emacs Indent Function

I work in cc-mode on Emacs and I find the indentation very very annoying.

As a VIM user, I'm pretty much used to the longer indentation, and also allowing myself to hit the tab on the tab as much as I want.

In Emacs, my TAB maps to goto-line . First, what function is designed to indent the current line (or what after the period)? indent-region annoying since you need to select an area.

Secondly, what is the best way to correct indentation?

Thanks,

+4
source share
3 answers

You can see what type of tab is currently in use with the Mx describe-variable RET c-indentation-style (but as indicated in the documentation, do not set this variable directly, but use Mx c-set-style ).

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

  • gnu is a coding style blessed by the Free Software Foundation for C code in GNU programs.
  • k & r is the classic Kernighan and Ritchie style for code C.
  • bsd - also known as "Allman style" after Eric Alman.
  • whitesmith. Popularized with examples that came with Whitesmiths C, an early commercial C compiler.
  • strustrup is the classic Stroustrup style for C ++ code.
  • ellemtel - popular C ++ coding standards defined by "C ++ Programming Rules and Recommendations", Eric Nyquist and Mats Henrison, Ellemtel ^ 1.
  • linux is the C encoding standard for Linux (kernel).
  • python is the C coding standard for Python ^ 2 extension modules.
  • java . Style for editing Java code. Please note that the default value for the c-default style sets this style when entering java mode.
  • awk - AWK code editing style. Note that the default value for the c-default style sets this style when entering awk mode.
  • user This is a special style created by you. It consists of the factory defaults for all style variables that are changed using the settings that you perform either through the settings interface, or by writing setqs and c-set-offsets at the top level of your .emacs file (see the Configuration Basics section) . The style system creates this style as part of its initialization and does not change it subsequently.

UPDATE:

Others suggested using the tab key to insert the tab character \t , but not to force the tab character! As one of the creators, StackOverflow says "only the moron will use tabs to format his code . " Now it's a little harsh, but it is worth noting that even the two most giant competitor, the Google and the Microsoft , settle this question (although they recommend a different number of spaces by default).

Google says :

Use only spaces and indents 2 spaces at a time.

Microsoft says :

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

In addition, emacswiki has a β€œTabs of Evil” section.

So go ahead and don’t take care !

+8
source

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

0
source

I like emacs too, but I can't stand his attempts to manage tabs for me. Therefore, I use the following in .emacs :

 (global-set-key "\r" 'newline-and-indent) (global-set-key "\Cm" 'newline-and-indent) (global-set-key "\Cj" '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 " ")) 

Resulting behavior: the tab key is only a tab character that you need to insert by pressing enter, copy the exact leading spaces (spaces or tabs) from the current line to a new line, and all special indent actions in these modes are disabled. If you use other languages, you may need to extend / adapt the above by adding hooks for them.

Note. In the above example, most spaces in quotation marks are actually literal tabs. If this does not happen through SO and copy / paste to the right, you may have to manually fix them yourself.

-2
source

Source: https://habr.com/ru/post/1402976/


All Articles