在Emacs ruby​​-mode中用制表符缩进而不是空格

我一直在尝试配置Emacs,以便在缩进Ruby代码时插入’tab’而不是一系列’spaces’。

到目前为止,我已经尝试将var ruby-indent-tabs-modet这样根据文档,它将“在ruby模式下插入标签,如果这是非零的”。 但到目前为止,没有骰子。

我也尝试通过Easy customisation自定义它,它将以下内容插入到我的init.el

 (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. '(ruby-indent-tabs-mode t)) (custom-set-faces ;; custom-set-faces 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. ) 

在通过Ch v检查变量之后,它报告变量设置为t ,但按TAB继续插入空格。

我甚至尝试编辑.el文件以获取ruby-mode并重新编译它以使其无效。

帮助将不胜感激。

—–编辑—–

这是通过Ch m报告的次要模式:

启用次要模式:缩写自动完成自动组合
自动压缩自动加密文件名称阴影字体锁定
全局自动完成Global-Font-Lock Inf-Ruby线号菜单栏
Show-Smartparens Show-Smartparens-Global Smartparens
Smartparens-Global Transient-Mark

init.el文件目前有:

 (require 'cask "/Users/snowingheart/.cask/cask.el") (cask-initialize) (require 'pallet) (add-to-list 'load-path "~/elisp") (load "php-mode") (add-to-list 'auto-mode-alist '("\\.php[34]?\\'\\|\\.phtml\\'" . php-mode)) (global-set-key (kbd "Cx ") 'windmove-up) (global-set-key (kbd "Cx ") 'windmove-down) (global-set-key (kbd "Cx ") 'windmove-right) (global-set-key (kbd "Cx ") 'windmove-left) (require 'package) (add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/")) (package-initialize) (global-set-key (kbd "Cx >") 'mc/mark-next-like-this) (global-set-key (kbd "Cx <") 'mc/mark-previous-like-this) (global-set-key (kbd "Cc C-<") 'mc/mark-all-like-this) (global-set-key (kbd "CSc CSc") 'mc/edit-lines) (require 'smartparens-config) (require 'smartparens-ruby) (require 'smartparens-erb) (smartparens-global-mode) (show-smartparens-global-mode t) (sp-with-modes '(rhtml-mode) (sp-local-pair "") (sp-local-pair "")) (require 'auto-complete-config) (add-to-list 'ac-dictionary-directories "~/.emacs.d/.cask/24.3.50.1/elpa/auto-complete-20130724.1750/dict") (ac-config-default) (setq ac-ignore-case nil) (add-to-list 'ac-modes 'enh-ruby-mode) (add-to-list 'ac-modes 'web-mode) (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. '(indent-tabs-mode t) '(ruby-indent-tabs-mode t)) (custom-set-faces ;; custom-set-faces 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. ) (setq-default indent-tabs-mode t) (setq enh-ruby-indent-tabs-mode t) (smart-tabs-insinuate 'ruby) (smart-tabs-advice ruby-indent-line ruby-indent-level) (setq ruby-indent-tabs-mode t) 

尝试将以下内容添加到init.el (在您已有的自定义项下面):

 (setq-default indent-tabs-mode t) 

indent-tabs-mode的文档:

如果这是非零,缩进可以插入制表符。

我不使用ruby-mode所以我不知道indent-tabs-moderuby-indent-tabs-mode之间可能的交互。 将indent-tabs-modet (并删除您对ruby-indent-tabs-mode所做的自定义)可能就足够了。 但是当您将上面的代码段添加到配置中时,Emacs的默认行为是插入缩进选项卡。


编辑

从这里可以看出, enh-ruby-mode定义了一个名为enh-ruby-indent-tabs-mode的可自定义变量,默认值为nil 。 稍后 ,此变量的值用于覆盖indent-tabs-mode的值,这就是为什么将indent-tabs-mode设置为t对启用了enh-ruby-mode缓冲区没有影响的原因。

因此,除非您启用除ruby-modeenh-ruby-mode之外的任何其他模式,可能正在修改indent-tabs-mode变量,

 (setq enh-ruby-indent-tabs-mode t) 

你的init.el应该解决你的问题。


另一个编辑(工作解决方案)

(致谢: 这个答案让我走上正轨。)

运用

  • Emacs 24.3.1

  • ruby-mode 1.2版(内置)

  • enh-ruby-mode版本20140406.252 (通过Mx package-installpackage-install

通过将以下内容添加到一个完全空的 init.el文件中,我能够使它工作:

 (package-initialize) (setq-default tab-width 2) (setq enh-ruby-indent-tabs-mode t) (defvaralias 'enh-ruby-indent-level 'tab-width) (defvaralias 'enh-ruby-hanging-indent-level 'tab-width) 

此解决方案适用于Emacs的GUI和控制台版本。 它可能与您的其他自定义集成良好,但您需要从上面发布的init.el版本中删除custom-set-variables部分及其下面的所有内容。

另请注意,如果您遇到Emacs插入空格而不是选项卡的情况,您可以随时删除它并通过Cq TAB 引用选项卡来强制插入选项卡。


包起来

原来在enh-ruby-mode存在一个错误 ,当enh-ruby-indent-tabs-mode设置为t时,会导致从第二级开始块的缩进失败。 enh-ruby-mode的作者/维护者没有修复它的计划,但是bug报告包含一个可以修复问题的补丁。