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 ...
16 votes
Accepted

call interactive function from elisp code without worrying about arguments

If you want to call an interactive function from within elisp, but call it as if you invoked it interactively (e.g., via M-x some-command), you can wrap it in the call-interactively function. The ...
Dan's user avatar
  • 33.1k
15 votes
Accepted

How to apply `call-interactively` to an interactive command that accepts the universal argument?

According to shell's interactive form, as long as current-prefix-arg is non-nil, shell will ask user a buffer to use, so you can set current-prefix-arg to non-nil (4 is used in following as an example)...
xuchunyang's user avatar
  • 14.6k
14 votes
Accepted

Insert character by its Unicode name

From the documentation of insert-char, I cannot see why (insert-char "GREEK SMALL LETTER EPSILON") doesn't work. It doesn't work because insert-char understands Unicode character names only ...
Basil's user avatar
  • 12.5k
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 write a function with an interactive choice of the value of the argument?

(defun foo (choice) "..." (interactive (list (completing-read "Choose: " '(("1" . "Cow") ("2" . "Rabbit") ("3" . "Dog")) nil t))) (message "You chose `%s'" choice) ...
Drew's user avatar
  • 77.9k
8 votes
Accepted

Break key as a shortcut -- what am I doing wrong?

You ask several related questions, but this is the main one, I think: How does one find out what name emacs gives to a key such as the one labeled pause/break on my keyboard? For Emacs's description ...
Drew's user avatar
  • 77.9k
8 votes

Default argument for interactive function?

(defun hello (st) (interactive (list (read-string "Your name: " "toto"))) (message "Hello Mr %s" st)) where "toto" is the initial-input.
gigiair's user avatar
  • 2,186
6 votes

Can Emacs use tramp to run in an interactive session on a remote HPC node?

I have been struggling to do the same. I found a different route that allowed me to do basically the same thing. You can open a shell buffer by M-x shell and from there connect to the login node, and ...
Joonha Park's user avatar
6 votes
Accepted

How to write a function with an interactive choice of the value of the argument?

The function you are looking for is read-char-choice. I cite the corresponding documentation: (read-char-choice PROMPT CHARS &optional INHIBIT-KEYBOARD-QUIT) Read and return one of CHARS, ...
Tobias's user avatar
  • 33.3k
5 votes
Accepted

How to repeat a command immediately after calling it?

There is also repeat-complex-command (normally bound to C-x ESC ESC as well as several others (that's the one I use, so I remember that)). This will go back further in the history to the last command ...
MAP's user avatar
  • 592
5 votes
Accepted

Detect interactive use of `kill-buffer` in `kill-buffer-hook`

Test the variable this-command to see whether it is kill-buffer
phils's user avatar
  • 51.6k
5 votes

How can I read a single character from the minibuffer?

The question was answered long ago, but this additional answer may provide some assistance to other searchers. read-char-choice allows you to specify a list of choices. The fn will not return until ...
Cheeso's user avatar
  • 245
5 votes
Accepted

Combine two interactive functions

[I believe you meant sort-lines, not sort - that's the only way the question makes sense, so I am going to assume it.] Both sort-lines and delete-duplicate-lines operate on the selected region. You ...
NickD's user avatar
  • 30.6k
4 votes
Accepted

how to find non-interactive function equivalents of elisp interactive functions

Most commands are absolutely fine for both interactive and non-interactive use, so a default position of "if it doesn't say otherwise, it's ok for non-interactive use" is going to be fine in the ...
phils's user avatar
  • 51.6k
4 votes
Accepted

Read input char without pressing RET

You apparently want to read a character and immediately dispatch behavior based on what it is. Help > Search Documentation > Find Any Object by Name (command apropos) tells you, for read plus char ...
Drew's user avatar
  • 77.9k
4 votes

How to reuse last input in command with a prompt?

Commands that read input from the minibuffer use functions such as completing-read, read-string, read-regexp, etc. The most general of these is read-from-minibuffer. All such functions take a HISTORY ...
Drew's user avatar
  • 77.9k
4 votes

Near-identical commands on multiple keys

You can bind one command to several key-sequences and access the calling key-sequence within the command with this-command-keys. That is demonstrated with the following code. There the same command my-...
Tobias's user avatar
  • 33.3k
4 votes

Near-identical commands on multiple keys

Let's look at this logically: you want to have near-identical commands bound to C-f2 and C-f3. The only difference between these commands is whether they store the thing under point in the f2 memory ...
Gilles 'SO- stop being evil''s user avatar
4 votes
Accepted

how to provide a parameter for interactive lamda function

The (lambda () (interactive) ...) syntax is just fine. The problem you are encountering is with your invocation of helm-swoop. The arguments for that function must be keyed (preceded by their ...
ebpa's user avatar
  • 7,499
4 votes
Accepted

(interactive "r") can't be used directly after Emacs startup

The use-region-p test should be within the interactive spec. "r" is for functions that always use the region. You probably want to use something like: (defun try-region (&optional begin end) (...
Stefan's user avatar
  • 26.5k
3 votes
Accepted

How to reuse last input in command with a prompt?

You could use read-string and give they default value def-say-word and save in later. (setq def-say-word "default value") (defun say-word (word) (interactive (list (read-string (format "Say ...
Konstantin Morenko's user avatar
3 votes
Accepted

Helm find-file foreground face for interactive current directory in minibuffer

M-x list-faces-display is your friend. It shows you all faces currently defined. And you can click on a face name to open Customize for that face. If you use library Face-Menu+ (code: facemenu+.el) ...
Drew's user avatar
  • 77.9k
3 votes
Accepted

"Invalid use of '\' in replacement text" with non-interactive calls

(Credits go to Tobias) replace-regexp (and query-replace-regexp) have their second read argument malaxed into a suitable form for the elisp call. In particular, in case the replacement string ...
T. Verron's user avatar
  • 4,293
3 votes
Accepted

eval-last-sexp produces unnecessary void-variable error when using a macro containing eval

After looking through the GNU Emacs Lisp Reference Manual, I realized that there is a subsection of "Common Problems Using Macros" called "Evaluating Macro Arguments in Expansion" which explains this ...
Jonathan Ganc's user avatar
3 votes

Problem with giving inputs to a function programmatically

The function sunrise-sunset is an interactive function takes a numeric prefix argument as a parameter. It does not take the latitude and longitude as parameters, which is what you're trying to pass ...
Aquaactress's user avatar
  • 1,453
3 votes
Accepted

Use write-region in read-only buffer

The interactive code * is used to: "Signal an error if the current buffer is read-only. Special." https://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes.html Thus, the line ...
lawlist's user avatar
  • 19.2k
3 votes
Accepted

Assign buffer-name to variable

Use cond to bind either the buffer provided or the current buffer to my-source-buffer. No need for the extra line below. As there is actually only one condition an if statement would probably serve ...
Hubisan's user avatar
  • 1,735
3 votes
Accepted

Asterisk in interactive specification

First, this is untrue, assuming I understand what you mean: On the other hand, pretty much any command can be made a no-op by a zero prefix argument Beyond that, the answer, IMO, is that you do what ...
Drew's user avatar
  • 77.9k

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