6

I have a variable that I want to switch between two different values and have it apply for the whole session. I'm able to do this using M-x customize-variable and picking "Set for Current Session".

What functions is this calling? I'd love to be able to set a variable for the current session (NOT just for the current buffer, and NOT for future sessions) without using customize.

2 Answers 2

7

Any change to the variable is equivalent here, so just M-x set-variable or (setq THE-VAR A-NEW-VALUE) is sufficient.

The function called when you click Set for Current Session is custom-variable-set. (Well, actually it is more complicated - it boils down to calling either the value of (get SYMBOL 'custom-set), where SYMBOL is the option symbol or function set-default.)

The advantage of using Set for Current Session in the Customize UI is that you can use the Customize UI to edit the value - e.g., menus, completion, etc.

With the Customize UI there are thus two steps to changing a variable value:

  1. Edit the value.
  2. Set for session.

If you do not set the value (set for session) and you only edit it, then the variable value is not changed at all.


UPDATE, per comments:

In Customize, when editing the custom group, you can call function custom-group-set on the widget for the group. In terms of existing keyboard key bindings, you can use TAB to move to the State button, then RET to "push" the button.

Then you are shown a plain-text menu, with indications of number keys you can hit to choose one of the menu items. In this case, you hit 0 to choose the Set for Current Session action. IOW, you can use keyboard keybindings to set the edited changes for a group in Customize.

Other key bindings in Customize:

  • C-c C-c for Custom-set/[Set for Current Session]
  • C-x C-s for Custom-save/[Save for Future Sessions]
9
  • I don't think so. I'm customizing c-basic-offset, and M-x set-variable only applies to the current buffer. It even says in the prompt "Set c-basic offset buffer-locally to value:". Commented Dec 10, 2014 at 15:57
  • 1
    As always, you can use setq-default to change the default value of a buffer-local variable.
    – Drew
    Commented Dec 10, 2014 at 16:27
  • Yes! This is the answer I needed. I can do M-: (setq-default c-basic-offset 4). Thanks. Commented Dec 10, 2014 at 17:03
  • Instead of clicking Set for Current Session, what I execute with M-x to do the same thing (apply currently changed to session), or keybind?
    – Andy
    Commented May 1, 2018 at 4:23
  • 1
    @Andy: You can call function custom-group-set on the widget for the custom group. You are apparently using Customize to edit group state but you don't want to use it to set the changes. In terms of existing keyboard key bindings, you can use TAB to move to the State button, the RET to "push" the button. Then you are shown a plain-text menu, with indications of number keys you can hit to choose one of the menu items. In this case, you hit 0 to choose the Set for Current Session action. IOW, you can already use keyboard keybindings to set the edited changes for a group in Customize.
    – Drew
    Commented May 2, 2018 at 22:13
3

One option is to use customize-set-variable, i.e.

(customize-set-variable 'some-variable "some value")

Variables set this way (after your custom file has been loaded) will be considered as unsaved customizations, just like variables Set for Current Session. You can list unsaved customizations using customize-unsaved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.