使用 Emacs 作为 IDE
目前,当我使用 C 或 C++ 进行编码时,我使用 Emacs 的工作流程涉及三个窗口。 右侧最大的包含我正在使用的文件。 左侧分为两部分,底部是一个 shell,我用它来输入编译或 make 命令,顶部通常是我在工作时想要查阅的某种文档或自述文件。 现在我知道有一些相当专业的 Emacs 用户,并且我很好奇如果打算将其用作完整的 IDE,那么其他 Emacs 功能上还有什么用处。 具体来说,大多数 IDE 通常以某种形式实现这些功能:
- 源代码编辑
- 器 编译器
- 调试
- 文档查找
- 版本控制
- OO 功能,例如类查找和对象检查器
对于其中的一些功能,Emacs 如何适应这些功能是非常明显的,但是呢其余的部分? 另外,如果一定要关注某种特定的语言,我认为应该是C++。
编辑:一位用户指出,当我说“其余的呢”时,我应该更具体。 我主要对高效的版本控制以及文档查找感到好奇。 例如,在 SLIME 中,对 Lisp 函数进行快速超规范查找相当容易。 有没有一种快速的方法可以在 C++ STL 文档中查找某些内容(如果我忘记了 的确切语法)例如,hash_map)?
Currently my workflow with Emacs when I am coding in C or C++ involves three windows. The largest on the right contains the file I am working with. The left is split into two, the bottom being a shell which I use to type in compile or make commands, and the top is often some sort of documentation or README file that I want to consult while I am working. Now I know there are some pretty expert Emacs users out there, and I am curious what other Emacs functionally is useful if the intention is to use it as a complete IDE. Specifically, most IDEs usually fulfill these functions is some form or another:
- Source code editor
- Compiler
- Debugging
- Documentation Lookup
- Version Control
- OO features like class lookup and object inspector
For a few of these, it's pretty obvious how Emacs can fit these functions, but what about the rest? Also, if a specific language must be focused on, I'd say it should be C++.
Edit: One user pointed out that I should have been more specific when I said 'what about the rest'. Mostly I was curious about efficient version control, as well as documentation lookup. For example, in SLIME it is fairly easy to do a quick hyperspec lookup on a Lisp function. Is there a quick way to look up something in C++ STL documentation (if I forgot the exact syntax of hash_map, for example)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
我同意您应该了解 Mx 编译(将其和 Mx next-error 绑定到短键序列)。
了解版本控制的绑定(例如 vc-diff、vc-next-action 等)。
查看寄存器。 您不仅可以记住缓冲区中的位置,还可以记住整个窗口配置(Cx rw - 窗口配置到寄存器)。
I agree that you should learn about M-x compile (bind that and M-x next-error to a short key sequence).
Learn about the bindings for version control (e.g. vc-diff, vc-next-action, etc.)
Look into registers. You not only can remember locations in buffers but whole window configurations (C-x r w -- window-configuration-to-register).
近年来,Clang 成为 Emacs C++ 支持的重要组成部分。 Atila Neves 在 CppCon 2015 上做了演讲:
“Emacs 作为 C++ IDE”
这是一个 16 分钟的演讲,他展示了以下主题的解决方案:
可以在 在这里。
In the recent years, Clang became an important part of the Emacs C++ support. Atila Neves had a talk on CppCon 2015:
"Emacs as a C++ IDE"
It is a 16 minute talk, where he shows solutions for the following topics:
Slides can be found here.
有一个 TFS.el 用于将 emacs 集成到 Microsoft TFS。 它适用于任何 TFS,包括运行 Codeplex.com 的 TFS。
设置的基本步骤:
将 tfs.el 放入您的加载路径中。
在您的 .emacs 文件中:
也在您的 .emacs 文件中,为 tfs 命令设置本地或全局键绑定。 像这样:
There's a TFS.el for emacs integration into Microsoft TFS. It works with any TFS, including the TFS that runs Codeplex.com.
Basic steps to setup:
Place tfs.el in your load-path.
In your .emacs file:
also in your .emacs file, set local or global key bindings for tfs commands. like so:
您可能还会发现 标签栏 很有用。 它模拟了我从 Eclipse 迁移到 Emacs 时唯一错过的行为。 绑定到“,”和“.” 为了移动到上一个和下一个选项卡栏,它使您免于一直通过 Ctrl-x b 切换缓冲区。
不幸的是,上述网页没有提供正确的版本下载。 然而,大多数 Ubuntu 版本都在其 emacs-goodies 包中提供它。
You might also find tabbar useful. It emulates the only behavior I missed when moving from Eclipse to Emacs. Bound to "," and "." for moving to the previous and next tab bar, it relives you from switching the buffer by Ctrl-x b all the time.
Unfortunately, the mentioned web page does not provide the correct version to download. Most Ubuntu versions, however, deliver it in their emacs-goodies packages.
我在 Windows 上使用 emacs。 编译模块很好,但我希望编译能够更智能地了解它建议的编译命令行。 可以使用 "文件变量”来指定编译命令,但我想要比这更聪明的东西。 所以我写了一个小函数来帮忙。 它会猜测要使用的编译命令,以在运行
compile
时提示用户。猜测函数查找 vbproj 或 csproj 或 sln 文件,如果找到,它会建议 msbuild。 然后它查看缓冲区文件名,并根据该文件名提出不同的建议。 .wxs 文件意味着它是一个 WIX 项目,并且您可能想要构建 MSI,因此猜测逻辑建议为 MSI 使用 nmake 命令。 如果它是 Javascript 模块,那么建议运行 jslint-for-wsh.js 来检查 .js 文件。 作为后备方案,它建议使用 nmake。
我使用的代码如下所示:
我尝试将其作为编译函数的“建议之前”执行,但无法使其令人满意地工作。 因此,我定义了一个新函数,并将其绑定到我用于编译的相同按键组合。
I use emacs on Windows. the compile module is nice, but I wanted compile to be smarter about the compile command line it suggests. It's possible to use "File Variables" to specify
compile-command
, but I wanted something a little smarter than that. So I wrote a little function to help out. It guesses the compile command to use, to prompt the user with, when runningcompile
.The guess function looks for a vbproj or csproj or sln file, and if found, it suggests msbuild. Then it looks at the buffer file name, and depending on that, suggests different things. A .wxs file means it's a WIX project, and you likely want to build an MSI, so the guess logic suggests an nmake command for the MSI. If it's a Javascript module, then the suggestion is to run jslint-for-wsh.js to lint the .js file. As a fallback, it suggests nmake.
The code I use looks like this:
I tried doing this as "before advice" for the compile function but couldn't get it to work satisfactorily. So I defined a new function and bound it to the same keystroke combination I have been using for
compile
.compile、next-error 和 previous-error 都是 Emacs 中 C++ 开发非常重要的命令(在 grep 输出上也很有效)。 Etags、visit-tags-table 和 find-tag 也很重要。 Completion.el 是 20 世纪最伟大的无名黑客之一,可以将 C++ 黑客攻击速度提高一个数量级。 哦,我们不要忘记 ediff。
我还没有学会如何在不访问 shell 的情况下使用版本控制,但现在我运行提交的频率如此之高(使用 git),我可能必须这样做。
compile, next-error, and previous-error are all pretty important commands for C++ development in Emacs (works great on grep output too). Etags, visit-tags-table, and find-tag are important as well. completion.el is one of the great unsung hacks of the 20th century, and can speed up your C++ hacking by an order of magnitude. Oh and let's not forget ediff.
I've yet to learn how to use version control without visiting a shell, but now that I'm running commits so much more frequently (with git) I will probably have to.
我知道这是一篇非常古老的帖子。 但这个问题对于 emacs 初学者来说是有效的。
IMO 使用 emacs 作为 ide 的最佳方法是在 emacs 中使用语言服务器协议。 您可以在链接网站中找到有关语言服务器的所有信息。
为了快速设置,我建议您访问此页面 eglot 。 IMO eglot 的工作做得很好。 它与公司等自动完成包集成良好。 提供查找参考等等。
同样对于调试器,您可能需要针对特定语言的特定调试器。 您可以在 emacs 中使用 gdb。 只需输入 Mx gdb 即可。
为了编译代码,最好使用 shell 命令。 我正在开发这个项目eproj。 需要一段时间才能完成。 但它所做的只是将 shell 命令映射到项目类型。 并通过 shell 构建您的项目。 执行命令的作用相同。 我可能需要帮助来完成这个项目。 它尚未准备好使用,但如果您了解一点 elisp,则可以浏览代码。
除此之外,最好使用 emacs 编译命令。
对于版本控制,我还没有看到任何其他软件包可以与 magit 的功能相媲美。 这是特定于 git 的。 另外,对于 git,还有另一个包 git-timemachine,我发现它非常有用。
对象查找和类查找由语言服务器协议提供。
项目树可用于 treemacs 的 IDE 接口。
还有一个名为 projectile 的项目交互库。
对于自动完成,我发现 company-mode 非常有用。
真正的 emacs 可以做任何事情。
I know this is a very old post. But this question is valid for emacs beginners.
IMO the best way to use emacs as an ide is to use a language server protocol with emacs. You can find all the information about language servers in the linked website.
For a quick setup, i would urge you to go to this page eglot . IMO eglot does it's job pretty well. It integrates well with auto completions packages like company. Provides find reference, and more.
Also for a debugger, you may need specific debuggers for specific languages. You can use gdb from within emacs. Just type
M-x gdb
.For compiling your code, it's best to use shell-commands. I am working on this project eproj. It's gonna take a while to complete it. But all it does is maps shell command to project type. And builds you project via shell. It does the same to execute command. I may need help completing this project. It's not ready for use, but if you know a bit of elisp you can go through the code.
That aside, it's always best to use the emacs compile command.
For version control, I haven't yet seen any other package which can match the power of magit. It's specific to git. Also for git there is another package git-timemachine, which i find very useful.
Object lookup and class lookup is provided by language server protocol.
A project tree can be used for ide like interface with treemacs.
There is also a project Interaction Library called projectile.
For auto completion, I find company-mode very useful.
Truly emacs can be made to do anything.
探索 Emacs VC 功能的起点(可能并不明显)是 Mx vc-next-action。
它根据文件和 VC 后端的状态对当前文件执行“下一个逻辑版本控制操作”。 因此,如果文件不受版本控制,它会注册它,如果文件已更改,则会提交更改等。
这需要一点时间来适应,但我发现它非常有用。
默认键绑定是Cx v v
A starting point (which may be non-obvious) for exploring the VC features of Emacs is M-x vc-next-action.
It does the "next logical version control operation" on the current file, depending on the state of the file and the VC backend. So if the file is not under version control, it registers it, if the file has been changed, the changes are submitted etc.
It takes a little getting used to, but I find it very useful.
Default keybinding is C-x v v
关于文档查找:这取决于您的编程语言。
C 库和系统调用通常记录在手册页中。 为此,您可以使用
Mx man
。 有些事情在信息页面中可能会得到更好的记录; 使用Mx信息
。对于 elisp 本身,请使用
Ch f
。 对于 python,请使用>>> 解释器中的help(<函数、类、模块>)
。我发现大多数其他语言都提供 html 形式的文档。 为此,请尝试使用嵌入式浏览器(我使用 w3m)。 将您的
BROWSER
环境变量设置为emacsclient -e "(w3m-goto-url-new-session \"$@\")"
周围的包装器脚本(在 *nix 上) ),以防某些东西可能打开浏览器并且您希望它在 emacs 中打开。On documentation lookup: that depends on your programming language(s).
C libraries and system calls are typically documented in man pages. For that you can use
M-x man
. Some things may be documented better in info pages; useM-x info
.For elisp itself, use
C-h f
. For python, use>>> help(<function, class, module>)
in the interpreter.I find that most other languages offer documentation in html form. For that, try an embedded browser (I use w3m). Set your
BROWSER
environment variable to a wrapper script aroundemacsclient -e "(w3m-goto-url-new-session \"$@\")"
(on *nix), in case something might open a browser and you want it opened inside emacs.尝试 lsp 模式。 现在您可以使用 emacs 内的其他 IDE 功能连接到服务器。 查找更多信息:lsp-mode
Try lsp-mode. Now you can use other IDE functionality inside emacs connecting to server. Look for more info: lsp-mode
在 Unix 或 X windows 风格中,我不知道是否有一个适用于所有情况的集成 IDE。
要与调试器交互(只是 IDE 的一个组件),请考虑 realgud。 我发现它有用的另一件事是位置消息的解析器,因此,如果您有调用堆栈跟踪并且想要在调用堆栈中的特定位置进行编辑,这个前端界面将可以做到这一点。
到目前为止,这个程序还需要改进。 但它也可以利用相关人员来改进它。
免责声明:我在 realgud 工作
In the Unix or X windows style, I don't know that there is an integrated IDE that works for everything.
For interacting with debuggers, just one component of an IDE, consider realgud. The other thing it has that I find useful are parsers for location messages, so that if you have a call stack trace and want to edit at a particular place in the callstack, this front-end interface will can do that.
By far this program could use improvement. But then it could also use people working on it to improve it.
Disclaimer: I work on realgud
您必须具体说明“其余”的含义。 除了对象检查器(我知道)之外,emacs 非常容易地完成上述所有操作:
Mxcompile
并输入编译命令。从那里开始,您只需Mx 编译
并使用默认值即可捕获 C/C++ 编译器错误(与 GCC 配合使用效果最佳)并帮助您导航到有警告或错误的行Mx gdb
,它将创建一个带有特殊绑定的 gdb 缓冲区编辑:我意识到我关于文档查找的答案确实与代码导航有关。更多要点信息:
谷歌搜索无疑会揭示更多的例子。
正如第二个链接所示,即使不支持开箱即用,也可以在其他文档中查找函数(以及其他内容)。
You'll have to be specific as to what you mean by "the rest". Except for the object inspector (that I"m aware of), emacs does all the above quite easily:
M-x compile
and enter your compile command. From there on, you can justM-x compile
and use the default. Emacs will capture C/C++ compiler errors (works best with GCC) and help you navigate to lines with warnings or errors.M-x gdb
and it will create a gdb buffer with special bindingsEdit: I realize my answer about documentation lookup really pertained to code navigation. Here's some more to-the-point info:
Google searching will no doubt reveal further examples.
As the second link shows, looking up functions (and whatever) in other documentation can be done, even if not supported out of the box.
对于版本控制,您可以使用多种方法,具体取决于您使用的版本控制系统。 但有些功能是所有这些功能所共有的。
vc.el 是在文件级别处理版本控制的内置方法。 它具有大多数版本控制系统的后端。 例如,Emacs 附带了 Subversion 后端,还有 git 后端和其他来源提供的其他后端。
最有用的命令是 Cx v v (vc-next-action),它对您正在访问的文件执行适当的下一个操作。 这可能意味着从存储库更新或提交更改,如果您使用的系统需要它(例如 RCS),vc.el 还会重新绑定 Cx Cq 来签入和签出文件。
其他非常有用的命令是 Cx v l 和 Cx v =,它们显示您正在使用的文件的日志和当前差异。
但为了真正提高工作效率,除了简单的事情之外,您应该避免使用单文件 vc.el 命令。 有几个包可以让您概览整个树的状态,并为您提供更多功能,更不用说创建跨多个文件的连贯提交的能力了。
其中大多数都深受 CVS 的影响或基于原始的 pcl-cvs/pcvs。 甚至有两个是 subversion 附带的,psvn.el 和 dsvn.el。 有 git 等的软件包。
For version control, there are several things that you can use, depending on what version control system you use. But some of the functionality is common to all of them.
vc.el is the built-in way to handle version control at a file level. It has backends for most version control systems. For instance, the Subversion backend comes with Emacs, and there are git backends and others available from other sources.
The most useful command is C-x v v (vc-next-action) that does the appropriate next action for the file you are visiting. This might mean updating from the repository or commiting your changes, vc.el also rebinds C-x C-q to check in and out files if you are using a system that needs it (like RCS).
Other very useful commands are C-x v l and C-x v = that show you the log and current diff for the file you are using.
But for real productivity, you should avoid using the single-file vc.el commands other than for simple things. There are several packages that can give you an overview of the status of your whole tree, and give you more power, and not to mention the ability to create coherent commits spanning several files.
Most of these are heavily influenced or based on the original pcl-cvs/pcvs for CVS. There are even two of them that comes with subversion, psvn.el and dsvn.el. There are packages for git etc.
我不得不推荐 Emacs Code Browser 作为更“传统”的 IDE 风格环境对于 emacs。
编辑:我现在还高度推荐 Magit ,而不是标准 VCS 界面emacs。
I have to recommend Emacs Code Browser as a more "traditional" IDE style environment for emacs.
EDIT: I also now recommend Magit highly over the standard VCS interface in emacs.
您是否尝试过 Mx 编译,而不是在 shell 窗口中运行 make 命令? 它将运行 make 命令,显示错误,并且在许多情况下,如果输出包含文件名和行号,则可以很容易地跳转到导致错误的代码行。
如果您是 IDE 的粉丝,您可能还想看看 emacs 的 speedbar 软件包 (Mx speedbar)。 而且,如果您还没有了解如何使用标签表来导航您的代码,请了解一下。
Instead of running a make command in the shell window, have you tried M-x compile? It will run your make command, display errors, and in many cases make it very easy to jump to the line of code that caused the error if the output includes filenames and line numbers.
If you're a fan of IDEs, you might also want to look at emacs' speedbar package (M-x speedbar). And, if you haven't already, learn about how to use tags tables to navigate your code.
一旦发现 emacs 的某些角落,它们就能以你从未想过的方式提高工作效率。 正如其他人提到的,使用标签是缩放源代码的一种奇妙且快速的方法,并且使用 M-/ (dabbrev-expand) 通常可以完全满足您在完成变量名称时所期望的效果。
使用“occurrence”对于获取包含缓冲区中所有出现的正则表达式的缓冲区非常有用。 当重构代码和查找代码片段或变量的使用时,或者如果您在源文件中使用 TODO 标记并且想要访问它们全部时,这非常方便。
冲洗行、排序数字字段、替换正则表达式和矩形函数对于从某些工具中获取转储并将其转换为有用的数据(例如 elisp 程序或逗号分隔的电子表格)非常有用。
我写了一个关于 IDE 的页面,就像你可以用 emacs 做的事情
http://justinsboringpage.blogspot.com/2007/09/11-visual-studio-tricks-in-emacs.html
学习 elisp 是另一种很好的方法,可以让你自己回答 emacs 之外还能做什么典型的 IDE 可以做什么。
例如,我在博客中写过关于编写 Perforce 辅助函数的文章,例如blame(编写您自己的函数意味着您可以使其完全按照您想要的方式运行)...
http://justinsboringpage.blogspot.com/2009/01/who-changed-line-your-working-on-last。 我还
编写了代码,可以动态地为某个函数创建注释,这与我正在使用的编码标准相匹配。
我的 elisp 代码都不是特别出色,而且其中大部分已经存在于库中,但是能够让 emacs 执行工作日中出现的自定义内容确实非常有用。
There are corners of emacs that once discovered make you more productive in ways you never thought of. As others have mentioned, using tags is a fantastic and fast way to zoom around your source code and using M-/ (dabbrev-expand) often does exactly what you expect when completing a variable name.
Using occur is useful to get a buffer with all occurences of a regular expression in a buffer. That's really handy when refactoring code and looking for fragments of code or uses of variables, or if you use TODO markers in your source files and you want to visit them all.
flush-lines, sort-numeric-fields, replace-regexp and rectangle functions can be really useful for taking a dump from some tool and converting it to useful data such as an elisp program or a comma delimited spreadsheet.
I wrote a page about IDE like things you can do with emacs
http://justinsboringpage.blogspot.com/2007/09/11-visual-studio-tricks-in-emacs.html
Learning elisp is a another great way to answer for yourself what else emacs can do beyond what a typical IDE can do.
For example I've blogged about writing Perforce helper functions like blame (writing your own means you can make it behave exactly as you want)...
http://justinsboringpage.blogspot.com/2009/01/who-changed-line-your-working-on-last.html
I've also written code that dynamically creates comments for a function at point, that matches the coding standards I'm working with.
None of my elisp code is particularly great, and most of it exists already in libraries, but it's really useful to be able to make emacs do custom stuff that just comes up during a working day.
您可以找到 emacs & 的详细说明; 我的网站上的版本控制集成。 我还在撰写有关使用 Emacs 作为多种语言的开发环境的文章 - C/C++、Java、Perl、Lisp/Scheme、Erlang 等...
You can find detailed description of emacs & version control integration on my site. I'm also working on article about using Emacs as Development Environment for many languages - C/C++, Java, Perl, Lisp/Scheme, Erlang, etc...
好的,这里的每个人都给出了完美的提示,使 emacs 成为一个出色的 IDE。
但任何人都应该记住,当您使用大量扩展(尤其是用于动态类型检查、函数定义查找等的扩展)自定义 emacs 时,您的 emacs 对于编辑器来说加载速度会非常非常慢。
要解决此问题,我强烈建议在服务器模式下使用 emacs。
它使用起来非常简单,无需自定义您的初始化文件。
你只需要以守护进程模式启动 emacs 即可;
这将创建一个 emacs 服务器,然后您可以从终端或 gui 连接它。 我还建议创建一些别名以方便调用。
这样,emacs 的启动速度将比 gedit 更快,我保证。
这里可能存在一个问题,如果您从临时用户运行 emacs 守护进程,您可能无法以
root
身份连接 emacs 服务器。因此,如果您需要打开具有 root 访问权限的文件; 使用
tramp
代替。 只需使用普通用户运行 emacs 客户端并打开这样的文件即可;这让我的生活变得更轻松,我可以通过这种方式在几毫秒内打开我的重型定制Python IDE。 您可能还想将 emacs --daemon 添加到系统启动中,或为 emacsclient 创建桌面文件。 这取决于你。
有关 emacs 守护进程和 emacs 客户端的更多信息可以在 wiki 中找到;
http://www.emacswiki.org/emacs/EmacsAsDaemon
http://www.emacswiki.org/emacs/EmacsClient
Okay, everyone here is giving perfect hints to make emacs a great IDE.
But anyone should keep in mind that, when you customize your emacs with a lot of extension (especially with the ones for type-checking on the fly, function definition lookups etc) your emacs will load very, very slow for an editor.
To workaround this, I would highly recommend to use emacs in
server mode
.It is pretty simple to use, no need to customize your init file.
You just need to start emacs in daemon mode;
This will create an emacs server, then you can connect it either from terminal, or from gui. I'd also recommend to create some aliases to make it easy to call.
This way, emacs will fire up even faster than gedit, promise.
The one possible problem here, if you are running emacs daemon from your casual user, you probably can't connect emacs server as
root
.So, if you need to open a file that has root access; use
tramp
instead. Just run your emacs client with your normal user and open files like this;This made my life easier, I can open my heavy customized python IDE in miliseconds this way. You may also want to add emacs --daemon to your system startup, or create a desktop file for emacsclient. Thats up to you.
More on emacs daemon and emacs client can be found at wiki;
http://www.emacswiki.org/emacs/EmacsAsDaemon
http://www.emacswiki.org/emacs/EmacsClient