Written by Ricardo Iglesias, with edits by Nick Troccoli
Eager to take advantage of more advanced Emacs features? While these are completely optional, you are welcome to take advantage of some of these tips if you'd like.
This mode comes built-in, but is turned off by default. When on, emacs will highlight your code whenever you go past a certain line length (e.g. if you want to follow an 80-column) rule. To do so, open your Emacs configuration file (
emacs ~/.emacs) and add this at the end - feel free to customize the column width value however you'd like:
(require 'whitespace) (setq whitespace-style '(face empty tabs lines-tail trailing)) (setq whitespace-line-column 80) ;; Or whatever other column number. (global-whitespace-mode t)
This enables whitespace highlighting for any file you open in emacs. If you want it to just activate in certain cases, for instance just when you open a C file, you can use an Emacs "hook", which lets you tell Emacs to do something only in certain cases. For instance, in these commands, we can tell Emacs to only turn on whitespace mode when a C file is opened. If you want to use this, replace the line
(global-whitespace-mode t) that you added above with the following two lines:
;; Turn on WS mode when entering a c file. (add-hook 'c-mode-common-hook 'whitespace-mode)
A nice feature to have is to automatically break a long comment into multiple shorter lines. A manual way to do this is to highlight the comment and enter
M-x fill-region, but you can also have Emacs do this automatically for you as you type out a comment. To turn this feature on, add the following to your
(add-hook 'c-mode-common-hook 'auto-fill-mode) (setq comment-auto-fill-only-comments t)
Once the editor splits your comment lines, it's a good stylistic idea to change any multiline
// comments to
/* */ comments instead.
Electric Pair Mode
This feature automatically fills in a closing brace / quote when you enter an opening one. To turn this feature on, add the following to your
(add-hook 'c-mode-common-hook 'electric-pair-mode)
Interactive Manual Pages
Emacs has a very useful function that allows you to interactively search through man pages. To bring it up, run
M-x man, and type in the manual page you're searching for.
Remap Your Control Key
If you find yourself struggling to reach all the way to the bottom-left corner of your keyboard for emacs's many
C- commands, one recommendation is to remap your caps-lock key to your control key and vice versa, which swaps the keys on your keyboard. What this does is essentially put the control key on your home row, which allows you to have your fingers move less. This doesn't work for everyone, but try it out and see if you like it! The instructions for doing this vary by platform, so try doing a quick Google search for your particular platform - but for instance, this remapping capability is built in to Macs (see instructions here) and there are a variety of different utility programs that you can find for Windows.
Emacs's out-of-the-box functionality is fairly impressive, but you might have noticed that it is a little unclear how to extend it to act more like a modern IDE like QT. Lucikly, Emacs has a package manager built-in to extend its functionality. Before you get new packages, you first need to tell Emacs where you want it to look to install packages. To do this, open your emacs configuration file (
emacs ~/.emacs) and add the following at the end:
(require 'package) (add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t) (package-initialize)
To actually install packages, you have to run
M-x package-list-packages within Emacs. (What this means is, press the meta key and x, then type "package-list-packages", then press ENTER).
Once you run this command, you have a list of all available packages, and a short description for what the package does. You can use this screen to install packages by putting your cursor on a package, pressing ENTER, then pressing ENTER again on the "Install" button. There are lots of packages for you to explore! Here is one in particular that might be useful:
Automatically Check For Compiler Issues with Flycheck
Instead of waiting for your compiler to tell you when you're writing code that will emit warnings from the compiler, you can have this feedback come while you're editing your program. To do so, first install the
flycheck package using the package list above. Once you've done this, then also add the following at the end of your
(global-flycheck-mode) (add-hook 'c-mode-common-hook (lambda () (setq flycheck-gcc-language-standard "c++11")))
From now on, when you are editing a C file, you'll see feedback as you type. Specifically, look for code bolded and underlined, and navigate your cursor over that content and wait a few seconds to see the issue message pop up in the Emacs console at the bottom.