49

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 ...


25

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 ...


18

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.


16

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:


14

Simply: (call-interactively 'test)


14

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 ...


11

I think you're looking for this-single-command-keys.


11

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 ...


9

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 << std::endl;" variable variable))) Here "sVariable:" consists of the "s" code character (read a string) and the prompt. (See Using interactive in the Emacs ...


9

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: ...


8

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 ...


8

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, 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 ...


8

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.


8

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 ...


8

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. ...


8

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/")) (call-interactively 'find-file))


7

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.


7

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 ...


7

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 ;; helm-def-source--eieio-classes ;; helm-def-source--eieio-generic ;; helm-def-source--emacs-variables ;; helm-def-source--emacs-faces ;; helm-def-source--...


7

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

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 (lambda () (put 'quit 'error-message "Quit") ,@body)) (minibuffer-keyboard-quit)))


7

I use helm-M-x. It's part of the package helm. You can view a command's description with helm-execute-persistent-action.


7

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 of a key, use C-h k. So C-h k, then hit the Pause key. Emacs probably tells you this: <pause> is undefined So you now know that Emacs describes that key ...


6

Here's one: (require 'cl-lib) (defun sum-numbers-in-region (start end) (interactive "r") (message "%s" (cl-reduce #'+ (split-string (buffer-substring start end)) :key #'string-to-number))) Because it uses string-to-number, it will treat the the ...


6

You can customize the variable compile-command to change the compilation command that is used by default: (setq compile-command "make package install")


6

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. (shell-command "ls" (generate-new-buffer "ls-stdout") (generate-new-buffer "ls-stderr")) And if you do this often, you might even ...


6

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 ...


6

You can use the following emacs lisp code saved in htmlize-script.el: (add-to-list 'load-path "/path/to/htmlize/") (require 'htmlize) (defun htmlize-files-in-dir (dir) (htmlize-many-files (directory-files dir t ".org$"))) And then use the following command from a script: emacs -Q -nw --eval "(progn (load-file \"/path/to/htmlize-script.el\") (htmlize-...


6

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 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 ...


6

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)


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