Hot answers tagged

104 votes
Accepted

Advantages of setting variables with setq instead of custom.el?

Some people might think it is simpler to use setq. Some people might think it is more lispy. In reality, it is naive in the general case. It is true that for some user options it does not matter. ...
user avatar
  • 69.1k
56 votes
Accepted

How to save a keyboard macro as a Lisp function?

Here are the steps that you can follow: Select the region you want to do the search-replace. Start recording macro. M-x start-kbd-macro Do the required M-x query-replace-regexp (replace "abc" with "...
user avatar
  • 24.2k
52 votes

Can I use org-mode to structure my .emacs or other .el configuration file?

Yes, you certainly can, you can use org-babel-load-file to do this. In your init.el, put the following: (require 'org) (org-babel-load-file (expand-file-name "settings.org" user-...
user avatar
  • 2,549
50 votes

How to replace an element of an alist?

While @Dan's answer is a perfectly fine solution, it is unnecessary. One of the reasons Emacs uses an alist here is that with an alist you can simply add a new element to the front of the list and it ...
user avatar
  • 69.1k
49 votes

How to replace an element of an alist?

Use setf to change the value in place: (setf (cdr (rassoc 'javascript-mode auto-mode-alist)) 'js2-mode) If you want to replace a value in the list, then setf is the generalized machinery you need to ...
user avatar
  • 31k
47 votes

Advantages of setting variables with setq instead of custom.el?

I prefer setq over customize for several reasons: First and foremost, it allows variables to be set programatically (as in, (setq foo (calculate-foo))). I use this power all the time in my ...
user avatar
  • 4,950
36 votes
Accepted

exec-path and $PATH

1) PATH and exec-path Emacs does set exec-path from the value of PATH on startup, but will not look at it again later. But if you run a command, it will inherit PATH, not exec-path, so subprocesses ...
user avatar
36 votes
Accepted

Why do regular expressions created with the regex builder use syntax different from the interactive regular expressions?

There are actually four different re-builder syntax options, and you can switch between them with C-cTAB Two are for the sexp-form regexp compilers rx and sregex (but as the former is more ...
user avatar
  • 43.1k
35 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 ...
user avatar
  • 19.1k
34 votes
Accepted

How to know when or when not to use the single quote before variable names?

Short answer If you are trying to use the variable itself, then use 'some-variable. If you are trying to use the value stored in the variable, use some-variable. boundp uses symbol so it will look ...
user avatar
33 votes
Accepted

Why does elisp not have namespaces?

Why no Namespaces? Because it's complicated, and no one has considered it urgent enough to take the full plunge yet. This has been discussed in the dev list before (more times than one), and there ...
user avatar
  • 22.3k
32 votes
Accepted

How to export top-level headings of org-mode buffer to separate files?

The following command lets you choose a back-end and then exports each top-level subtree to a separate file: (defun org-export-all (backend) "Export all subtrees that are *not* tagged with :...
user avatar
  • 14.2k
32 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 ...
user avatar
  • 1,481
31 votes
Accepted

When to sharp-quote a lambda expression?

Once upon a time, the sharp quote was necessary for lambdas, now that's no longer the case. So, it appears that (lambda (x) x) and #'(lambda (x) x) are equivalent, but '(lambda (x) x) is not (most ...
user avatar
  • 22.3k
30 votes
Accepted

Get content of a buffer

A lot of things in Emacs operate on the current buffer. You need to change the current buffer and restore it when you're done. Use with-current-buffer for simple cases where you just need to do ...
user avatar
30 votes
Accepted

What is the point of quote with single argument and comma? (quote ,arg)

The comma is used in the context of a backquoted (a.k.a. quasiquoted) list, which allows you to evaluate some portions of the list selectively. See also this thread for an example of a use for ...
user avatar
  • 31k
29 votes
Accepted

When should I use autoload instead of require?

autoload is not a substitute for require. Typically require is used to make sure that a certain file is loaded. autoload on the other hand gives Emacs a hint in which file to find a given function ...
user avatar
  • 1,886
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,...
user avatar
  • 5,786
28 votes
Accepted

What's the difference between push and add-to-list?

What #zck mentions is one difference. But if that were the only difference then you could ask about cl-pushnew and add-to-list. Another important difference: add-to-list is a function, which means ...
user avatar
  • 69.1k
27 votes

How to save a keyboard macro as a Lisp function?

elmacro was already mentioned by @lunaryorn, but as the author of this package I thought it'd be nice to provide an example. If you record a macro like this: F3 C-e M-b M-u C-a C-n F4 Then doing M-...
user avatar
  • 781
27 votes
Accepted

How can I simulate an arbitary key event from Elisp?

You can feed arbitrary events (keystrokes, mouse clicks, etc.) to the command loop by putting them onto unread-command-events. For example, the following will cause the command loop to execute a ...
user avatar
  • 5,480
27 votes
Accepted

Reset custom variable to default value programmatically

C-hig (elisp) Variable Definitions Internally, defcustom uses the symbol property standard-value to record the expression for the standard value, saved-value to record the value saved by the user ...
user avatar
  • 43.1k
27 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 ...
user avatar
  • 5,480
26 votes

Advantages of setting variables with setq instead of custom.el?

One advantage of using setq instead of customize is readability. One is free to annotate each customization to one's liking which IMO improves readability. One can also group related customizations ...
user avatar
  • 3,816
26 votes
Accepted

How to know my buffer's visible/focused status?

window-buffer returns the buffer currently displayed by a given window. get-buffer-window, on the contrary, returns a window currently displaying the given buffer (or nil if there is no such window; ...
user avatar
26 votes

Can I use org-mode to structure my .emacs or other .el configuration file?

If all you want is selective showing of sections and navigation between headings, you don't need Org mode. All you need is Outline mode. Outline mode is basically the header level and section ...
user avatar
25 votes

Can functions access their name?

Updated answer with expansion time lookup: I said in my original answer that there may be a way to do this at expansion/compile time instead of run time to give better performance and I finally ...
user avatar

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