Emacs标签的全局配置

时间:2010-07-02 12:09:51

标签: emacs tabs spaces expand

我正在尝试从Vim切换到Emacs,但是我正在试图将它配置为按照我希望的方式处理标签。我要求:

  • 插入“标签”以展开为两个空格。无论我做什么,Emacs都会顽固地坚持到八。
  • 要在屏幕上用两个空格表示的标签(即真实\t字符)。
  • 按TAB键应在光标处插入标签,而不是缩进整行。目前,我在任何地方按TAB,Emacs在行开始时销毁所有空格;到目前为止,这是最令人愤怒的事情。

我当前的~/.emacs读取

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

但是我从网上尝试过没有结束的建议配置,但没有一个完成他们所说的。 (API是否会不断变化?显然我正在使用GNU Emacs 23.1.1。)

3 个答案:

答案 0 :(得分:7)

Emacs对处理缩进提供了非常灵活的支持。通常,您所处的模式决定了它们的工作方式 - 因此,如果您正在处理C文件,那么按Tab键的工作方式将与您处理Python文件时的方式不同。

所以它确实取决于你正在使用哪种模式,这将限制你得到的答案。在大多数情况下,我建议你不要反对它 - 对我来说,缩进行为是emacs的最佳功能之一。但是,您确实需要花时间为自己定制它。

要更改选项卡的显示方式,您需要将tab-width设置为2.如果您正在编辑Java或C样式代码,那么听起来您想要将这些优秀的缩进功能关闭到NIL:

  • C-制表总是缩进
  • C-句法压痕
  • 缩进凸片模式

我建议你通过自定义界面设置这些。如果使用“M-x customize-group RET C”,则可以看到C模式的各种设置。

如果您正在编辑不同类型的文件,那么说明会有所不同。

也许emacs的文件模式错误。您可以尝试使用“M-x基本模式”来查看您是否更喜欢那里的行为。

答案 1 :(得分:4)

;; * 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's 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."

答案 2 :(得分:1)

这可以让你获得你想要的大部分。您可能需要自定义常用的其他编程模式。

(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)