Skip to main content
Share Your Experience: Take the 2024 Developer Survey
27 votes

Why can't I bind my function to a key or call it with M-x?

The core point is that there is a difference between a function and a command. In Emacs lisp, functions are not interactively callable by default. That means you can't access them via M-x or bind ...
19 votes
Accepted

When should sharp quotes be used?

#' is just shorthand for function, just as ' is shorthand for quote. You can use it anywhere where you want to indicate to the byte-compiler or the interpreter or a human reader that its argument is ...
Drew's user avatar
  • 77.9k
15 votes
Accepted

Is everything in Emacs made of functions?

Not everything is a function, no. For starters, Elisp (the language) has many other kinds of object besides functions. However, I think you're specifically asking whether every interactive action ...
phils's user avatar
  • 51.5k
13 votes
Accepted

Insert line above/below

The special form which saves and restores the current point and buffer is save-excursion. So you could write your functions as: (defun insert-line-below () "Insert an empty line below the current ...
Basil's user avatar
  • 12.4k
13 votes

When should sharp quotes be used?

#' (aka function) can be used in front of (lambda ...) but it's redundant there, so the only place where it's really meaningful is in front of a symbol, as in #'car. In ELisp, #'car and 'car are ...
Stefan's user avatar
  • 26.5k
13 votes

How to delete a function?

Functions are unbound with: (fmakunbound 'NAME) (The equivalent for variables is makunbound.)
phils's user avatar
  • 51.5k
12 votes
Accepted

How do I invoke a non-interactive lisp function interactively?

You're looking for M-: which runs the command eval-expression
phils's user avatar
  • 51.5k
11 votes
Accepted

Define a function alias within a let binding?

Definitions This topic confuses me sometimes, so let's start with some definitions, namely those of let: let is a special form in ‘C source code’. (let VARLIST BODY...) Bind variables according to ...
Basil's user avatar
  • 12.4k
10 votes
Accepted

How to invoke functions that are quoted in let?

There seems to be a fair bit of confusion how quoting works. In Lisp, symbols and lists fulfill a dual meaning, depending on whether they're quoted or not: Unquoted symbol: Evaluate the symbol (by ...
wasamasa's user avatar
  • 22.2k
10 votes
Accepted

How to make interactive functions (simply) undoable?

I recommend you do C-h f replace-string RET and read it: [...] This function is for interactive use only; in Lisp code use `search-forward' and `replace-match' instead. [...] So, as suggested ...
Stefan's user avatar
  • 26.5k
10 votes
Accepted

What does `(fset SYMBOL KEYMAP)` do?

fset sets a symbol's function definition. Here, projectile-command-map is used as a prefix command. A prefix command is a symbol whose function definition is a keymap. The definition of a prefix ...
Tianxiang Xiong's user avatar
10 votes
Accepted

Are local nested functions possible in elisp?

You can also do it without resorting to cl-lib as follows: (defun my-fn (x y) (let ((my-local-fn (lambda (a b) (message "%S" (- a b))))) (funcall my-local-fn x y) (funcall my-local-fn y x))...
Stefan's user avatar
  • 26.5k
9 votes

Org-mode link to function definition

For elisp that is certainly possible. The general strategy might be something like: [[elisp:(find-function 'describe-function)]] You could add a description if you wanted to also, e.g. [[elisp:(...
John Kitchin's user avatar
  • 11.9k
8 votes

How to repeat a command immediately after calling it?

You are looking for repeat, bound by default to C-x z. The first part of the docstring: (repeat REPEAT-ARG) Repeat most recently executed command. If REPEAT-ARG is non-nil (interactively, ...
Dan's user avatar
  • 33.1k
8 votes

How to save all modified buffers and kill emacs

From the docstring of C-x C-c: With prefix ARG, silently save all file-visiting buffers, then kill. So you want to use C-u C-x C-c EDIT: If you also want that the above silently kills all ...
andrej's user avatar
  • 993
8 votes

How to bind keys to a function which can be defined per-mode?

(defvar my-local-variable nil "Variable used on a bufer-local basis to indicate ...") (make-variable-buffer-local 'my-local-variable) USAGE: (setq my-local-variable (lambda () (message "major-...
lawlist's user avatar
  • 19.2k
8 votes

Are local nested functions possible in elisp?

You can use cl-flet or cl-labels from cl-lib. The docstrings: (cl-flet ((FUNC ARGLIST BODY...) ...) FORM...) Probably introduced at or before Emacs version 24.3. Make local function definitions. ...
Dan's user avatar
  • 33.1k
8 votes
Accepted

How to replace focus-out-hook with after-focus-change-function in emacs 27?

You could try using something like: (add-function :after after-focus-change-function #'your-function-here) So, in your case, something like this should do what you are after: (add-function :after ...
Manuel Uberti's user avatar
8 votes
Accepted

Why do people add their initials to the names of tailor-made functions added to their config file?

It's a workaround for a misfeature of elisp: no namespaces. Its main purpose is to prevent name collisions for code written by different users/provided by different packages. It secondarily helps ...
Phil Hudson's user avatar
  • 1,741
8 votes

When is the first element in the argument list regarded as a function symbol and when not?

the first element of a list is interpreted as a function symbol That's the most common case, but it isn't an absolute rule. First, you need to know that every symbol can have both a function meaning (...
Gilles 'SO- stop being evil''s user avatar
7 votes
Accepted

Symbol's function definition is void: insert-string

See this issue. This problem should be fixed in the next release. Quick fix: Change the function insert-string to insert in ~/.emacs.d/core/core-spacemacs-buffer.el, and everything works well.
nalzok's user avatar
  • 675
7 votes

Keybinding for buffer word count

Your binding does successfully run the keyboard macro you've specified. The problem is that the resulting message does not remain in the echo area long enough for you to see it, which is a side-...
phils's user avatar
  • 51.5k
7 votes
Accepted

Hook with quoted lambda as an argument

(add-hook 'coffee-mode-hook '(lambda () (coffee-custom))) Definitely not recommended. Quoting lambdas like that is not good practice. As you say, (add-hook 'coffee-mode-hook #'coffee-custom) would ...
phils's user avatar
  • 51.5k
7 votes
Accepted

How to delete a function?

In addition to removing the function definition with fmakunbound and variable binding with makunbound, you can also remove the name itself with unintern. Note also that, unless you save your ...
sds's user avatar
  • 6,124
7 votes
Accepted

Symbol’s function definition is void

You define a local variable, then later call a non-existent function with the same name in the if: (if (helm-in-frame-p) Drop the surrounding parentheses and it should work again.
wasamasa's user avatar
  • 22.2k
7 votes
Accepted

Why can't I compose commands `mark-whole-buffer` and `copy-region-as-kill` in Elisp code?

What's wrong with your attempt (copy-region-as-kill (mark-whole-buffer)) This calls the function copy-region-as-kill with one argument, which is the value returned by mark-whole-buffer. All Emacs ...
Gilles 'SO- stop being evil''s user avatar
7 votes

Call function without calling advice defined on it

I've thought about this before, and there's a less-than-perfect solution: you need to store the original definition before any advice-adding. (defun f () (insert "233\n")) (setq f.backup ...
shynur's user avatar
  • 5,318
6 votes
Accepted

Bringing a function into a lexical environment

You're trying here to access the lexical variable x from outside its lexical scope, which obviously can't work. What you can do is pass a lexical environment to eval: LEXICAL can also be an actual ...
npostavs's user avatar
  • 9,203
6 votes
Accepted

Trace calls to a function with the parameters

The easiest way to find out the arguments passed to a function is to use M-x trace-function RET name-of-your-function RET. Once you're done, use M-x untrace-function or M-x untrace-all. Note that ...
YoungFrog's user avatar
  • 3,526

Only top scored, non community-wiki answers of a minimum length are eligible