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 ...
23 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 ...
glucas's user avatar
  • 20.6k
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 ...
Tyler's user avatar
  • 22.4k
13 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 ...
dfeich's user avatar
  • 1,904
12 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 ...
Drew's user avatar
  • 77.9k
12 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 (...
Basil's user avatar
  • 12.5k
11 votes
Accepted

How to change the default transient level temporarily? ("Show hidden magit commands")

Update: I have now implemented this. The key binding is C-x a 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 ...
tarsius's user avatar
  • 25.8k
9 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.
user673592's user avatar
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>")...
Tobias's user avatar
  • 33.3k
8 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 ...
abo-abo's user avatar
  • 14.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, ...
Dan's user avatar
  • 33.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). ...
Konstantin Morenko's user avatar
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 ((...
phils's user avatar
  • 51.6k
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
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." (...
zck's user avatar
  • 9,102
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.
bertfred's user avatar
  • 1,729
7 votes
Accepted

How to see the name of the command every time I run it by a shortcut?

You can use post-command-hook. (defun my-echo-command-name-hook () (unless (or (eq this-command 'self-insert-command) (eq this-command 'next-line)) (message "%s" this-command))) (...
bertfred's user avatar
  • 1,729
7 votes

lazy find/search for commands within emacs

One way you can try to find such commands is to use C-h a. In vanilla Emacs this is bound to apropos-command. For C-h a you can input words to match in the command documentation - e.g., org and ...
Drew's user avatar
  • 77.9k
6 votes
Accepted

How to disable query to turn on disabled command?

Put this in your init file: (setq disabled-command-function 'ignore) By default, disabling a command means that you get that prompt when you try to use it. But you can override this design by ...
Drew's user avatar
  • 77.9k
6 votes

How can I set a key binding to run two other key bindings?

For this special case (save a *TeX buffer before running the compiler), you don't need to define a binding, or advice a function, but just set TeX-save-query to nil: (setq TeX-save-query nil)
giordano's user avatar
  • 3,255
6 votes
Accepted

Open File and Directory: What's the Emacs command?

Emacs is designed to be self-documenting. A comment has already shown you how to use C-h k to get help for a keyboard shortcut (or a menu shortcut in this case). There are other quite a lot of other ...
db48x's user avatar
  • 18.3k
6 votes

How can I pass the NO-LINE-BREAK argument to base64-encode-region in M-x?

If you copy the following lisp code to your init file and restart emacs you can call base64-encode-region with universal prefix argument C-u to get the base64 encoded string without linebreaks: ...
Tobias's user avatar
  • 33.3k
5 votes

How to disable query to turn on disabled command?

Drew's answer solves your stated question so I would suggest accepting that; but my best guess is that something like this is what you're actually after: (global-set-key [remap mouse-save-then-kill] '...
phils's user avatar
  • 51.6k
5 votes
Accepted

Exit minibuffer and execute a command afterwards?

I don't know of a way to do what you request. As you say in your question, and as @abo-abo shows, a workaround is to use a timer to execute the code you want run after the minibuffer is exited. The ...
Drew's user avatar
  • 77.9k
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

How can I set a key binding to run two other key bindings?

The general way to link multiple key-bindings to a single binding is a Keyboard Macro ((emacs) Keyboard Macros). Keyboard macros can either be 'recorded' interactively, or written in elisp (in string ...
Tyler's user avatar
  • 22.4k
5 votes
Accepted

Refile and keep the original

No big problem, it's documented in the info page "(org) Refile and copy" C-c M-w (org-copy) Copying works like refiling, except that the original note is not deleted.
p_wiersig's user avatar
  • 1,081
5 votes
Accepted

What keystrokes brings up the prompt to issue Lisp commands?

You are looking for eval-expression, bound by default to M-:. M-: is different from M-x, which is bound to execute-extended-command. The latter is used specifically for commands (interactive ...
Dan's user avatar
  • 33.1k
5 votes
Accepted

How to specify a command that should be called by #'repeat?

If you look at the source code for repeat (C-h k C-x z and then click on the repeat.el link in the *Help* buffer), you will see that the command to be repeated is stored in the variable last-...
sds's user avatar
  • 6,124
5 votes
Accepted

What function does C-x invoke?

In the default configuration, it is not a complete key sequence: it is a prefix key. It is an incomplete specification: it has to be followed by another key (or more than one, if the next key is also ...
NickD's user avatar
  • 30.6k

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