As of May 31, 2023, we have updated our Code of Conduct.

Hot answers tagged

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 ...
glucas's user avatar
  • 20.1k
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 ...
kdb's user avatar
  • 1,541
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 ...
jch's user avatar
  • 5,680
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,...
legoscia's user avatar
  • 6,002
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.) ...
nanny's user avatar
  • 5,644
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
legoscia's user avatar
  • 6,002
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 ...
camdez's user avatar
  • 386
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 ...
Clément's user avatar
  • 3,914
20 votes

Swap two variables in Elisp

This is the elegant idiom I use ;-). (setq a (prog1 b (setq b a)))
Drew's user avatar
  • 74.7k
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 ...
zck's user avatar
  • 8,909
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 ...
Kaushal Modi's user avatar
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.
Mark Karpov's user avatar
  • 4,893
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 ...
ben's user avatar
  • 181
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 ...
Drew's user avatar
  • 74.7k
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: ...
Drew's user avatar
  • 74.7k
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-...
Sacha Chua's user avatar
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 ...
Stefan's user avatar
  • 26k
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 (...
wasamasa's user avatar
  • 21.7k
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 ...
Jack Rusher's user avatar
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's user avatar
  • 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 ...
François Févotte's user avatar
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 ...
wvxvw's user avatar
  • 11.2k
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 ...
Drew's user avatar
  • 74.7k
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 ...
Stefan's user avatar
  • 26k
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: ...
Radon Rosborough's user avatar
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) (...
Drew's user avatar
  • 74.7k
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's user avatar
  • 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-...
wasamasa's user avatar
  • 21.7k

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