{"id":8,"date":"2014-11-19T19:33:00","date_gmt":"2014-11-19T19:33:00","guid":{"rendered":""},"modified":"2016-02-25T18:09:02","modified_gmt":"2016-02-25T18:09:02","slug":"autocompletion-for-c-and-c-with-emacs-24","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=8","title":{"rendered":"Autocompletion for C and C++ with Emacs 24"},"content":{"rendered":"<p>I have already done a <a href=\"https:\/\/www.root42.de\/blog\/?p=51\">blog<\/a> post on auto completion using Emacs. But that was back in Emacs 23 days. Long ago&#8230;<\/p>\n<p>Since then a lot has happened. <a href=\"http:\/\/www.gnu.org\/software\/emacs\/\">Emacs 24<\/a> has been released, package managers like <a href=\"http:\/\/melpa.milkbox.net\/\">MELPA<\/a> or <a href=\"http:\/\/elpa.gnu.org\/\">ELPA<\/a> have become standard, and <a href=\"http:\/\/company-mode.github.io\/\">company-mode<\/a> seems to be winning against <a href=\"https:\/\/github.com\/auto-complete\/auto-complete\">auto-complete<\/a>. Also, <a href=\"http:\/\/clang.llvm.org\/\">clang<\/a> has made huge strides forward.<\/p>\n<p>So it is time to revisit the task of developing C or C++ using Emacs. I have put <a href=\"https:\/\/github.com\/root42\/yet-another-emacs-init-el\">online an easy-to-install Emacs init.el<\/a> that you can use as a start for your own development environment. I am using the OS X version of Emacs, but this should also work on Linux, given that you have clang and git installed.<\/p>\n<p>You can install this init.el by issuing the following commands:<\/p>\n<pre>cd<br \/>git clone https:\/\/github.com\/root42\/yet-another-emacs-init-el<br \/>mkdir ~\/.emacs.d<br \/>cd ~\/.emacs.d<br \/>ln -snf ..\/yet-another-emacs-init-el\/init.el .<br \/><\/pre>\n<p>The file will make sure that upon startup of Emacs all necessary packages are installed, like company, magit but also LaTeX tools like aucTeX or refTeX. You can disable this check (or individual packages) in the init.el.<\/p>\n<p>The code is up on <a href=\"https:\/\/github.com\/root42\/yet-another-emacs-init-el\">github<\/a>, so feel free to fork and\/or contribute.<\/p>\n<p>When doing simple C++ programming using only standard libraries, you should be ready to go. For more complex projects, you have to tweak the clang parameters, so that the compiler will find the header files. Completion happens automatically after &#8220;<span style=\"font-family: Courier New, Courier, monospace;\">.<\/span>&#8220;, &#8220;<span style=\"font-family: Courier New, Courier, monospace;\">-&gt;<\/span>&#8221; and &#8220;<span style=\"font-family: Courier New, Courier, monospace;\">::<\/span>&#8221; but also when pressing M-\/. You can rebind this key in the init.el, of course. This is what the completion looks like:<\/p>\n<div style=\"clear: both; text-align: center;\"><\/div>\n<div style=\"clear: both; text-align: center;\"><\/div>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/2.bp.blogspot.com\/-82aIcc-VOOA\/VGzyClUyeEI\/AAAAAAAADcI\/ywTsLSznV-Q\/s1600\/company.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" border=\"0\" src=\"http:\/\/2.bp.blogspot.com\/-82aIcc-VOOA\/VGzyClUyeEI\/AAAAAAAADcI\/ywTsLSznV-Q\/s1600\/company.png\" height=\"460\" width=\"640\" \/><\/a><\/div>\n<div style=\"clear: both; text-align: center;\"><\/div>\n<div style=\"clear: both; text-align: left;\">My most commonly used keys that I have re-bound are as follows:<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<ul>\n<li>f3 &#8211;&nbsp;Runs ff-find-other-file, trying to switch between header and implementation for C\/C++ programs.<\/li>\n<li>f4 &#8211; Toggles the last two used buffers.<\/li>\n<li>f5, f6 &#8211;&nbsp;If tabbar is enabled (tabbar-mode), navigates back\/forward through tabs.<\/li>\n<li>f7 &#8211;&nbsp;Toggle ispell dictionaries (german\/english).<\/li>\n<li>f8 &#8211; Kill current buffer.<\/li>\n<li>f9 &#8211; Run compile.<\/li>\n<li>M-? &#8211; Run grep.<\/li>\n<li>M-n &#8211; Go to next error in compilation buffer.<\/li>\n<li>M-S-n &#8211; Go to previous error in compilation buffer.<\/li>\n<li>M-&gt;, M-&lt; &#8211; Go to next\/previous Emacs frame.<\/li>\n<li>M-\/ &#8211; Run autocompletion using company mode.<\/li>\n<li>C-x o, C-x C-o &#8211; Go to next\/previous Emacs window<\/li>\n<\/ul>\n<p>Very nice is also the <a href=\"http:\/\/magit.github.io\/\">magit-mode<\/a>, which is a very sane interface to git for Emacs. It looks like this:<\/p>\n<div style=\"clear: both; text-align: center;\"><\/div>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/4.bp.blogspot.com\/-JTLOKPAaR8s\/VGzySVb5QvI\/AAAAAAAADcQ\/HUZYAEOQqjM\/s1600\/magit.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" border=\"0\" src=\"http:\/\/4.bp.blogspot.com\/-JTLOKPAaR8s\/VGzySVb5QvI\/AAAAAAAADcQ\/HUZYAEOQqjM\/s1600\/magit.png\" height=\"506\" width=\"640\" \/><\/a><\/div>\n<p>You can run it by executing <span style=\"font-family: Courier New, Courier, monospace;\">M-x magit-status<\/span>. Just type <span style=\"font-family: Courier New, Courier, monospace;\">?<\/span> to get online help. Magit uses simple one-character-commands, like <span style=\"font-family: Courier New, Courier, monospace;\">s<\/span> for stage, <span style=\"font-family: Courier New, Courier, monospace;\">c<\/span> for commit, <span style=\"font-family: Courier New, Courier, monospace;\">p<\/span> for push and so on.<\/p>\n<p>Currently I am evaluating the integration of lldb into Emacs, but haven&#8217;t come far enough to say that I have found a powerful and flexible interface, apart from the standard command line. So there&#8217;s more to come, hopefully!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have already done a blog post on auto completion using Emacs. But that was back in Emacs 23 days. Long ago&#8230; Since then a lot has happened. Emacs 24 has been released, package managers like MELPA or ELPA have become standard, and company-mode seems to be winning against auto-complete. Also, clang has made huge &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=8\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Autocompletion for C and C++ with Emacs 24&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,6,2,5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":1,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}