在文本模式下在Emacs中设置4个空格缩进

时间:2020-03-05 18:55:18  来源:igfitidea点击:

当在具有主要模式"文本模式"的缓冲区中按" TAB"键时,我无法使Emacs从8个空格键切换到4个空格键。我在我的.emacs中添加了以下内容:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)

;;; And I have tried
(setq indent-tabs-mode nil)
(setq tab-width 4)

无论我如何更改.emacs文件(或者缓冲区的局部变量),TAB按钮始终会执行相同的操作。

  • 如果上面没有文本,则缩进8个空格
  • 如果前一行有文字,请缩进第二个单词的开头

就我所爱Emacs而言,这变得越来越烦人。当上一行没有文本时,是否有办法使Emacs至少缩进4个空格?

解决方案

回答

你有没有尝试过

(setq  tab-width  4)

回答

(customize-variable (quote tab-stop-list))

或者向.emacs文件中的自定义设置变量添加tab-stop-list条目:

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120))))

回答

试试这个:

(add-hook 'text-mode-hook
  (function
   (lambda ()
     (setq tab-width 4)
     (define-key text-mode-map "\C-i" 'self-insert-command)
     )))

这将使TAB始终插入一个文字TAB字符,并且制表符停靠每4个字符(但仅限于文本模式)。如果这不是我们要的,请描述我们希望看到的行为。

回答

我们可能会发现按以下步骤设置标签更加容易:

M-x customize-group

在"自定义组:"提示下,输入"缩进"。

我们会看到一个屏幕,我们可以在其中设置所有缩进选项并将其设置为当前会话,或者将其保存为以后的所有会话。

如果我们采用这种方式,则需要设置一个自定义文件。

回答

(setq tab-width 4)
(setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80))
(setq indent-tabs-mode nil)

回答

此问题不是由缺少制表位引起的;它不是由制表符引起的。这是emacs具有一个名为indent-relative的(新?)制表符方法,该方法似乎旨在排列表格数据。 TAB键映射到方法indent-for-tab-command,该方法调用将变量indent-line-function设置为的任何方法,该方法是文本模式的indent-relative方法。我还没有找到一种覆盖indent-line-function变量的好方法(文本模式钩子不起作用,所以它可能在模式钩子运行后被重置了?),但是一种简单的方法可以摆脱这种情况行为是通过将TAB设置为更简单的Tab-to-Tab-Stop方法来取消Intent-For-Tab-Command命令方法:

(定义键文本模式映射(kbd" TAB")"制表符到制表符停止")

回答

(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)

回答

仅用c-set-style更改样式就足够了。

回答

简短答案:

关键是告诉emacs在缩进时插入我们想要的任何内容,这是通过更改indent-line-function来完成的。将其更改为插入一个选项卡,然后将选项卡更改为4个空格比将其更改为插入4个空格要容易得多。以下配置将解决问题:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)

解释:

从主要模式控制的缩进@ emacs手册:

An important function of each major
  mode is to customize the  key to
  indent properly for the language being
  edited.
  
  [...]
  
  The indent-line-function variable is
  the function to be used by  (and
  various commands, like when calling
  indent-region) to indent the current
  line. The command
  indent-according-to-mode does no more
  than call this function.
  
  [...]
  
  The default value is indent-relative
  for many modes.

相对于缩进@ emacs手册:

Indent-relative Space out to under next
  indent point in previous nonblank line.
  
  [...]
  
  If the previous nonblank line has no
  indent points beyond the column point
  starts at, `tab-to-tab-stop' is done
  instead.

只需将indent-line-function的值更改为insert-tab函数,并将制表符插入配置为4个空格即可。