53
votes
Accepted
What is the difference between a function and a command?
Every command is a function, but not every function is also a command.1
A command includes a call to interactive; this is why commands are commonly referred to as "interactive functions". Commands ...
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 ...
Community wiki
19
votes
Accepted
How to find out what command I just accidentally ran with a keyboard shortcut?
When some key sequence triggers an unexpected command, use view-lossage (bound to C-h l by default) to see what keystrokes Emacs has recently received. This is most useful since Emacs 25 as it now ...
16
votes
Accepted
How can I test and use a Perl regular expression interactively?
There are two nice packages for working with perl kind of regexes effectively, which should be used together - visual-regexp and it's extension - visual-regexp-steroids. They are great addition to ...
14
votes
Accepted
How to use 'interactive' arguments when calling the command from code
Simply:
(call-interactively 'test)
14
votes
Run shell commands in emacs by something similar to M-x ...?
M-! runs the command shell-command. This will prompt you for the actual command to run in the minibuffer, which then runs. You can run it in the background by putting a & at the end, just as you ...
11
votes
Accepted
Getting the key binding for the current command
I think you're looking for this-single-command-keys.
11
votes
Get output of external command in the buffer
Actually the shortest way of doing it is to use shell-command with a prefix argument. This is mapped to C-u M-!. The C-u prefix argument changes the standard M-! to insert the output in the current ...
11
votes
Accepted
Display corresponding key binding of command during M-x completion
The command counsel-M-x does this for you:
(ivy) Global key bindings recommends you remap M-x (execute-extended-command) to counsel-M-x in order to avail of its increased Ivy-ness, e.g. via
(when (...
10
votes
Accepted
How to change the default transient level temporarily? ("Show hidden magit commands")
I [as the author of this package] did not consider this feature but it sounds potentially useful and doable but it doesn't exist yet. I've added this idea to my TODO list but for now you'll have to ...
9
votes
Define an emacs function to take arguments in the same way as something like M-x goto-line
The interactive special form provides the easiest way to get input from a user.
(defun td (variable)
(interactive "sVariable:")
(insert (format "std::cout << \"%s is: \" << %s <<...
9
votes
Accepted
Repeating a command n times
The documentation for indent-rigidly says:
If called from a program, or interactively with prefix ARG, indent all
lines starting in the region forward by ARG columns. If called from a
program, ...
9
votes
How to find out what command I just accidentally ran with a keyboard shortcut?
As @glucas has mentioned, view-lossage helps. Unfortunately, it displays relatively few events, and users have no control over the number.
As C-h k C-h l tells you, you can record all keyboard ...
9
votes
Accepted
Keymap that is dependent on the buffer
You set shell-command as global key-binding for F1
and you add a hook function to shell-mode-hook that sets the local key binding to mode-line-other-buffer.
(global-set-key (kbd "<f1>")...
8
votes
Can emacs commands be called from a shell script?
You can run emacs --batch f htmlize-my-org --kill where htmlize-my-org is a function you've written that runs htmlize on those files.
You can also pass files on the command line, e.g. emacs --batch -...
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♦
- 31.1k
8
votes
Accepted
How to quickly create a new directory without using dired-mode?
You could use M-x make-directory DIRNAME RET. The default path is the path to the current buffer folder.
You could bind it to key (like C-x C-f) with (global-set-key (kbd "C-c d") 'make-directory).
...
8
votes
Accepted
Move to the beginning of a heading smartly in Org-mode
This is supported right out-of-the-box, by setting:
(setq org-special-ctrl-a/e t)
As a bonus C-e handles tags similarly to C-a handling asterisks.
8
votes
Accepted
Call function interactively and set initial content
find-file uses the buffer-local default-directory value as the default filename (see also find-file-read-args), so all you need to do is bind that value for the scope of the call to find-file:
(let ((...
7
votes
Reading info files without knowing their path
The info reader in Emacs can be accessed with C-h i. There you can jump directly into the some-node node with g (some-node) RET.
Alternatively, you can go to the some-node node by eval'ing (info "...
7
votes
How can I test and use a Perl regular expression interactively?
re-builder allows on the fly construction of emacs lisp style regular expressions. I don't know if there is an equivalent package for building Perl regular expressions interactively.
See http://www....
7
votes
Accepted
How can I speed up individual commands?
helm-apropos is indeed pretty slow. I think it comes from joining 5
very large lists:
helm-apropos-function-list
;; =>
;; (helm-def-source--emacs-commands
;; helm-def-source--emacs-functions
;; ...
7
votes
Can a command determine the event sequence that invoked it?
C-h f this-command-keys:
this-command-keys is a built-in function in `C source code'.
(this-command-keys)
For more information check the manuals.
Return the key sequence that invoked ...
7
votes
How to see which functions are executed
view-lossage will show the last few hundred keystrokes. There's also command-log-mode which records the keystrokes and the command it executed.
7
votes
Exit minibuffer and execute a command afterwards?
I wrote about this once.
The code:
(defmacro ivy-quit-and-run (&rest body)
"Quit the minibuffer and run BODY afterwards."
`(progn
(put 'quit 'error-message "")
(run-at-time nil nil
...
7
votes
Accepted
How to make emacs automatically kill buffer after invoking delete-file command?
I wrote this method, which differs slightly from the one in the other answer:
(defun delete-file-visited-by-buffer (buffername)
"Delete the file visited by the buffer named BUFFERNAME."
(...
7
votes
Is there Sublime Text - style command palette in Emacs?
I use helm-M-x. It's part of the package helm.
You can view a command's description with helm-execute-persistent-action.
6
votes
Accepted
How can I customize the compile command?
You can customize the variable compile-command to change the compilation command that is used by default:
(setq compile-command "make package install")
6
votes
Accepted
how to give optional arguments to commands
Remember that "commands" are just functions, and can be called as such. You can do this:
M-: (shell-command "ls" <arg1here> <arg2here>) RET
If you want to create new buffers, you'll ...
6
votes
Accepted
Bind C-e C-j and C-a C-o TAB to single key, aka insert and jump to newline
You can always check what function some keys are bound to using C-h k. For example, if we type C-h k C-e, we see that it is bound to (move-end-of-line ARG); similarly, C-j is bound to (newline-and-...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
commands × 152key-bindings × 35
interactive × 21
buffers × 8
functions × 8
help × 8
region × 7
evil × 5
keymap × 5
minibuffer × 5
motion × 5
history × 5
spacemacs × 4
helm × 4
completion × 4
org-mode × 3
regular-expressions × 3
shell × 3
eshell × 3
ivy × 3
keyboard-macros × 3
prefix-argument × 3
m-x × 3
python × 2
indentation × 2