30 votes
Accepted

Elisp for applying command to only the selected region

(defun some-function (beginning end) (interactive "r") (if (use-region-p) (message "The region is active, and is from %d to %d" beginning end) (message "The region is still there (from % ...
user avatar
  • 69.1k
26 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 ...
13 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 ...
user avatar
  • 11.3k
11 votes
Accepted

ielm, bound to a certain buffer

Hit C-c C-b to select a buffer for IELM to work upon.
user avatar
  • 20.9k
11 votes
Accepted

What is a raw prefix argument? (capital P in interactive)

Hard to believe that people described this here without also giving you links to Emacs's own descriptions of this: In the Emacs manual, node Arguments. In the Elisp manual, node Prefix Command ...
user avatar
  • 69.1k
11 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)...
user avatar
  • 13.5k
11 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 ...
user avatar
  • 31k
10 votes

What is a raw prefix argument? (capital P in interactive)

When raw prefix Interactive Code "P" is used, the argument is passed on as it is whereas "p" converts the arg to a number. After evaluating the below elisp, try out C-u M-x my/fn-with-num-arg and ...
user avatar
  • 24.2k
8 votes
Accepted

How can I read a single character from the minibuffer?

Rather than read-char I recommend read-key. The difference is that read-key obeys all the usual remappings such as input-decode-map and function-key-map, so it will work properly in a tty.
user avatar
  • 25.2k
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 ...
user avatar
  • 19.1k
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, ...
user avatar
  • 31k
7 votes

How can I determine which function was called interactively in the stack?

You can look up the stack using the backtrace frames, here is an implementation getting the name of the current function. Can functions access their name? The code in that question looks up the ...
user avatar
7 votes
Accepted

How can I use an interactive function in a script?

Interactive simply provides a way to call the function via M-x. You can use venv-workon normally, like any other emacs-lisp function. Here's an example: (require 'virtualenvwrapper) (venv-workon "...
user avatar
  • 1,272
7 votes

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

It sounds like you should extend tramp-methods, adding a new method which is similar to the sudo method but uses qsub -I instead. You would then be able to use a multi-hop tramp path to first connect ...
user avatar
  • 11.2k
7 votes
Accepted

buffer-file-name is a function, so why doesn't M-x buffer-file-name work?

buffer-file-name is a function, but not an interactive command. M-x calls execute-extended-command, so can't be used on non-command functions. For more information, see What is the difference ...
user avatar
  • 31k
7 votes

What is a raw prefix argument? (capital P in interactive)

Just to add a bit more detail to @kaushalmodi's answer (and useful test case): The raw argument lets you distinguish between arguments provided with universal-argument and digit-argument. With a ...
user avatar
  • 19.1k
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 ...
user avatar
  • 69.1k
7 votes
Accepted

Elisp code execution order?

Forms in the function body are executed in order, just as you would expect. When you choose "y" if does not "jump" past the first message call. If looks like it did because the first message gets ...
user avatar
  • 8,912
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 ...
user avatar
6 votes
Accepted

Why does multiple cursors use the same char for all cursors with zap-to-char but not with zap-up-to-char?

This is because read-char is advised to have special behaviour. When running in 'multiple-cursors-mode' it caches the character read the first time. If this cache is full then it does not ask for a ...
user avatar
  • 118
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, ...
user avatar
  • 29.7k
6 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 ...
user avatar
  • 69.1k
6 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.
user avatar
  • 1,680
5 votes

Can I make a function non-interactive after it's been defined?

If it's ok, that the function isn't interactive in the first place, something like this might work. (defmacro define-noninteractive-minor-mode (mode doc &optional ...
user avatar
  • 3,266
5 votes
Accepted

Partial input to interactive command

So you want to invoke a command and have a given string automatically inserted at that command's interactive prompt. You can do this using minibuffer-setup-hook, and there is a handy macro which ...
user avatar
  • 43k
5 votes

What does the (interactive ...) form mean?

Given the function (defun foo (arg1 arg2) (interactive (list 'interactive-value1 'interactive-value2) (message "%s %s" arg1 arg2)) You would get (foo 'value1 'value2) => "value1 value2" (...
user avatar
  • 24.2k
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 ...
user avatar
  • 235
5 votes

How can I read a single character from the minibuffer?

In addition to built-in ways to read single events such as read-char and read-char-exclusive, here's an option to read a single character, but also specify which characters are acceptable input: (...
user avatar
  • 31k
5 votes
Accepted

How to initiate a prompt for user input based on the prefix argument?

Here's a more elegant way of doing it: (defun foo (arg) (interactive (list (if (consp current-prefix-arg) (read-number "Number: ") (prefix-numeric-value current-prefix-...
user avatar
  • 20.9k
5 votes

How can I make an interactive function be local to directories?

So as per my earlier comment, Emacs doesn't really support what you're asking for. I would strongly recommend that you simply define a global command in the normal way, and make it behave ...
user avatar
  • 43k

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