Skip to main content
Share Your Experience: Take the 2024 Developer Survey
26 votes
Accepted

What does `setq-local` do, and when should I use it?

Most of your assumptions are close. I'll mention a few later. But, first the main question. The form setq-local is merely a convenience, it is the same as doing make-local-variable followed by setq....
MAP's user avatar
  • 592
22 votes
Accepted

Mark a local variable safe for any value

Set the safe-local-variable property to a function that validates the value. For example, fill-column has a safe-local-variable property set to integerp. Use (lambda (x) t) as the validation function ...
Gilles 'SO- stop being evil''s user avatar
10 votes

Buffer-local after-save-hook?

This is standard functionality built into add-hook: add-hook is a compiled Lisp function in ‘subr.el’. (add-hook HOOK FUNCTION &optional APPEND LOCAL) Add to the value of HOOK the ...
phils's user avatar
  • 51.5k
9 votes

Set buffer-local variable in buffer other than the current one?

is there a way to do this without changing the current buffer No, but buffer-local-value is a Generalized Variable, so you can use setf to take care of the buffer switching for you: (setf (buffer-...
npostavs's user avatar
  • 9,203
8 votes
Accepted

How can I get buffer-local environment variables via .dir-locals?

Both the buffer-local and the global variable are initially pointing to the same cons cell / list. If setenv pushes a new value to the front of the list, that would only be reflected in the local ...
phils's user avatar
  • 51.5k
6 votes
Accepted

The default value of buffer-local variable not set until first `setq`

I, as a package author, bind case-fold-search to nil in the outer let, and I want to use the default value (it might or might not be set by the user) in the with-temp-buffer, In this case I would ...
npostavs's user avatar
  • 9,203
6 votes
Accepted

Why isn't face-remapping-alist permanent-local by default?

permanent-local is fundamentally a way to distinguish whether the variable's buffer-local value is really specific to this buffer or is specific to the mode currently in use in this buffer. Sadly, in ...
Stefan's user avatar
  • 26.5k
5 votes
Accepted

How can I get the effect of buffer-local symbol plist property values?

I am certain that the answer is no, and that variables are the only kind of buffer-local bindings provided by elisp. (I'm sure one of the elisp language maintainers will correct me if I'm wrong about ...
phils's user avatar
  • 51.5k
5 votes
Accepted

Set a buffer/file local variable in elisp like a file local variable

If you want to set a buffer-local value for a variable, use setq-local. Depending on what you are actually trying to accomplish, you may also want to look at the help for make-local-variable and make-...
glucas's user avatar
  • 20.6k
5 votes
Accepted

Set buffer-local variable in buffer other than the current one?

What you're doing is correct. is there a way to do this without changing the current buffer, i.e. can I explicitly specify the buffer whose value I want to change? While there is nothing built in, ...
phils's user avatar
  • 51.5k
4 votes
Accepted

How to get an insert-text-button 'action property accessing a dynamically-bound buffer-local variable

You can add not only action but any other attributes to a text-button, that can be referred later with button-get function. So saving the (reference to the) current buffer, together with action, seems ...
zk_phi's user avatar
  • 446
3 votes
Accepted

How can I change external program to open org URLs for one org file only

Error is caused by this quote: # -*- browse-url-browser-function: 'browse-url-chromium -*- ___________________________________| It shouldn't be there. Open video path in MPV on *nix OS's Add this ...
Alexandr Karbivnichyi's user avatar
3 votes
Accepted

Making a variable window-local

Save your data as a window-parameter. It will force you to use :eval inside your mode-line, but that's not something to be afraid of. If you want to discuss changes to the "core" Emacs features (...
Stefan's user avatar
  • 26.5k
3 votes

How to customize major mode initialization with local variables?

If you want to set a buffer-local value for a given mode, do so after the mode has been established. You do that by putting the value-assignment on the mode hook. For example, if you want to set ...
Drew's user avatar
  • 77.9k
3 votes
Accepted

How to set a variable on a single buffer?

eval: is a special form in the Local Variables section that evaluates the given Elisp-form. That is not what you want. The actual syntax for setting file local variables is: MAYBE_COMMENT ...
Tobias's user avatar
  • 33.2k
2 votes
Accepted

How to Persist Evil Markers?

desktop.el supports storing/restoring markers (now at least). (add-to-list 'desktop-locals-to-save 'evil-markers-alist) or (cl-pushnew 'evil-markers-alist desktop-locals-to-save) work for me.
noctuid's user avatar
  • 439
2 votes
Accepted

buffer-local advice

The doc you show isn't for advice-add but for add-function (which is used internally by advice-add but offers other functionality, such as working on variables (which can be buffer-local) rather than ...
Stefan's user avatar
  • 26.5k
2 votes
Accepted

Have shell inherit buffer local process-environment

That's actually a bit tricky on account of how shell is written, so the following is quite implementation-specific and could break if things change upstream; however, give this a whirl: (defadvice ...
phils's user avatar
  • 51.5k
2 votes

Buffer-local input history for read-from-minibuffer

Here's how to do it buffer-locally: (defvar-local my-hist-symbol nil) (defun my-function () (unless my-hist-symbol ;; Create the buffer's history symbol. (setq-local my-hist-symbol (make-...
Stefan's user avatar
  • 26.5k
2 votes

Buffer-local input history for read-from-minibuffer

Do this: (defvar my-hist nil) (make-variable-buffer-local 'my-hist) (defun my-function (hist) (read-from-minibuffer "> " nil nil nil hist)) (defun my-command () (interactive) (add-to-...
Drew's user avatar
  • 77.9k
2 votes
Accepted

How to provide my own ediff-make-wide-display-function for ediff

I think you've diagnosed the issue correctly, and you can either: Use ediff-mode-hook to set the buffer-local value. Use setq-default to set the default value. Offhand I think #2 seems like the best ...
phils's user avatar
  • 51.5k
2 votes

Cannot get a symbol's value for the first time when using `setq-default`

Put the make-local-variable after the setq-default: (with-temp-buffer (setq-default bar "xyz") (make-local-variable 'bar) (symbol-value 'bar)) setq-default sets the global value. ...
Drew's user avatar
  • 77.9k
2 votes

How to set a variable on a single buffer?

[Not an answer: just a summary and linearization of the comments, which are pretty confusing as they stand and mostly deal with a different problem.] The way to set a local variable is as described ...
NickD's user avatar
  • 30.4k
2 votes

Can I bind a key as buffer local but not in the active keymap?

Here's a way to do it by making isearch-mode-map buffer-local: (make-local-variable 'isearch-mode-map) (setq isearch-mode-map (copy-keymap isearch-mode-map)) (define-key isearch-mode-map (kbd "...
rpluim's user avatar
  • 5,315
2 votes
Accepted

`make-local-variable` Behaves Differently depending on Whether Variable is Special

Variables with dynamic scope can have global and buffer-local values, which are associated with the canonical symbol for the variable. The global vs buffer-local concepts are irrelevant to lexically-...
phils's user avatar
  • 51.5k
2 votes
Accepted

How can I set a buffer-local variable (format-all-mode) to nil within .dir-locals.el? (using Doom emacs)

The real question is about disabling a minor mode, but just to answer the stated question of setting a buffer-local variable, it's as simple as this: ((python-mode . ((format-all-mode . nil)))) I.e. ...
phils's user avatar
  • 51.5k
2 votes
Accepted

Is there a reason for having beginning-of-defun-function/end-of-defun-function global?

I can't think of a particular reason why it couldn't have been automatically buffer-local. or simply they forgot about them? More likely it was simply considered fine as it was, but you'd probably ...
phils's user avatar
  • 51.5k
2 votes

cannot use buffer-local variables after defining them

Presumably, since you setq the variable m-buffer-local-variable, that's the variable you want to print - instead you are trying to print a different (and undefined) variable mitmp-request-id. Assuming ...
NickD's user avatar
  • 30.4k
1 vote
Accepted

org-mode in-buffer todo setting not working as expected

By the time I posted my question I have manage to find the culprit. Posting it with an answer just in case others need to see this. The setting line was missing a space after the WAIT state and the "|...
Sergiu Elmi's user avatar
1 vote
Accepted

Buffer-local input history for read-from-minibuffer

What I'm actually trying to do is ... calling read-number from a separate function (my-command) with a buffer-local variable as HIST. As read-number doesn't have its own history list, you could let-...
phils's user avatar
  • 51.5k

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