Skip to content

Latest commit

 

History

History
167 lines (104 loc) · 6.91 KB

02-the-basics.org

File metadata and controls

167 lines (104 loc) · 6.91 KB

Emacs for developers - The basics

Emacs for developers - The basics

Config files

In order to customize Emacs, you will have to edit its configuration files. When you install the software, the configuration file does not exist (or is empty) and does nothing until you decide to tweak Emacs. Some people use Emacs for decades without any extra configuration because they like it out of the box.

There are several ways to manage your Emacs configuration:

  • Simply create and edit a ~/.emacs file
  • Create a ~/.emacs.d/ directory with a ~/.emacs.d/init.el file

The first solution is great if you have a small, clean and exportable configuration file.

The second solution is great if you need several configuration files (keep your configuration parts separated) or you need to install external extensions (you will install them in ~/.emacs.d to keep them centralized and exportable).

Executing commands

The main idea of Emacs is that everything is a command. For example: opening a file with a keybinding calls a command that is executed by the Emacs’ core. And all the commands are written in Lisp.

To call a command, you simply have to press Escape and then x. It could be Alt-x for more comfort. The official name is ”Meta x” and is written M-x

If you type this key sequence, you will see at the bottom of the editor a little prompt. This part of Emacs is called the mini buffer and is used for every interactive actions.

Let’s try something:

  • type M-x
  • Then, in the minibuffer, type version and RET (return)

Emacs should display the current version number of the software.

../images/version.gif

Congratulations! You just called your first Emacs command.

Key bindings

Here are the most useful keys in Emacs. You will use them several hundreds (or thousands) of times a day.

How does it work?

  • The sequence C-something means that you have to press and hold the Control key while pressing the something key. Example: C-a means Control + a.
  • The sequence C-something somethingelse means that you press Control + something, then you release Control to press somethingelse.
  • The sequence C-something C-somethingelse means that you have to press Control while pressing something, then releasing something and pressing somethingelse.

Manipulate files

  • C-x C-f: open a file, which corresponds to M-x find-file
  • C-x C-s: save a file, which corresponds to M-x save-buffer

../images/open-and-save.gif

Manipulate the current line

  • C-a: go to the beginning of the current line, which corresponds to M-x begining-of-line
  • C-e: go to the end of the current line, which corresponds to M-x end-of-line
  • C-k: cut the rest of the line at the cursor position, which corresponds to M-x kill-line
  • C-y: paste what you had cut with the command above, which corresponds to M-x yank
  • C-l: center the buffer at the cursor position, which corresponds to M-x recenter-top-bottom

../images/line.gif

Selecting a region

Regions are selections, in Emacs.

They are composed by two points. A starting one and and an ending one.

To select the starting point, move your cursor to the wanted location and simply press C-Space. In your minibuffer, you should have seen a message like “Mark set”. Now, move your cursor to the location you wantyour selection to end.

You have now a selection (or a region) delimited by the place you set the first mark and your cursor. With this region, you can do what ever you want:

  • Copy (M-w)
  • Cut (C-k)
  • Paste (C-y)
  • Replace (M-%)
  • … etc

../images/selecting.gif

Commenting

In order to comment a code block, select the region you want to comment and use:

  • M-x comment-region in order to comment the block
  • M-x uncomment-region in order to uncomment the block

Of course, Emacs knows, according to the major mode loaded how to comment properly your code. If the major-mode is ruby-mode, it will add a “#” before the line; if the major-mode is lisp-mode, it will add “;;”, and so on and so forth.

../images/comment.gif

Windows

This might be confusing but a window is a part of a frame.

An Emacs frame is the window that you opened when you launched Emacs.

An Emacs frame can be divided into windows in itself.

  • C-x 2: open a new window horizontally, which corresponds to M-x split-window-below
  • C-x 3: open a new window vertically, which corresponds to M-x split-window-right
  • C-x o: switch to the next window, which corresponds to M-x other-window
  • C-x 0: close the current window, which corresponds to M-x delete-window
  • C-x 1: close all the windows but the one focused, which correspond to M-x delete-other-windows

../images/windows.gif

Buffers

A buffer is displayed in a frame.

  • C-x b: switch to an already opened buffer, which corresponds to M-x switch-to-buffer
  • C-x C-b: list opened buffer (and jump to the selected one), which corresponds to M-x list-buffers
  • C-x k: kill a buffer, which corresponds to M-x kill-buffer

On most read-only buffers, press q to close it.

../images/buffers.gif

Cancel a command

  • C-g or ESC ESC ESC: cancel the current command running in the minibuffer, which corresponds to M-x keyboard-quit

It also cancels all previously pressed keys.

The most useless command

  • C-x C-c: quit emacs (use at your own risks!), which corresponds to M-x save-buffers-kill-terminal

Help!

Emacs has a powerful built-in help system for key bindings and internal functionalities.

  • C-h f <function-name>: Find the key binding corresponding to <function-name> (ex: C-h f save-buffer)
  • C-h k <key-sequence>: Find the function name corresponding to <key-sequence> (ex: C-h k C-x C-s)
  • C-h v var: Display the documentation of the Lisp variable var (describe-variable) (ex: C-h v emacs-version)
  • <key-sequence> C-h: Show the next possible keys to press after the <key-sequence> (ex: C-x 4 C-h)

For more detail about Emacs help system, see: https://www.gnu.org/software/emacs/manual/html_node/emacs/Help-Summary.html

When executing these commands, a new frame opens. To close it, switch to it (C-x o) and type q. If not, simply close it (C-x 0)

After executing a command with M-x, if the command have a corresponding keybinding bound to it, a message indicating what to press will appear in the minibuffer.

Emacs also includes the full manual (also available online: http://www.gnu.org/software/emacs/manual/html_node/emacs/)

  • C-h r: browse the Emacs manual within Emacs

Finally, there are so many other help functions: http://www.emacswiki.org/emacs/EmacsNewbieHelpReference