Using Emacs, Beginner Level

Hello, let’s talk about development environments.

Currently, Emacs is my preferred editor, I’m still a beginner with it but it already helps me a lot on C, and sometimes Python, programming.

emacs

I started using Emacs to work with remote files, making ssh connections and accessing files is very easy with it, and it provides many more features than nano, the editor I was using at the time. When learning the basics, I read much about its power and adaptability  and took as a challenge to turn it into a good Python IDE.

I’m afraid I failed this challenge, since I still prefer Pycharm; mainly because of better autocomplete and documentation access, I couldn’t configure a good documentation search tool with Emacs, maybe a more skilled user is able to. However, I still use Emacs to write small scripts, Pycharm is heavy and hangs too much, so it’s something 🙂

I tried it with C++ and now Emacs is my main C++ IDE, I use it mostly to play with Codeforces problems. It helps to have three windows available in the same screen, one for the main code, another for the custom input and a terminal window.

Anyway, too much was said about why I use it, and that’s not the purpose of this post. I intend to show how Emacs can help you to work with huge codebases, similar to the ones found in Linux Kernel.

Before we begin, be aware, Emacs will mess with indentation, you should be careful when configuring it. The configuration I use seems ok, you can find it at: https://github.com/Elise-Lennion/Emacs. Rename emacs_init to .emacs and place it at ~/, then Emacs should load this configuration at launch.

Now you’re ready to use it on Open Source C projects, but what does it offer? I can show you the features which help me most.

On the following paragraphs, C refers to Ctrl and M (Meta) to Alt. If the command is a function name (e. g. occur), you should type M-x and the command name, you can configure custom key bindings to any command you choose.

Split window: This feature is a built-in of Emacs, you can split a view horizontally, C-x 2, or vertically, C-x 3. It’s the most useful feature mentioned here, you can see multiple files you’re working on at the same time, just beautiful. To navigate between windows, press C-x ‘direction arrow’, the configuration file I use also sets ‘Windows key’-‘direction arrow’ to make navigation faster.

emacs_split_screen
Emacs multiple screens

Highlight 80+ columns: All characters after 79th column are highlighted, so you know when a line won’t be properly displayed in a terminal window, simple and prevents checkpatch warnings.

Interactive completion: To open a file you should type C-x C-f, then a little window at the bottom of the screen will appear, where you type the filename. Using Interactive completion, all files in the current folder are listed, also, it filters the displayed files to match what you’re typing. This speeds codebase navigation a lot, no need to use ls in every folder, also, typing filenames can be frustrating slow when there are many similar names.

String search, occur: Emacs offers a lot of string search methods, search-forward, search-backwardRegexp matching, occur and more. They all have their uses but occur is special. It displays all occurrences of a string in a new interactive window, when you click on an occurrence your screen will be directed to it. That’s a blessing when trying to understand what a specific function does and where it’s called.

emacs_occur
Occur example

Built-in terminal: Emacs isn’t simply a IDE, you can open a terminal window inside it, with M-x shell, no need to mention what this offers. However, the terminal isn’t always 100% functional, e.g. it doesn’t work properly when using ‘man’ or ‘git grep’. Usually I have a dedicated terminal window open.

Some useful commands: C-g cancels your current action, M-w is similar to Ctrl+c, C-w to Ctrl+x and C-y to Ctrl+v, if it pastes the wrong text try to use M-y afterwards.

These features are very helpful, and are only very basic functionalities of Emacs. For further readings, there are many easy to find tutorials about Emacs, it is 40 years old, the community and references are very developed, and there is much to discover.

That’s a beginner view on why it’s good and how to use Emacs on C projects, code navigation will be hard at first but you’ll get used to it. I’ll share more features when I learn them, magit is next  🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s