缩进 - 在文本Mod中的Emacs中设置4个空格缩进
在使用主模式.emacs的缓冲区中按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个空格?
Cristian asked 2019-04-28T14:27:44Z
19个解决方案
129 votes
简短回答:
关键点是告诉emacs在缩进时插入你想要的东西,这是通过改变缩进线功能来完成的。 更改它以插入选项卡然后将选项卡更改为4个空格比将其更改为插入4个空格更容易。 以下配置将解决您的问题:
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)
说明:
来自主流模式控制的缩进@ emacs手册:
每个专业的重要功能 模式是自定义键 正确地缩进语言 编辑。
[...]
indent-line-function变量是 (和。)使用的功能 各种命令,比如打电话 缩进区域)缩进当前 线。 命令 缩进按照模式不再做了 比调用这个功能。
[...]
默认值是indent-relative 对于许多模式。
来自缩进相关的@ emacs手册:
缩进相对空间到下一个 前一个非空行中的缩进点。
[...]
如果前一个非空行没有 缩进点超出列点 从“tab-to-tab-stop”开始 代替。
只需将indent-line-function的值更改为insert-tab函数,并将Tab键插入配置为4个空格。
alcortes answered 2019-04-28T14:29:31Z
65 votes
当number-sequence函数坐在那里等待使用时,它总是让我略微看到像(setq tab-stop-list 4 8 12 ................)这样的东西。
(setq tab-stop-list (number-sequence 4 200 4))
要么
(defun my-generate-tab-stops (&optional width max)
"Return a sequence suitable for `tab-stop-list'."
(let* ((max-column (or max 200))
(tab-width (or width tab-width))
(count (/ max-column tab-width)))
(number-sequence tab-width (* tab-width count) tab-width)))
(setq tab-width 4)
(setq tab-stop-list (my-generate-tab-stops))
phils answered 2019-04-28T14:30:01Z
28 votes
(customize-variable (quote tab-stop-list))
或者在.emacs文件中添加tab-stop-list条目到custom-set-variables:
(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))))
Bert F answered 2019-04-28T14:27:55Z
19 votes
您可能会发现更容易设置选项卡,如下所示:
M-x customize-group
在Customize group:提示符下输入indent。
您将看到一个屏幕,您可以在其中设置所有缩进选项并为当前会话设置它们,或者为将来的所有会话保存它们。
如果您这样做,您将需要设置自定义文件。
Dave Webb answered 2019-04-28T14:30:52Z
11 votes
(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)
answered 2019-04-28T14:31:13Z
9 votes
(defun my-custom-settings-fn ()
(setq indent-tabs-mode t)
(setq tab-stop-list (number-sequence 2 200 2))
(setq tab-width 2)
(setq indent-line-function 'insert-tab))
(add-hook 'text-mode-hook 'my-custom-settings-fn)
lawlist answered 2019-04-28T14:31:32Z
8 votes
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)
(setq c-default-style "linux")
(setq c-basic-offset 4)
(c-set-offset 'comment-intro 0)
这适用于C ++代码和内部注释
user2318996 answered 2019-04-28T14:31:59Z
7 votes
此问题不是由缺少制表位引起的; 这就是emacs有一个名为indent-relative的(新的?)tab方法,它似乎是为了排列表格数据而设计的。 TAB键映射到方法indent-for-tab-command,它调用变量indent-line-function设置的任何方法,这是文本模式的缩进相对方法。 我没有想出一个覆盖缩进行函数变量的好方法(文本模式挂钩不工作,所以也许它在模式挂钩运行后重置?)但是一个简单的方法来摆脱这个 行为是通过将TAB设置为更简单的tab-to-tab-stop方法来查看intent-for-tab-command方法:
(define-key text-mode-map(kbd“TAB”)'tab-to-tab-stop)
answered 2019-04-28T14:32:37Z
6 votes
试试这个:
(add-hook 'text-mode-hook
(function
(lambda ()
(setq tab-width 4)
(define-key text-mode-map "\C-i" 'self-insert-command)
)))
这将使TAB始终插入一个文字TAB字符,每4个字符使用制表位(但仅限于文本模式)。 如果那不是您所要求的,请描述您希望看到的行为。
cjm answered 2019-04-28T14:33:05Z
6 votes
您可以将这些代码行添加到.emacs文件中。它为文本模式添加了一个钩子,以使用insert-tab而不是indent-relative。
(custom-set-variables
'(indent-line-function 'insert-tab)
'(indent-tabs-mode t)
'(tab-width 4))
(add-hook 'text-mode-hook
(lambda() (setq indent-line-function 'insert-tab)))
我希望它有所帮助。
gigilibala answered 2019-04-28T14:33:33Z
4 votes
只是用c-set风格改变风格对我来说已经足够了。
dividebyzero answered 2019-04-28T14:33:59Z
4 votes
将其添加到.emacs文件中:
这会将选项卡显示的宽度设置为2个字符(将数字2更改为您想要的任何值)
(setq default-tab-width 2)
要确保emacs实际上使用制表符而不是空格:
(global-set-key (kbd "TAB") 'self-insert-command)
另外,在选项卡上退格时,emacs的默认设置是将其转换为空格然后删除空格。 这可能很烦人。 如果您希望它只删除选项卡,您可以这样做:
(setq c-backspace-function 'backward-delete-char)
请享用!
qwerty9967 answered 2019-04-28T14:34:52Z
2 votes
自定义可以影子(setq tab width 4)所以要么使用setq-default或让自定义知道你在做什么。 我也有类似OP的问题并单独用它修复它,不需要调整tab-stop-list或任何insert功能:
(custom-set-variables
'(tab-width 4 't)
)
发现之后立即添加它很有用(来自emacsWiki的提示):
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-indent-level 'tab-width)
Yary answered 2019-04-28T14:35:29Z
1 votes
在我在.emacs文件中写这个之前,最好的答案不起作用:
(global-set-key (kbd "TAB") 'self-insert-command)
user1009285 answered 2019-04-28T14:35:58Z
1 votes
这是唯一一个不会为我插入标签的解决方案,没有顺序或将标签转换为空格。 这两个似乎都足够,但浪费:
(setq-default
indent-tabs-mode nil
tab-width 4
tab-stop-list (quote (4 8))
)
请注意,Python需要两个数字才能工作(但不能更多!)。
此外,在大多数主要模式(例如Python)中,缩进在Emacs中是自动的。 如果您需要在自动缩进之外缩进,请使用:
M-我
ryanpcmcquen answered 2019-04-28T14:36:47Z
0 votes
你有没有尝试过
(setq tab-width 4)
answered 2019-04-28T14:37:10Z
0 votes
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
Dason answered 2019-04-28T14:37:38Z
0 votes
顺便说一句,对于C模式,我将。添加(setq-default c-basic-offset 4)到.emacs。 有关详细信息,请参阅[http://www.emacswiki.org/emacs/IndentingC]。
flyrain answered 2019-04-28T14:38:07Z
0 votes
从我的init文件,不同,因为我想要空格而不是标签:
(add-hook 'sql-mode-hook
(lambda ()
(progn
(setq-default tab-width 4)
(setq indent-tabs-mode nil)
(setq indent-line-function 'tab-to-tab-stop)
(modify-syntax-entry ?_ "w") ; now '_' is not considered a word-delimiter
(modify-syntax-entry ?- "w") ; now '-' is not considered a word-delimiter
)))
forkandwait answered 2019-04-28T14:38:36Z