【问题标题】:How can I give the filename of current buffer automatically when I execute 'compile' command in Emacs?在 Emacs 中执行“编译”命令时,如何自动给出当前缓冲区的文件名?
【发布时间】:2015-09-06 10:34:39
【问题描述】:

如你所知,当我们在 emacs c++ 模式下执行 'M-x compile' 时,我们可以看到 'make -k' 命令。

但是,现在我正在研究一些 boost 库,所以我必须经常在编写示例代码后只编译单个文件。 所以,为了方便起见,我一直在这样做。

(1) make an alias in shell init file for g++ compile command.
    alias c="g++ -g -Wall -O0 -std=c++11"
(2) map 'M-x compile' command to Function Key F9 in .emacs
    (global-set-key [f9] 'compile)
(3) write c++ code
(4) press F9 and modify filename to current open file

例如,您可以在下面的屏幕截图中看到“编译命令:c vvv.cpp”。 (这个屏幕截图只是我为这个问题制作的一个简单代码。 'c' 是编译命令和选项的别名) 但是,这个打开的文件名实际上是'vector1.cpp',所以'vvv.cpp'应该自动替换为'vector1.cpp'。

我认为这还不错。 但是,我想找到一种不必修改当前文件名的方法。 “只需按 F9 和 RET,即可编译当前文件。”这正是我想要的。

有没有更好的想法或方法可以做到这一点? 或者请让我知道您的最佳编译方式,以实现最少的按键操作。 谢谢。

【问题讨论】:

    标签: c++ emacs compilation keymapping


    【解决方案1】:

    EmacsWiki 有一个很好的部分解释了如何自定义 compile 命令:Compile Command

    在您的情况下,您可以在 c++ 模式中添加一个挂钩来根据需要定义 compile-command 变量。按照wiki上的例子:

    (add-hook 'c++-mode-hook
      (lambda ()
        (set (make-local-variable 'compile-command)
             (format "g++ -g -Wall -O0 -std=c++11 %s" (buffer-name)))))
    

    将此 sn-p 添加到您的 .emacs 应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-18
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多