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

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

Set 4 Space Indent in Emacs in Text Mode

在主模式text-mode的缓冲区中按TAB时,我没有成功让Emacs从8个空格标签切换到4个空格标签。 我已将以下内容添加到.emacs中:

1
2
3
4
5
6
(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个空格?


    简短回答:

    关键点是告诉emacs在缩进时插入你想要的东西,这是通过改变缩进线功能来完成的。更改它以插入选项卡然后将选项卡更改为4个空格比将其更改为插入4个空格更容易。以下配置将解决您的问题:

    1
    2
    3
    (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函数,并将Tab键插入配置为4个空格。


    更新:自Emacs 24.4以来:

    tab-stop-list is now implicitly extended to infinity. Its default value is changed to nil which means a tab stop every tab-width columns.

    这意味着不再需要以下面显示的方式设置tab-stop-list,因为您可以将其设置为nil

    原始答案如下......


    number-sequence函数坐在那里等待使用时,它总是让我略微看到像(setq tab-stop-list 4 8 12 ................)这样的东西。

    1
    (setq tab-stop-list (number-sequence 4 200 4))

    要么

    1
    2
    3
    4
    5
    6
    7
    8
    9
    (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))


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

    或者在.emacs文件中添加tab-stop-list条目到custom-set-variables:

    1
    2
    3
    4
    5
    6
    (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))))

    您可能会发现更容易设置选项卡,如下所示:

    1
    M-x customize-group

    Customize group:提示符下输入indent

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

    如果您这样做,您将需要设置自定义文件。


    1
    2
    3
    (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)

    1
    2
    3
    4
    5
    6
    (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 ++代码和内部注释


    1
    2
    3
    4
    5
    6
    7
    (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)

    此问题不是由缺少制表位引起的;这就是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)


    试试这个:

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

    这将使TAB始终插入一个文字TAB字符,每4个字符使用制表位(但仅限于文本模式)。如果那不是您所要求的,请描述您希望看到的行为。


    您可以将这些代码行添加到.emacs文件中。
    它为文本模式添加了一个钩子,以使用insert-tab而不是indent-relative。

    1
    2
    3
    4
    5
    6
    (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)))

    我希望它有所帮助。


    只是用c-set风格改变风格对我来说已经足够了。


    将其添加到.emacs文件中:

    这会将选项卡显示的宽度设置为2个字符(将数字2更改为您想要的任何值)

    1
    (setq default-tab-width 2)

    要确保emacs实际上使用制表符而不是空格:

    1
    (global-set-key (kbd"TAB") 'self-insert-command)

    另外,在选项卡上退格时,emacs的默认设置是将其转换为空格然后删除空格。这可能很烦人。如果您希望它只删除选项卡,您可以这样做:

    1
    (setq c-backspace-function 'backward-delete-char)

    请享用!


    自定义可以隐藏(setq tab width 4),因此使用setq-default或让Customize知道您正在做什么。 我也遇到类似于OP的问题并单独修复它,不需要调整tab-stop-list或任何insert函数:

    1
    2
    3
    (custom-set-variables
     '(tab-width 4 't)
     )

    发现之后立即添加它很有用(来自emacsWiki的提示):

    1
    2
    (defvaralias 'c-basic-offset 'tab-width)
    (defvaralias 'cperl-indent-level 'tab-width)

    这是唯一一个不会为我插入标签的解决方案,没有顺序或将标签转换为空格。这两个似乎都足够,但浪费:

    1
    2
    3
    4
    5
    (setq-default
        indent-tabs-mode nil
        tab-width 4
        tab-stop-list (quote (4 8))
    )

    请注意,quote需要两个数字才能工作(但不能更多!)。

    此外,在大多数主要模式(例如Python)中,缩进在Emacs中是自动的。如果您需要在自动缩进之外缩进,请使用:

    M-i


    在我在.emacs文件中写这个之前,最好的答案不起作用:

    1
    (global-set-key (kbd"TAB") 'self-insert-command)

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


    顺便说一句,对于C模式,我将(setq-default c-basic-offset 4)添加到.emacs。有关详细信息,请参见http://www.emacswiki.org/emacs/IndentingC。


    从我的init文件,不同,因为我想要空格而不是标签:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    (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
                   )))

    你有没有尝试过

    1
    (setq  tab-width  4)

    推荐阅读

      linux命令行设置语言?

      linux命令行设置语言?,系统,管理,环境,国家,工具,电脑,软件,文化,底部,语言,l

      linux设置壁纸的命令?

      linux设置壁纸的命令?,图片,系统,电脑,照片,位置,终端,颜色,字体,单击,壁纸,

      linux恢复命令行设置?

      linux恢复命令行设置?,系统,工作,密码,信息,工具,地址,电脑,命令,情况,地方,

      linux命令提示设置?

      linux命令提示设置?,系统,工作,地址,信息,命令,软件,目录,管理,变量,文件,Lin

      linux设置命令ip?

      linux设置命令ip?,地址,系统,代码,命令,密码,网卡,终端,计算机,测试,网关,lin

      linux设置man命令?

      linux设置man命令?,信息,系统,工具,工作,地址,命令,基础,地方,基本知识,技术

      linux参数设置命令?

      linux参数设置命令?,网络,系统,地址,工作,信息,管理,服务,名称,状态,命令,在l

      linux文本查询命令?

      linux文本查询命令?,标准,命令,文件,工具,数据,信息,位置,系统,内容,文本,Lin

      linux在线命令文本?

      linux在线命令文本?,系统,工作,信息,在线,地址,命令,基础,标准,工具,目录,Lin

      linux编写文本命令?

      linux编写文本命令?,工作,系统,命令,第一,信息,发行,代码,名字,文件,终端,lin

      linux设置电源的命令?

      linux设置电源的命令?,系统,信息,管理,设备,扩大,时间,设计,电脑,代码,位置,l

      linux切换成命令模式?

      linux切换成命令模式?,密码,系统,工具,模式,命令,首页,状态,图形界面,终端,

      linux设置路由器命令?

      linux设置路由器命令?,网络,信息,代码,地址,电脑,工作,环境,系统,密码,路由,l

      linux命令行模式清页?

      linux命令行模式清页?,工作,系统,命令,信息,地址,目录,内容,文件,操作,功能,l

      linux压缩文本的命令?

      linux压缩文本的命令?,系统,图片,命令,设备,工具,位置,软件,管理,文件,目录,

      linux设置根命令过短?

      linux设置根命令过短?,系统,工作,软件,地址,命令,代码,情况,管理,基础,位置,3

      linux设置路由器命令?

      linux设置路由器命令?,网络,信息,代码,地址,电脑,工作,环境,系统,密码,路由,l

      linux进入命令行模式?

      linux进入命令行模式?,系统,地址,情况,工作,命令,终端,首页,信息,目录,界面,l

      linux输入文本命令?

      linux输入文本命令?,系统,位置,电脑,工作,首开,命令,终端,模式,指令,字符,如

      linux底线模式命令?

      linux底线模式命令?,系统,档案,密码,状态,工作,命令,模式,文件,明文,界面,lin