General Emacs Tab Configuration

I am trying to switch from Vim to Emacs, but I will rip my hair off trying to set it to handle tabs as I wish. I need:

  • Insertable tabs will be expanded in two . Emacs stubbornly sticks to eight, no matter what I do.
  • Tabs (i.e. real characters \t) should be displayed on the screen with two spaces.
  • Pressing TAB should insert a tab with the cursor , and not indent the entire line . Currently, I press TAB anywhere, and Emacs kills all the spaces at the beginning of the line; this is the most annoying thing so far.

My current ~/.emacsreads

(setq standard-indent 2)
(setq-default indent-tabs-mode nil)

but I have not tried any specific configurations from the Internet, none of which did what they said. (Is the API ever-changing? I am using GNU Emacs 23.1.1, apparently.)

+5
source share
3 answers

Emacs has extremely flexible indentation support. Typically, the mode you are in determines how they work, so if you are working on a C file, the way tabs work will be different than if you are working on a Python file.

, , , . - emacs. , .

, 2. Java C, , NIL:

  • -
  • --
  • -

. "M-x customize-group RET C", C.

, .

, emacs . "M-x basic-mode", , .

+7
;; * Inserted "tabs" to be expanded into two spaces. Emacs stubbornly
;;   sticks to eight, no matter what I do.

;; * Tabs (i.e. real \t characters) to be represented on screen by two
;;   spaces.

(setq-default tab-width 2)


;; * Pressing TAB should insert a tab at the cursor rather than indent
;;   the entire line. Currently, I press TAB anywhere and Emacs
;;   destroys all whitespace at the start of the line; this is the
;;   most infuriating thing so far.

(setq-default indent-tabs-mode t)

(mapcar (lambda (hooksym)
          (add-hook hooksym
                    (lambda ()
                      (kill-local-variable 'indent-tabs-mode)
                      (kill-local-variable 'tab-width)
                      (local-set-key (kbd "TAB") 'self-insert-command))))

        '(
          c-mode-common-hook

          ;; add other hook functions here, one for each mode you use :-(
          ))

;; How to know the name of the hook function?  Well ... visit a file
;; in that mode, and then type C-h v major-mode RET.  You'll see the
;; mode name in the *Help* buffer (probably on the second line).

;; Then type (e.g.) C-h f python-mode; you'll see blather about the
;; mode, and (hopefully) somewhere in there you'll see (again e.g.)
;; "This mode runs the hook `python-mode-hook', as the final step
;; during initialization."
+4

This should give you more than anything you want. You may have to configure some of the other programming modes that you usually use.

(defun insert-tab ()
  "self-insert-command doesn't seem to work for tab"
  (interactive)
  (insert "\t"))
(setq indent-line-function 'insert-tab)  ;# for many modes
(define-key c-mode-base-map [tab] 'insert-tab) ;# for c/c++/java/etc.
(setq-default tab-width 2)
+1
source

All Articles