【问题标题】:Emacs global configuration of tabsEmacs 全局配置选项卡
【发布时间】:2011-03-11 01:54:33
【问题描述】:

我正在尝试从 Vim 切换到 Emacs,但我正在努力配置它以按照我的意愿处理选项卡。我需要:

  • 插入的“标签”将展开为 两个 空格。无论我做什么,Emacs 都坚持使用 8 个。
  • 标签(即真正的\t 字符)在屏幕上由两个空格表示。
  • 按 TAB 应该在光标处插入一个制表符,而不是缩进整行。目前,我在任何地方按 TAB ,Emacs 会破坏行首的所有空格;这是迄今为止最令人愤怒的事情。

我当前的~/.emacs 读取

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

但我已经尝试了无数来自网络的建议配置,但没有一个能做到他们所说的那样。 (API 会不断变化吗?我显然在使用GNU Emacs 23.1.1。)

【问题讨论】:

    标签: emacs tabs spaces expand


    【解决方案1】:

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

    因此,它确实取决于您使用的模式,这将限制您获得的答案。在大多数情况下,我建议您不要反对它——对我来说,缩进行为是 emacs 的最佳特性之一。但是,您确实需要花时间为自己定制它。

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

    • c-tab-always-indent
    • c-句法缩进
    • 缩进标签模式

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

    如果您要编辑不同类型的文件,则说明会有所不同。

    也许 emacs 为您的文件设置了错误的模式。您可以尝试执行“M-x basic-mode”,看看您是否更喜欢那里的行为。

    【讨论】:

      【解决方案2】:

      这应该可以满足您的大部分需求。您可能需要自定义一些您常用的其他编程模式。

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

      【讨论】:

        【解决方案3】:
        ;; * 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."
        

        【讨论】:

          猜你喜欢
          • 2014-08-08
          • 1970-01-01
          • 1970-01-01
          • 2011-09-22
          • 2013-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多