【问题标题】:How to edit code in Emacs handling tabs and whitespace like Visual Studio does?如何像 Visual Studio 那样在 Emacs 中编辑处理选项卡和空格的代码?
【发布时间】:2011-02-04 23:11:36
【问题描述】:

我正在开始一个项目,每个人都在使用 Microsoft Visual Studio 编辑代码。但我是 Emacs 用户。

当我在 Emacs 中打开这些 C++ 文件时,它们看起来很奇怪。示例:

namespace·ns·{
»class·Foo·{
»»virtual·function_name(·some_type::const_iterator·start
»»»»»»»»····,·some_type::const_iterator·end
»»»»»»»»»,·boost::shared_ptr<some_type>·varname·)·=·0;
»};
}

我在这里将标签显示为»,将空格显示为“.”,以显示差异。

我想透明地查看和编辑代码,以便它在我的 Emacs 中看起来正确,并保存下来以便 Visual Studio 用户看起来正常。

那么,我该如何设置 Emacs 以便它显示应该显示的文件,这大概是:

namespace ns {
  class Foo {
    virtual function_name( some_type::const_iterator start
                         , some_type::const_iterator end
                         , boost::shared_ptr<some_type> varname ) = 0;
  };
}

我想告诉 Emacs 以原始的、奇怪的方式使用选项卡保存它。

【问题讨论】:

    标签: visual-studio emacs editor


    【解决方案1】:

    设置 tab-width 3 给我们:

    namespace ns {
       class Foo {
         virtual function_name( some_type::const_iterator start
                               , some_type::const_iterator end
                              , boost::shared_ptr<some_type> varname ) = 0;
       };
    }
    

    这与我们所能得到的假定格式最接近,但并不准确。你知道这是否符合 Visual Studio 显示的内容吗?

    您可能需要以下内容:

    (add-hook 'c++-mode-hook 'my-c++-mode-hook)
    (defun my-c++-mode-hook ()
      (setq tab-width 3
            indent-tabs-mode t
            c-basic-offset 3))
    

    希望这是一个好的开始,但肯定需要其他设置来匹配 Visual Studio 的工作方式,所以我建议您从 Emacs Wiki 开始阅读:

    http://www.emacswiki.org/emacs/IndentingC

    【讨论】:

    【解决方案2】:

    您应该选择代码并使用M-x untabify 将所有制表符转换为空格。这将几乎统一每个编辑器中代码的外观。

    【讨论】:

    • 真的吗?它将摆脱选项卡,Visual Studio 的人会发疯的。
    • 这是旧的 tabs-vs-spaces 战争。虽然我在摘要中同意你的观点,但在我工作的地方,“tabists”占主导地位,并且这种哲学已写入我们的编码标准。
    猜你喜欢
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多