views:

272

answers:

6

I want to be able to make the changes to my .emacs file without having to reload Emacs.

I found three questions which sort of answer what I am asking (you can find them here, here and here), but the problem is that the change I have just made is to a toggle, and as the comments to two of the answers (a1, a2) to those questions explain, the solutions given there (such as M-x reload-file or M-x eval-buffer) don't apply to toggles.

I imagine there is a way of toggling the variable again with a command, but if there is a way to reload the whole .emacs and have the all the toggles re-evaluated without having to specify them, I would prefer.

In any case, I would also appreciate if someone told me how to toggle the value of a variable so that if I just changed one toggle I can do it with a command rather than re-start Emacs just for that (I am new to Emacs). I don't know how useful this information is, but the change I applied was the following (which I got from this answer to another question):

(setq skeleton-pair t)  
(setq skeleton-pair-on-word t)  
(global-set-key (kbd "[") 'skeleton-pair-insert-maybe)  
(global-set-key (kbd "(") 'skeleton-pair-insert-maybe)  
(global-set-key (kbd "{") 'skeleton-pair-insert-maybe)   
(global-set-key (kbd "<") 'skeleton-pair-insert-maybe)  

Edit: I included the above in .emacs and reloaded Emacs, so that the changes took effect. Then I commented all of it out and tried M-x load-file. This doesn't work. The suggestion below (C-x C-e by PP works if I am using it to evaluate the toggle first time, but not when I want to undo it). I would like something that would evaluate the commenting out, if such thing exists...

Thanks :)

+4  A: 

Some things you might want to try:

M-x load-file (then when prompted, type ~/.emacs enter). M-x means press the meta key (usually Esc on Linux and Windows desktops) then press the ordinary x character key.

Or, while your .emacs file is open, place your prompt just after a close bracket for the function you want to execute and type Ctrl-X, Ctrl-E. This executes that block enclosed within the nearest set of parenthesis to the left of the cursor.

This last technique I use frequently for complicated search-and-replaces. Say I'm editing an XML file and I want to move close tags onto the line before: I would type into my current XML buffer (query-replace-regexp "[ \r\n\t]*</" "</"), then place my cursor immediately after the closing parenthesis, and type Ctrl-X, Ctrl-E.

PP
Thanks for that. I had tried the M-x load-file before to evaluate the commenting out, but it hadn't worked. I was trying to check whether it would work if I was including the toggle for the first time, but I did something wrong and deleted all my customisations :( I will check whether it works for this case when I sort my mess and get back to you. The C-x C-e works for toggling it the first time, but it doesn't work when I am trying to comment things out. Another problem with it is that I have to do that for each line of my commands (unless it works if I select the 6 lines and execute it).
Vivi
Ah, I understand what you are asking now. There is no real way around having to re-start Emacs each time you change your .emacs file to find out the real effect it has. Setting up your .emacs file is one of those time consuming tasks that programmers love to do because they are programmers. My .emacs file hasn't changed much in the last 10 years, though. For what it's worth, experiment as much as you dare now - eventually you will have something stable that you become proud of for years.
PP
+2  A: 

Not sure I actually understood your question (a bit tired), but how about marking what you have changed (C-spc) and then M-x eval-region? Unless I've done some major stuff this does what I usually need.

monotux
I don't think this would work to get emacs to "comment out" my piece of code... It would evaluate to toggle it, but not to untoggle by making it a comment and asking emacs to evaluate the bit. This may be useful in other instances, though, so thanks :)
Vivi
+4  A: 

I take it that you want to experiment with turning on/off skeleton-pair?

I'd be tempted to do this by having:

(setq skeleton-pair t)    ; turns skeleton-pair on
(setq skeleton-pair nil)  ; turns skeleton-pair off

In the .emacs, and pressing C-x C-e next to the closing parenthesis for the particular version I want to try. Of course I'd have to remember to make sure that the .emacs only has the final setting that I want in it so it does the right thing next time I start up.

Damyan
Thanks for that. I wish I could accept more than one answer, but that is not possible. I appreciate the help, though :)
Vivi
@Vivi: The point is that every toggle function changes the setting of some particular Emacs variable. So if you only work with the variables, you don't have this class of problem. Working with variables isn't a panacea, though: if your setup does things like add items to alists that Emacs initialises, then these will get longer and longer each time you run your .emacs file.
Charles Stewart
+4  A: 

I included the above in .emacs and reloaded Emacs, so that the changes took effect. Then I commented all of it out and tried M-x load-file. This doesn't work.

It doesn't work because when you started Emacs you set a few variables and keys. Reloading .emacs with these commands commented out does not undo them. You'll have to overwrite the settings manually to undo them with

(setq variable-name nil)

and

M-x global-unset-key enter followed by the key you want to unset.

mmmasterluke
Just FYI: I too had the idea to uncomment stuff and then reload to undo it as an Emacs beginner. There are cases where this would be really handy but unfortunately this is not how things work.
mmmasterluke
Yeah, I understand... I also know that a lot of the things I want to do are unnecessary and useless, but that is how we learn, right? By being stubborn and trying to do things the way we want, and not accepting a way around it. I have no doubt though that a lot of what I am trying to do now I will end up changing back or forgetting, but that is life :)
Vivi
+4  A: 

You can always use the *scratch* buffer to evaluate expressions.

If you'd like to set your variable:

(setq skeleton-pair t)^J

Unset:

(setq skeleton-pair nil)^J

You have to use ctrl-J to invoke the evaluation; return (or ctrl-M) won't work.

This works for all lisp expressions, BTW. If you've got a quick calculation to do:

(+ 51 33)^J 84

Or if you want to find the width of your frame: (frame-width)^J 80

It's useful if you're tinkering or have non-interactive commands or variables to evaluate.

RealityMonster
Oh, so that scratch buffer is actually useful! Thank you, this is very helpful :)
Vivi
A: 

Each time you evaluate elisp code, it goes to the global emacs environment of this emacs session; so if you set variable value, it is still present in the environment even if you reload your .emacs.

It would be nice if we could pre-load a .emacs (or any lisp) in a "sandbox" to test it. When I am doing heavy .emacs customization I usually open one emacs for editing the .emacs and another one to evaluate (which I can relaunch to avoid corrupting the environment).

Zeugma