Practical Emacs Guide for C++ developers
Author: Chen Bin <chenbin DOT sh AT gmail DOT com>
This guide focus on practical side. I’m already an expert of IDEs (Visual Studio, Eclipse, IntelliJ, Qt Creator ….) before turning to Emacs. So I will introduce you the features that is equals to the quality of professional IDEs.
Intellisense (smart code completion)
It’s called intellisense in Visual Studio. It means when you insert a object name and dot character, the object’s method or member is automatically completed.
Many people say Cedet is a complete solution. I don’t use it because it’s too slow and hard to setup.
There are two alternatives,
To make auto-complete work in C++ code, you need also install another plugin auto-complete-clang.
company-mode don’t need any other plugin. I use company-mode because it’s actively maintained.
Both auto-complete and company-mode require Clang installed if you use Clang as a parser.
Clang is just one of many backend command line tool which can extract useful meta information from C/C++ headers.
GNU Global is the alternative of Clang which I highly recommended. company-mode support it out of the box.
Pick up either Clang or GNU Global as backend tool.
Both company-mode and auto-complete can complete lots of other things like file name, line, dicitionary. I suggest turning of those features when coding in C++ because too many candidates for code completion will distract you.
Whatever you use as backend parser, it will fail if you over-engineered your code. For example, multiple inheritance plus some C++ template? Don’t complain that Emacs sucks in your bloated C++ code. It’s your code sucks. Write the simplest code just works.
I recommend two books for anyone whose code Clang or GNU Global cannot handle,
Command line version is good enough for me. I don’t use any Emacs plugins.
- I use evil-mode, vim key binding, end of story
- complete line in evil-mode is actually very useful
- search and replace in evil-mode is even better than the orginal vim one
- I use evil-nerd-commenter to comment/uncomment code
- for navigation in current file, I use imenu
- for navigation between files, you can use cscope or gtags
- both are good enough for me. I use gtags simply because it’s updated more often
- ctags is good for C and simple C++ code. Avoid writing complex C++ code could be a better idea. Then you can use ctags.
flymake (real time syntax check)
- I found it a huge boost to my productivity
- The headache to write makefile to make flymake work is huge. For example, Manually insert correct include C++ header directories into makefile.
- I suggest use cmake and cpputils-cmake to do this job automatically
- compile mode in Emacs. All the handy hot keys to locate the next previous compile error.
- you can change variable compile-command from “make” to “make -C /whatever/directory/you/want” in elisp.
- hide the compiling message window in elisp when compiling succeeds.
- I use M-x gud-gdb which works on gdb 6.3. On OSX 10.7.3, gdb 6.3 is the installed by default.
- Set hot key for gud-run
- Or only use the cli gdb plus some tile window manager like i3. Because for real world debugging, the actual thing you need do is watch the variable, read the context of the code.
- Gud and dired could be programmically changed, so you can jump to the directory of executable easily
- The only meaningful documentation these days for me is to search google, search stackoverflow. So I need trigger firefox from Emacs.
- It’s useful when you copy/paste code from firefox
- I found the only reliable way is use cli tool for cross-platform development. xclip is not as reliable as xsel.
- jump to the corresponding header. You know “M-x ff-find-other-file”? In real projects, you need give set up 3rd party C++ directories for search. cpputils-cmake can help you. But of course you can do it manually.
- ido is good
- helm plugins are fine. But “M-x helm-ls-git-ls” is very useful if you use git.
- sometime I just use “M-x rgrep”
- Os grep plus percol by mooz plus some handy clipboard bash function plus “:e” in evil-mode is very useful to search and open file quickly
- I don’t understand why everything should be done in Emacs, Some handy shell tools plus Emacs is much more powerful.
- I usually open 4~8 sub-windows
- Use window-numbering.el, the hotkeys are “M-NUM”.
- I use evil-mode, so no need to input numeric argument
- I am also using other window navigation plugins, but they are rarely used.
- winner-mode, undo the windows layout
- I use smex to input commands because I cannot remember hot keys because hot keys are different in different modes.
- My tip is to assign a unique hot key for yas/expand. So you can use single character as the key of your most frequently used snippet. “a” for assert “l” for log and “i” for include. Other snippets are rarely used actually.