Hot answers tagged

91

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. But for others, it does matter, and setq is the wrong approach for those options. So as a general rule, setq is the wrong approach. If you use custom-set-...


52

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 "def") and use ! to force search-replace in the whole region. Stop recording macro. M-x kmacro-end-or-call-macro. Do M-x kmacro-name-last-macro and give the ...


47

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-emacs-directory)) (I'm using ~/.emacs.d/settings.org but that is personal preference). In the settings.org file you can arrange it however you would like, ...


43

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 configuration to keep things DRY. For me, the whole point of using Emacs is programmability, and the customize interface does nothing but get in the way. setq lends ...


42

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 do so. For the more idiomatic way to deal with the auto-mode-alist, see @Drew's answer (and his explanation of shadowing).


42

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 will shadow matches further down the list. (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))


35

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 comprehensive and almost entirely syntax-compatible, you can really ignore sregex unless you happen to be working with old code that used it). The other two syntax ...


31

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 can find different commands than Emacs does. As Francesco says, this can be especially confusing for shell-command, as that does not run a process directly, but ...


31

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 me: (defun command (a &optional b) (or b (setq b default)) (command-body a b)) As suggested in the comments, using unless may be preferable to or: (...


30

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 importantly, when byte-compiling). Yes. In fact, the first two are completely identical when evaluated. As described in the manual page you linked: The ...


28

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 without loading the file right away. Only when the autoloaded function is called is the corresponding file loaded. Basically with autoload you can delay the ...


28

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 something in another buffer, and save-current-buffer for more complex cases where you need to navigate between several buffers. (defun buffer-string* (buffer) (...


28

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 at anything that can be bound, including functions. It only cares whether there is a symbol that matches, not what the value is. bound-and-truep uses var and ...


28

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 :noexport: to separate files. Note that subtrees must have the :EXPORT_FILE_NAME: property set to a unique value for this to work properly." (interactive "sEnter ...


28

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 have been promises of fixing this after the move to git. In the meantime, I wrote a solution of my own (see below for a list of options). What are the technical ...


27

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, will be evaluated every time the function is called.


26

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-x elmacro-show-last-macro upcase-last-word produces a buffer with: (defun upcase-last-word () "Change me!" (interactive) (move-end-of-line 1) (backward-...


26

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; play with the optional 2nd argument to tell it how to behave in cases where you have multiple frames). With these two ingredients, you should be able to ...


26

See the variable user-init-file described in section 38.1.2 The Init File of the Emacs Lisp manual. Using this variable it is easy to build an interactive command that opens the init file: (defun my-open-init-file () "Open the init file." (interactive) (find-file user-init-file))


26

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 that it evaluates all of its arguments, in particular, the first one. push is a macro (as is cl-pushnew) - it does not evaluate its second argument; instead, it ...


26

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 function by the function described in the table in C-h f add-function, depending on your choice of the WHERE argument, only cleaner for the sake of tracking what ...


25

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 together which improves modularity. Finally, I would argue that navigating through an elisp buffer is more "easy" then navigating customize UI and widgets. On ...


25

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 break the next time it is run: (setq unread-command-events (listify-key-sequence "\C-g")) Note that this only feeds events to the command loop, so it will do ...


25

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 with the customization buffer, and customized-value to record the value set by the user with the customization buffer, but not saved. See Symbol Properties. ...


25

If you have the Emacs source code installed, you can find the source code for sort with M-x find-function. There you can see that sort performs a merge sort. It checks the length of the list, breaks the list into half, sorts the "front" and the "back" parts separately through recursion, and then merges the two. As for whether your implementation would be ...


24

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 implemented that today while working on my answer for this question: How can I determine which function was called interactively in the stack? Here is a function ...


24

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 funcalled. Your two forms are therefore completely equivalent, and the one with #' is preferred. Edit: as pointed out by Malabarba, this is not quite true: #' on ...


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