Emacs 选项卡全局配置

发布于 2024-09-08 03:39:36 字数 551 浏览 5 评论 0原文

我正在尝试从 Vim 切换到 Emacs,但我正在绞尽脑汁地尝试将其配置为按照我希望的方式处理选项卡。我要求:

  • 插入“制表符”以扩展为两个空格。无论我做什么,Emacs 都顽固地坚持八个。
  • 制表符(即真正的\t 字符)在屏幕上由两个 空格表示。
  • 按 T​​AB 应该在光标处插入制表符,而不是缩进整行。目前,我在任何地方按 TAB 键,Emacs 都会销毁行开头的所有空格;这是迄今为止最令人气愤的事情。

我当前的 ~/.emacs 读取,

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

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

I'm attempting to switch from Vim to Emacs, but I'm tearing my hair out trying to configure it to treat tabs how I wish. I require:

  • 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.
  • 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.

My current ~/.emacs reads

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

but I have tried no end of suggested configurations from the web, none of which have done what they said they would. (Does the API constantly change? I'm using GNU Emacs 23.1.1, apparently.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疏忽 2024-09-15 03:39:36

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

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

要更改制表符的显示方式,您需要将制表符宽度设置为 2。如果您正在编辑 Java 或 C 样式代码,那么听起来您想通过将这些设置为 NIL 来关闭所有良好的缩进功能:

  • c-tab -always-indent
  • c-syntropic-indentation
  • indent-tabs-mode

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

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

也许 emacs 的文件模式不正确。您可以尝试执行“Mx basic-mode”,看看您是否喜欢那里的行为。

Emacs has extremely flexible support for handling indentation. Generally the mode that you are in dictates how they work - so if you're working on a C file then the way that pressing tab works will be different than if you're working on a Python file.

So it does depend which mode you're working in, which will limit the answers you get. In most cases I would recommend that you don't fight against it - for me the indentation behaviour is one of the best features of emacs. However, you do need to spend the time to customize it for yourself.

To change the way that tabs are displayed you need to set tab-width to 2. If you're editing Java or C style code then it sounds like you want to turn off all the nice indentation features by these to NIL:

  • c-tab-always-indent
  • c-syntactic-indentation
  • indent-tabs-mode

I suggest you set these through the customization interface. If you use "M-x customize-group RET C" then you can see the various settings for C mode.

If you're editting different types of files then the instructions will be different.

Perhaps emacs is in the wrong mode for your file. You could try doing "M-x fundamental-mode" to see if you prefer the behaviour there.

心在旅行 2024-09-15 03:39:36
;; * 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."
;; * 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."
↙厌世 2024-09-15 03:39:36

这应该能让你得到你想要的大部分东西。您可能需要自定义一些常用的其他编程模式。

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

This should get you most of what you want. You'll probably have to customize some other programming modes you commonly 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文