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 can be invoked via M-x name-of-command RET, and they can also be bound to a key sequence. Regular functions do not include a call to interactive, can not be ...
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 them to a key or mouse click. If you want to do that, you need to explicitly declare the function to be interactive, which you do by adding an (interactive) form ...
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 also shows the commands invoked by each key sequence. In earlier releases you'll just see the raw keystrokes.
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 emacs:
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 would in a terminal. alternatively, you can use M-&, async-shell-command.
The output from the shell command will be placed in a temporary buffer. If you ...
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 (commandp 'counsel-M-x)
(global-set-key [remap execute-extended-command] #'counsel-M-x))
Alternatively (and more simply), you can just enable counsel-mode and ...
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 buffer instead of just echoing it in the messages buffer. So, the full command for the df example is
C-u M-! df RET
The command C-u M-| that Emacs User is ...
The interactive special form provides the easiest way to get input from a user.
(defun td (variable)
(insert (format "std::cout << \"%s is: \" << %s << std::endl;" variable variable)))
Here "sVariable:" consists of the "s" code character (read a string) and the prompt. (See Using interactive in the Emacs ...
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, START and END specify the beginning and end of the text to
act on, in place of the region.
To indent by four columns, just pass the prefix argument with C-u:
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 characters by using open-dribble-file:
view-lossage is an interactive compiled Lisp function in help.el.
It is bound to C-h l, <f1> l, <help> l.
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>") #'shell)
(defun my-shell-mode-hook ()
"My preferred settings for `shell-mode'."
(local-set-key (kbd "<f1>") #'mode-line-other-...
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 --insert ~/foo/file1.org -f htmlize-my-org --kill
As a more complete example, here's a way to run M-x delete-trailing-whitespace on all files in a dir:
$ for ...
You are looking for repeat, bound by default to C-x z. The
first part of the docstring:
Repeat most recently executed command.
If REPEAT-ARG is non-nil (interactively, with a prefix argument),
supply a prefix argument to that command. Otherwise, give the
command the same prefix argument it was given before, if any.
If you ...
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).
You could create buffer in nonexist path with C-x C-f non/exist/path/file.name RET and then create nececcary folders with M-x make-directory RET RET.
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 ((default-directory "/home/"))
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 stick to your workarounds. I am not aware of any existing solution.
As of August 2021 this feature still does not exist.
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 "some-node").
Note that the info reader on the terminal picks man pages when info nodes cannot be found. There is no "glibc" info node, but just a man page. To ...
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.masteringemacs.org/articles/2011/04/12/re-builder-interactive-regexp-builder/ for more discussion.
helm-apropos is indeed pretty slow. I think it comes from joining 5
very large lists:
C-h f this-command-keys:
this-command-keys is a built-in function in `C source code'.
For more information check the manuals.
Return the key sequence that invoked this command.
However, if the command has called read-key-sequence, it returns
the last key sequence that has been read.
The value is a string or a ...
I wrote about this once.
(defmacro ivy-quit-and-run (&rest body)
"Quit the minibuffer and run BODY afterwards."
(put 'quit 'error-message "")
(run-at-time nil nil
(put 'quit 'error-message "Quit")
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."
(let* ((buffer (get-buffer buffername))
(filename (buffer-file-name buffer)))
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 need a bit more lisp.
And if you do this often, you might even ...
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-indent).
In this case, it turns out that C-e C-j is a sequence of two commands rather than a single thing. (The same with C-a C-o TAB.) To bind this to a single ...