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 ...
17 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
  • 75.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
  • 49k
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.1k
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.2k
13 votes

How to delete a function?

Functions are unbound with: (fmakunbound 'NAME) (The equivalent for variables is makunbound.)
phils's user avatar
  • 49k
11 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
  • 49k
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.1k
10 votes
Accepted

What is identity doing?

The first example becomes a little clearer if you look further down the function. Just a bit past what you quoted we have (mapcar (lambda (x) (funcall display-fn x)) choices) So a list of "choices"...
erikstokes's user avatar
  • 12.7k
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.2k
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
  • 21.8k
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.2k
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.6k
8 votes

What are the situations where defining a function as non-interactive is better than interactive?

While it is entirely subjective, I would suggest only making functions interactive if you actually expect them to be called that way. Many functions are intended to be used as building blocks rather ...
glucas's user avatar
  • 20.3k
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
  • 32.7k
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
  • 18.9k
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
  • 32.7k
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,661
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

How to pass a parameter together with function

Not out of the box. However, we can use lambda: (define-key my-map my-key (lambda () (interactive) (jump... ...))) You can even create a macro: (defmacro defkey-arg (map key func &rest args) `...
sds's user avatar
  • 5,958
7 votes
Accepted

Are keybindings mode-dependent? How can I customize this?

What you want is to change what H-x does when in python-mode so that instead of running eval-region (which only works on elisp) it calls python-shell-send-region. You can use define-key to do this: (...
erikstokes's user avatar
  • 12.7k
7 votes

What are the situations where defining a function as non-interactive is better than interactive?

Others have mentioned some reasons for not making a given function into a command. Here is another: user discovery and access to commands. If you ever manipulate or access commands interactively by ...
Drew's user avatar
  • 75.9k
7 votes

Override a function locally, but allow calls to the original function

Store the original function (obtained with symbol-function) in a local variable, and use funcall to call the function object stored in that variable. Cumbersome, but it mostly works. (defadvice TeX-...
Gilles 'SO- stop being evil''s user avatar
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
  • 49k
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
  • 49k
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
  • 5,958
7 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
  • 983
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
  • 21.8k

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