44

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


20

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


15

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:


13

Simply: (call-interactively 'test)


13

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


12

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.


11

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


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

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


8

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


7

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


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

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

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

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


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

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.


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

view-lossage will show the last few hundred keystrokes. There's also command-log-mode which records the keystrokes and the command it executed.


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)


5

Like this (defun example(name) (interactive "sWhat's your name: ") (message "Hello, %s" name)) The key part is the "s" prefix. Use s for plain strings, b for buffers, etc.


5

I use C-M-s to search for a regexp. Then I use M-x replace-regexp. Can I save typing the regexp for the second time? Yes, instead of exiting the incremental search, just hit M-% (which, by the way, is the global binding for query-replace). This will use the search string you've just typed and only prompt you for a replacement. This will work both on ...


5

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


5

This project is also a good alternative: https://github.com/michael-heerdegen/interaction-log.el


5

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 or vector format). The easiest way to produce the latter is to use the kbd function, so that you can write the keystrokes using the familiar syntax Emacs uses ...


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