37
votes
Optional parameter defaults
Unless you use Common Lisp extensions as suggested by @legoscia, you need to check if the optional argument was specified. Note that you don't really need to use let here. This seems more idiomatic to ...
33
votes
Accepted
How do I use nadvice?
All information you need is included in C-h f add-function which
describes the underlying mechanism of advice-add.
The new advice system basically acts like replacing the current
definition of a ...
32
votes
Accepted
What is the difference between ' and #' in front of a symbol?
In Emacs Lisp, if foo is a symbol, then 'foo and #'foo are completely equivalent. The latter form (with #') is preferred when foo is a function, as it documents the fact that it is intended to be ...
29
votes
Optional parameter defaults
You can use cl-defun, which lets you specify a default value for optional arguments:
(cl-defun command (a &optional (b default-b))
(command-body a b))
The default value, in this case default-b,...
26
votes
Accepted
How to get element number in a list?
Here's a function that is included with Emacs 24.3 and later:
(cl-position 2 '(6 7 8 2 3 4)) ;; => 3
(Before Emacs 24.3, use function position from library cl.el, which is included with Emacs.)
...
26
votes
How to check in elisp if a string is a substring of another string?
cl-search can do that (and also returns the index of the substring, if found):
ELISP> (cl-search "f t" "df tj")
1 (#o1, #x1, ?\C-a)
ELISP> (cl-search "ft" "df tj")
nil
25
votes
Accepted
How to properly use defcustom?
The customization system is a built-in feature of Emacs designed to solve precisely the problem you describe—programming may not be the ideal way for the average user to configure their editor.
The ...
22
votes
Accepted
How can I find the path to an executable with Emacs Lisp?
@Sigma's answer is a good start, but it doesn't filter by executability, nor does it allow for extra suffixes. On windows, for example, running a can invoke a.exe, if that's what's in your path.
So ...
20
votes
Swap two variables in Elisp
This is the elegant idiom I use ;-).
(setq a (prog1 b (setq b a)))
18
votes
Accepted
Why do elisp files have end of file comments?
From the Emacs Documentation:
‘;;; filename ends here’
This is the footer line; it appears at the very end of the file. Its purpose is to enable people to detect truncated versions of the
...
18
votes
Accepted
Move selected lines up and down
drag-stuff
Check out the drag-stuff package (also available on Melpa).
You can then select a region and use drag-stuff-up/drag-stuff-down to move that region up/down.
Alternative behavior when ...
18
votes
Accepted
Swap two variables in Elisp
If memory serves me well and you're willing to use cl-lib then:
(cl-rotatef a b)
Note that this is Common Lisp way of solving the problem.
17
votes
How can I evaluate elisp in an orgmode file when it is opened?
This solution requires no change in init.el (with minor modifications). It involves file-local evaluations, though - but that's exactly what the OP asked for. Advantages of the solution are:
asks for ...
17
votes
Accepted
When should sharp quotes be used?
#' is just shorthand for function, just as ' is shorthand for quote.
You can use it anywhere where you want to indicate to the byte-compiler or the interpreter or a human reader that its argument is ...
16
votes
Accepted
Elisp regexps ^ and $ vs ` and '
Your string might have an embedded newline character, in which case \' matches the end of the string but $ matches just before the newline char.
To quote the Elisp manual, node Regexp Special:
...
16
votes
Accepted
Passing a variable to template function in org-capture-templates
You can specify your own properties in the property list for the template, and then you can access those properties with plist-get and org-capture-plist. Here's a brief example:
(defun my/expense-...
16
votes
Accepted
Emacs Lisp comment conventions
Actually, 3-and-more semi-colons stand for headings, where the more semi-colons you put the deeper the nesting of the heading. So it should look like
;;; Main heading
;;;; Sub heading
;;;;; Sub sub ...
15
votes
Meaning of period in (. 123)
It looks like Emacs simply reads (. 123) as 123, what happened?
That's exactly what happened. To back it up with sources:
if (ch == '.')
{
if (!NILP (tail))
XSETCDR (tail, read0 (...
14
votes
Get all regexp matches in buffer as a list
It's probably worth noting that invoking occur with the universal argument causes it to populate the *Occur* buffer with only matches — no file names, line numbers or header information. When combined ...
14
votes
why won't gethash return my key's value?
The default membership test for a hash table is eql. If you'd like to use a string as the key, set it to equal instead:
(setf hash (make-hash-table :test #'equal))
(puthash "a" 1 hash)
(gethash "a" ...

Dan♦
- 32.3k
14
votes
Accepted
Simple request.el usage
This is because '(("address" . address)) is quoted (i.e. prefixed by the ' operator), which means that what is inside does not get evaluated. The request function thus gets called with an argument ...
13
votes
Map a function across a property list?
This would probably depend on a situation. In general, if I need to tie a number of values with a number of names, I'd use a hash-table, but if I have to use a property list, I'd use cl-loop. Below ...
13
votes
Accepted
Naming convention for variable and function names
There is no GNU Emacs convention wrt case for Emacs-Lisp function and variable names. (There can be any number of such "conventions" that anyone could come up with, of course.)
The "Emacs Lisp Style ...
13
votes
When should sharp quotes be used?
#' (aka function) can be used in front of (lambda ...) but it's redundant there, so the only place where it's really meaningful is in front of a symbol, as in #'car. In ELisp, #'car and 'car are ...
12
votes
How to replace an element of an alist?
The OP asks for a solution which handles alists that have string keys. To handle that, see this question. If by chance you only need to handle alists with symbol keys, then as of Emacs 25 you can use:
...
12
votes
Accepted
Replace string in buffer programatically
Use functions re-search-forward and replace-match in a loop, or function perform-replace. See the Elisp manual, node Search and Replace.
Sample code:
(while (re-search-forward "hello" nil t)
(...
12
votes
Accepted
why does cdr return a list of one element?
Here's the docstring for cdr:
(cdr LIST)
Return the cdr of LIST. If arg is nil, return nil.
Error if arg is not nil and not a cons cell. See also `cdr-safe'.
See Info node `(elisp)...

Dan♦
- 32.3k
12
votes
Accepted
Run code right after compilation
Try using compilation-finish-functions:
(defun my-compilation-finish-function (buffer desc)
(message "Buffer %s: %s" buffer desc))
(add-hook 'compilation-finish-functions 'my-compilation-finish-...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
elisp × 418elisp-macros × 25
functions × 23
org-mode × 18
hooks × 15
variables × 13
init-file × 12
regular-expressions × 9
files × 9
lexical-scoping × 9
key-bindings × 8
debugging × 8
string × 8
list × 7
text-properties × 7
quote × 7
symbols × 7
common-lisp × 7
buffers × 6
package × 6
window × 6
major-mode × 6
comment × 6
byte-compilation × 6
advice × 6