Weekly Emacs tip #7: functions
This week’s “tip” is not so much a tip as it is a bit of background on how Emacs works. A bit of extra knowledge that will hopefully help you in your Emacs journey.
Although you may not realise it, many of your interactions with Emacs consist of calling functions. Keybindings and menu items call functions. In fact, most functions don’t even have a menu entry or keybinding. Luckily, aside from using the menu system or keyboard shortcuts, you can run any function directly by pressing M-x
(which calls the execute-extended-command
function) followed by the function name (and the Enter
key). For example, running C-x C-s
is the same as running M-x save-buffer
, similarly, C-x C-f
runs the find-file
function.
This is a very powerful concept. For example, for modes that you don’t use very often it is not uncommon that you forget the exact keybindings it offers. On the other hand, you may remember the name of the function, or part of it. Then, depending on the completion framework you are using (a topic for a future tip), typing M-x
followed by (part of) the function name will allow to find and run the function anyway.
Emacs goes pretty far in this function paradigm. Even regular keypresses are functions. For example, typing the i
key actually runs the self-insert-command
function, which then inserts that letter into the buffer. This is one of the reasons why Emacs is so flexible. It allows you to change the function bound to a given keybinding, or to redefine or “advise” a function to modify its behaviour. For example, in an Org mode (also something for a series of future tips) file, pressing a regular ket doesn’t run the self-insert-command
, instead t runs org-self-insert-command
, which adds extra functionality to pressing the regular keys. From the help information of this function:
Like ‘self-insert-command’, use ‘overwrite-mode’ for whitespace in tables. If the cursor is in a table looking at whitespace, the whitespace is overwritten, and the table is not marked as requiring realignment.
So if you insert a character in an Org mode table, it is as if you have the “Insert” key enabled so that the table alignment is not modified by the text you inserted (as long as your are typing “over” whitespace).
Similarly, pressing Enter
(a.k.a. the return
key) doesn’t just insert a newline character. By default, the key is bound to the newline-and-indent
function, which, according to its help text, does the following:
Insert a newline, then indent according to major mode. Indentation is done using the value of ‘indent-line-function’. In programming language modes, this is the same as TAB. In some text modes, where TAB inserts a tab, this command indents to the column specified by the function ‘current-left-margin’.
One final thing to remember is that keybindings can be changed (by you or by package authors, for example). So if you would like to bind a given key combination to a function you use often: go ahead. As long as you remember the original function’s name you can always call it by its name, even if it no longer has a keybinding associated with it.
No Comments