reduce load time emacs [duplicate] - emacs

I use Emacs v. 22 (the console version, either remotely with PuTTY or locally with Konsole) as my primary text editor on Linux. It takes a while to load up each time I start it though, probably almost a second, although I never timed it. I tend to open and close Emacs a lot, because I'm more comfortable using the Bash command-line for file/directory manipulation and compiling.
How can I speed up the start-up time?

Others have covered using gnuserve and emacsclient, and I'd suggest compiling within emacs (being able to jump to compilation errors is a win).
But, specifically speeding up the .emacs can be done by:
Byte compiling the .emacs file, which you can do automatically by using this snippet of code
Replacing as many of the (require 'package) statements with autoloaded functionality. This will delay loading of lisp until it's actually required. Using this technique allowed me to speed up my startup from >6 seconds to <1. This takes a little bit of work because not all libraries come properly marked autoload.
Removing code/functionality you no longer use.
Try running emacs with the option --no-site-file to avoid loading unnecessary packages in the site installation site-start.el.
If you are really serious, you can roll your own emacs with your favorite functionality already loaded. This, of course, means it's more involved to make changes to what you have in your .emacs because it's a part of the binary. Follow the link for information on how to use dump-emacs.
Buy a faster computer and/or faster disk.
How to determine what your .emacs loads
Now, how do you find out what your .emacs loads? With the goal to remove the functionality, or to delay it? Check your *Messages* buffer, which contains lines like:
Loading /home/tjackson/.emacs.tjackson.el (source)...
Loading /home/tjackson/installed/emacs/lisp/loaddefs.el (source)...done
Loading /user/tjackson/.elisp/source/loaddefs.el (source)...done
Loading autorevert...done
Loading /home/tjackson/.emacs.tjackson.el (source)...done
If you'll notice, the Loading statements can nest: the first .emacs.tjackson.el ends with ... and the last line shows the .emacs.tjackson.el load is ...done. All those other files are loaded from inside my .emacs.tjackson.el file. All the other loads are atomic.
Note: If you have a large .emacs, it's possible that the *Messages* buffer will lose some of the messages because it only keeps a fixed amount of information. You can add this setting early on to your .emacs to keep all the messages around:
(setq message-log-max t)
Note: It the 'load command will suppress the messages if its fourth argument nomessage is non-nil, so remove any such invocations (or, advise 'load and force the fourth argument to be nil).

In addition to Adam Rosenfield's solution, I recommend to use Emacs in server mode. You may add (server-start) to your dotemacs, and run emacsclient instead of emacs whenever you want to open file in Emacs. That way you have to pay the loading cost of Emacs only once, after then clients pop up immediately.
Edit
You're right, v22 does not create a new frame. Create a shell script that do the trick:
#!/bin/bash
# Argument: filename to open in new Emacs frame
/usr/bin/emacsclient -e '(let ((default-directory "`pwd`/")) (select-frame (make-frame)) (find-file "'$1'"))'
Edit 2
In v24+, you can do emacsclient -c to create a new frame.

Don't close Emacs every time you want to use the shell. Use Ctrl-Z to move Emacs to the background and the fg command in Bash to move it back to the foreground.

A couple of tips:
Use autoloads
Using autoload saves you from loading libraries until you use them.
For example:
(if (locate-library "ediff-trees")
(autoload 'ediff-trees "ediff-trees" "Start an tree ediff" t))
Compile your .emacs
Gives you a slight speed increase although there are pitfalls if you
work with version control and your .emacs is newer than .emacs.elc.
One common trick is:
(defun autocompile nil
"compile itself if ~/.emacs"
(interactive)
(require 'bytecomp)
(let ((dotemacs (file-truename user-init-file)))
(if (string= (buffer-file-name) (file-chase-links dotemacs))
(byte-compile-file dotemacs))))
(add-hook 'after-save-hook 'autocompile)
Learn to love emacs server.
Running emacs as a server means never having to close it down. However
I note your still using emacs22. emacs23 supports multi-tty which makes
it a lot easier to run emacs in one screen session and then bring up
new windows in another terminal. I use emacs to edit mail for my mail
client (mutt) and emacsclient is fantastic for these sort of quick edits.

One of
M-x shell
M-x eshell
M-x term
M-x ansi-term
should meet your command-line needs from within Emacs.
You can also use M-! (aka M-x shell-command) to execute a one-liner without dropping to the shell.

Check your .emacs file to see if you're loading unnecessary packages. Loading packages can take a significant amount of time. For example, you might only want to load the php-mode package if you're editing a PHP file. You can do that by installing a hook procedure, although I'm not certain of the details.
Also make sure that any packages you're loading are compiled (.elc files). You can compile an elisp file by running
emacs -batch -f batch-byte-compile thefile.el
Compiled packages load much faster than uncompiled packages.

"I tend to open and close emacs a lot, because I'm more comfortable using the bash command line for file/directory manipulation and compiling."
You're describing the way an editor like vim is used like. Shoot in&out. Emacs is usually kept open, and mostly all is done from "within it". hiena already answered what would be the correct approach here.

The fastest way is to profile your .emacs. I cut down my load time from >3s to 1s in 5 minutes after I found that 4 particular lines in my .emacs were taking up more than 80% of the load time.

One thing that helped me reduce the load time of my .emacs, in addition to autoload (as others have suggested), is eval-after-load. In the following example, delaying the call to sql-set-product saves you from having to load sql in your .emacs, making the exisiting sql autoloads more effective.
(eval-after-load "sql"
'(progn
(sql-set-product 'mysql)
(setq sql-mysql-options '("-C" "-t" "-f" "-n"))
(setq sql-sqlite-program "sqlite3")
))
Of course, for some packages there will be a hook available that you can do the same thing, but sometimes there isn't, or else this way just proves easier to think about.

Emacs is designed to run "all the time" (or at least for long periods of time), thus starting and stopping Emacs several times during a day is not recommended.
I would suggest using screen. Screen is a terminal multiplexer, giving you an unlimited virtual terminals in one terminal.
After installing simply write "screen emacs" in your terminal. Emacs will start as usual, but pressing "c-a c" (that is press ctrl-a and then c) will open a new virtual terminal. You can get back to emacs by pressing "c-a c-a" (that's two times ctrl-a).
You can even detach from the running screen session, the key sequence is "c-a d".
Re-attach to the session by issuing "screen -R" and you will be back where you left. This enables you to start an emacs session at work, detach, go home, and re-attach from home.
I've been running Emacs like this for months in a row.
Here's the official web site: http://www.gnu.org/software/screen/ but try googling for screen tutorials and howtos

You can use benchmark-init to profile your Emacs startup. It will keep track of what modules are being loaded and how much time is spent on each. The results can be presented either in a tabulated form or as a tree. The tree makes it easier to track who loads what, which can be helpful when you load a package with a lot of dependencies, and the tabulated form helps you quickly find where most of the time is being spent.
Once you have these results try to figure out if all of the modules have to be loaded all the time or if you can perhaps load some of them on-demand. For instance, in my configuration I only load Emacs extensions that are specific to certain modes when that mode is actually activated since most of the time I only use a small subset of them in a session. eval-after-load and mode hooks will be your friends here.
By applying this method my Emacs starts in 3-4 seconds and I have close to 200 extensions installed. Most of the time is spent loading Helm, which I always load since it replaces find-file and other core functions that are always needed, and CEDET, since I use the latest version and it has to be loaded before Emacs tries to load the older built-in version.

Try using the https://github.com/jwiegley/use-package macro to define your package loads and customizations. It handles deferred loading of packages for you, making it relatively easy to get good startup times even in the presence of large numbers of configured packages. I have almost 100 packages referenced in my .emacs, but my startup time is under 2 seconds on Linux, and 2.2s on the Mac.

One thing that others haven't mentioned is to include the elisp libraries you use as part of the dumped Emacs to move the library loading time from Emacs startup to Emacs build. It is not for the faint-hearted, but if you load several libraries in .emacs it could win you a few seconds of startup time.

I had around 120sec start time. I was able to find the fix installing this:
https://github.com/dholm/benchmark-init-el
put on top of your init.el
(let ((benchmark-init.el "~/.emacs.d/el-get/benchmark-init/benchmark-init.el"))
(when (file-exists-p benchmark-init.el)
(load benchmark-init.el)))
then once your emacs started, run:
M-x benchmark-init/show-durations-tree
On my side the problem was 127 secs in tramp-loaddefs
I fixed it by adding
127.0.0.1 host.does.not.exist
to /etc/hosts and that made my startup fast
see more here: https://github.com/emacs-helm/helm/issues/1045
another thing that maybe helpful to you: https://www.emacswiki.org/emacs/ProfileDotEmacs

This doesn't answer the question, but is kind of relevant
I don't know how to make it start faster, but there are a few things I could suggest:
for most things you do on the command line, you can do them in emacs:
compile: M-x compile, then type the command you use
my experience is only with C++, but with g++ you can press C-x ` to jump to lines that the compiler complains about
run shell commands: M-!, dumps output into a buffer
interactive shell: M-x shell
alternatively, you could run emacs like this:
emacs file.ext &
which opens emacs in the background so you can still use the shell ( this works best with putty and X forwarding with something like Xming)

I was trying to solve the same problem, when I came across this question here. I just wanted to add that the problem for me was not because of the load time of emacs lisp packages, but the fact that the host did not have a fully resolved hostname
To check your package load time do
M-x emacs-init-time
For me it was 0.3 seconds, and yet the load time was extremely high.
After changing my hostname correctly, it fixed the problem.
To configure your fully resolved hostname edit /etc/hostname, and /etc/hostsfile with:
127.0.0.1 localhost localhost.localdomain
192.168.0.2 hostname hostname.domain

I would have to check my customization, but there is a package called gnuserve or emacsclient. It migrates a lot so you will have to google for it.
It runs one emacs session in the background. Any further sessions of emacs are essentially just new frames of that session. One advatage is quick startup times for your later sessions.

Related

Use mit-scheme with REPL and editor together

I'm going through SICP course and as recommended installed mit-scheme. I want to use the REPL together with a scheme file. The reason is because I can add scheme code in the file and then run the commands in REPL. What I have works, but the problem is every time I edit the file, I have to quit terminal and reload the file for REPL to see changes.
Is there a way to reload the file easily or some other way for REPL to see changes from the file?
This my setup:
I installed mit-scheme with brew install mit-scheme
I have a local file named code.scm
In terminal, I load the file with mit-scheme --load /Users/name/Desktop/code.scm
Terminal now starts the REPL and everything works. The problem is that if I add new code to the file code.scm, I have to quit terminal and call this again: mit-scheme --load /Users/name/Desktop/code.scm
System details:
macOS Catalina - 10.15.6
Default Mac Terminal app - Version 2.10
MIT/GNU Scheme running under OS X
The text editor I use is Atom - 1.50.0
Question Edit #1 (Based on answer below)
I tried following instructions but this is complicated.
This is what I did:
Run mit-scheme < /Users/Desktop/code.scm
After this I ran mit-scheme --edit to open Edwin. I tried to use the code inside of the code.scm file but it doesn't recognize it. This is the code in code.scm file:
This is what I want to be able to do:
Notice in this picture, I can type a command, press enter and it automatically runs command. However, I want to be able to call (fib 5) and it references the function in code.scm file.
Could someone explain step by step how to do this? It's confusing looking at documentation for scheme websites.
There's actually a built-in load procedure available in the MIT Scheme REPL.
Evaluating
(load "path/to/file.scm")
causes the Scheme file located at path/to/file.scm to be evaluated at the top level (note that the double quotes around the file name are required).
And, as it turns out, this same function can be used to reload a file.
With this in mind, a possible "workflow" might look like this:
Create new source file
Evaluate (load "path/to/file.scm") in the REPL
Edit source file
Evaluate (load "path/to/file.scm") in the REPL
...etc.
Unfortunately, I don't think there is a built-in "reload" procedure.
But...if you find yourself reloading a lot (as I imagine you will), you can always quickly write your own at the beginning of a hacking session:
(define (reload)
(load "path/to/file.scm"))
And then just call (reload) whenever you make a change to your source file.
If you're interesting in using Emacs, I'd say it's worth a shot.
There's a bit of a learning curve, but it's not as steep as it looks up front :)
Also, I cannot recommend the Racket programming language(s) enough.
There is an incredibly straightforward way to set it up for SICP, and it's a much more forgiving environment than Emacs.
Let me know if you are interested and want any help getting started.
You should use emacs with xscheme.el. It works much better.
If you continue as you do, you can also do mit-scheme < code.scm or mit-scheme --edit code.scm and you will also get a *repl* buffer inside edwin.
I recommend you the emacs way, however.
A (load "/path/to/file") command should be available to you within MIT Scheme and the Edwin editor it comes with. However, I would actually recommend that you use Emacs, and use Geiser within that to access both the REPL and to help with scheme file editing. It also makes dealing with various Scheme REPLs such as Chez Scheme, Racket, MIT Scheme, Guile, Chicken, Gambit and Chibi Scheme fairly effortless. The same (load "/path/to/file") command would then be available to you within the REPL running under Geiser, within Emacs, but is generally much more powerful and seamless than when using the "naked" REPL. Emacs is very well tuned to use with Scheme and LISP. Highly recommended.
Evaluate entire buffer: press M-o (M is Alt on Windows). When in source file window, press it. It will evaluate the entire buffer i.e. (re)load the entire source file (without even saving it first). I found it by googling "mit scheme edwin tutorial". Edwin is kind of Emacs itself.
This page says: "C-c C-s when done in a scheme-mode buffer [i.e. Scheme source file window], will switch to the Scheme interaction buffer [i.e. REPL]". i.e. you press C-x C-s to save file, M-o to evaluate (i.e. load), C-c C-s to switch to the REPL.
If you've split your screen with C-x 2 between a source file buffer and the REPL ("interactions buffer"), you can switch between them by pressing C-x o (for "go to the other window").

How to reload and restart quickly in SLIME in development

I started using emacs and slime to develop some little service.
I have found a way to reload the code after changes but I want this a lot more convenient and faster.
This is how I doo it now:
1) start emacs, start slime, then in slime:
2) (load "init.lisp") ; load some initialisation code that does not
change
3) (load "myseervice.lisp"); this containts the code that i am
working on
4) (myservice:start)
5) At this point the seeervice is runing and i can test it. then I
make changes to myseervice.lisp, to modify it. To swap the code to
the new version I do this:
6) (myservice:stop)
7) (load "myservice.lisp")
8) go to 4) to start it again...
This works so far. But it is no fun to manually stop, reload and start. And there is a lot of output in slime between the calls, so it is not easy to reuse the previously typed commands 4)-7).
To have a solution I started a devhelper package which should do this for me in only one command, but it does not work:
(defpackage :devhelper
(:use :common-lisp :myservice)
(:export :start :reload))
(in-package :devhelper)
(defun start ()
(myservice:start))
(defun reload ()
(myservice:stop)
(load "myservice.lisp") ;I think it is not possible to load it here,
;because this module is using the file that it is just loading
;But it does not have to work this way,
;I just like any good solution
(myservice:start))
And I thought I could do it like this now:
1) start emacs, start slime, then in slime:
2) (load "init.lisp") ; load some initialisation code that does not
change
3a) (load "myseervice.lisp"); this containts the code that i am
working on
3b) (load "devhelper.lisp")
4) (devhelper:start)
5) At this point the seeervice is runing and i can test it. then I
make changes to myseervice.lisp, to modify it. To swap the code to
the new version I jussst dreamed I could:
6) (devhelper:reload)
But it freezes at this point.
And I am not sticking to this devhelper idea, I just want a smoother development cycle.
How would a real lisper do it? I am very new to all this and I come from conventional programming background ;) with IDEs and imports.
History
Reusing previous commands is not difficult in SLIME/Emacs.
The commands M-p and M-n get the previous or next input. M-r and M-s allows you to search (using a regular expression) in the input.
Execution from a buffer
Another way is to write down the commands into a Lisp file, open the file in a buffer and then you can execute them from there.
Reload
Your reload idea is okay. You could shutdown a service, load new code and start the service again. You should find out why it freezes. You should debug that. One of the differences between your manual and your coded version is this: the time between stopping, loading and starting is in the coded version much shorter. You should check if there is some problem.
More advanced organization of code
Typically when you have more than one file it makes sense to use one or more systems to organize the code. Also it makes sense if your code is in files that you use the file compiler to compile your code. This way you get warnings and errors early on. Often in Common Lisp developers use ASDF as a system tool. Many implementations additionally have their own (with more or less features).
A system tool provides you some commands that you can use on a system:
load: load compiled or source code if necessary (or forced)
compile: compile code if necessary (or forced)
compile and load: compiles and loads code if necessary (or forced)...
Usually it will issue the smallest amount of commands necessary to compile or load the code. But you can force it to reload or recompile everything.
Typically a system can have subsystems. A subsystem could for example be a compile service. If you change the code, save it and compile and load the subsystem. ASDF (or a similar tool) will compile the changed files and load them.
More advanced: write your own commands for the system tool, which then stops a running service, compiles/loads the changes and then starts the service.
Recommendation:
Get your version going, find out why it hangs.

Open new python shell on C-c C-c in python-mode.el

I have a small GTK python application that imports a package (Twisted) that may not be loaded twice.
If I run my application in emacs with python-mode.el and press C-c C-c, the application gets executed in a python shell window.
If I now close the application, the python shell stays up and running. If I now press C-c C-c again, emacs "reuses" the old python process and thus I run into problems because I'm installing a Twisted reactor twice.
Is it possible to have python-mode.el open a new shell window each time I execute a buffer?
python-mode.el comes with a command py-execute-buffer-dedicated,
opening a new and reserved process for it
In python.el, a new inferior process is launched in a new buffer if the python-buffer variable is set to nil. Therefore, it's possible to advise the python-send-buffer function to reset that variable to nil after every invocation, thereby forcing a new Python process to be executed for every subsequent python-send-buffer command. Something like the following should work:
(defadvice
python-send-buffer
(after python-send-buffer-new-proc activate)
(setq python-buffer nil))
(ad-activate python-send-buffer)
I know that your post was asking for help with python-mode.el, but I thought it might be helpful to mention this anyway, as I'd surprised if python-mode.el doesn't use a similar mechanism. If I have time, I'll try to look into it.
Edit: the python-mode.el package uses the command py-shell to initiate a new inferior Python process. I found a mailing list posting in which a user provides an ad hoc function that appears to do what you need.
By the way, it might be worth considering that trying to alter the default behavior of python-mode isn't the best approach to this problem. I don't know what your code does, and I'm not particularly familiar with Twisted, but it seems to me that experiencing major errors when evaluating your code a second time within the same session could be a sign of a more fundamental design problem. I fail to see how it could be a matter of multiple imports of the same module being the issue, as Python modules are only loaded once, with successive import statements having no effect (for that, an explicit reload or execfile() is required). If I'm completely off-base here, I apologize, but I felt this possibility might merit mention.

Is there a fast way for connect clojure swank and slime for a fast repl in emacs?

I've a question:
When I use emacs with clojure and elein (leiningen extension) I write my code inside a file, then I need connect to swang, I type "elein swank" and open the conexion...2 step) I open a repl...3) I type slime-connect (and press y two times) 4) this step is really annoying: I need use my file or change the namespace...
so far so good..the problem is if inside my file there is a little mistake (maybe a parentheses) now I insult my code and I've repeat all steps...again!!
for me this is really annoying, I really like emacs, I've used this for long time and is the best editor, but comparing this to netbeans (I try this today..It's nice but its repl suck...) with netbeans I only need ONE click for do all these steps...
I can press "load file" and this load my file inside repl....seriously!!
and if I press refer alias/file in NS it open the repl and change the namespace...so good
now..my question is if is possible create a command inside emacs than make everything..maybe and it would be really nice..a command than open a repl and load my file or my ns...would be great...is it possible?...has someone do it??..thanks
thanks a lot a have a good day!!
elein-swank should automatically connect to the swank backend after starting it for you. There was a recently-fixed bug which prevented this for some swank-clojure versions, so you might want to update your elein.el to the latest version.
You can use elein-reswank to restart the backend and reconnect to it if necessary.
Once it's running, C-c C-k will compile and load a .clj file in the backend. C-c C-z will flip you to the REPL from any clojure source buffer.
Hopefully those tips will help to streamline your emacs/slime experience -- it's really a nice working environment, so stick with it if you can! :-)
You might want to try out swank-clojure if you aren't already.
With swank-clojure you just M-x clojure-jack-in and it loads the REPL. If you call it from within a project.clj file, it makes all the namespaces of your project available. It takes a few seconds to start, but after that it's very easy and you don't have to reload.
Why you need to reload everything if you made one typo? You just need to run lein swank once and connect to swank using slime-connect... And then you can load and evaluate your code as you want. To (re-)load your file you can use slime-load-file command, that is bound to C-c C-l...
You can also to look to M-x clojure-jack-in command from fresh clojure-mode...
P.S. I personally run swank sessions for a whole day (and sometime several days), without leaving it, writing new code, evaluating it, etc.

Implementing a continuous "revert-buffer" aka Textpad

One of my colleagues uses TextPad, and one feature I found really useful is the Auto-Reload. (The feature has been described in this SO quesion: Alternative to TextPad's Prompt to Reload File). Basically, it keeps reloading the file without any prompt from the user, which is really helpful when monitoring log files that are updated in real-time. Is there something similar available for Emacs? If not, can anyone whip up the required elisp magic?
M-x auto-revert-mode
I should add that for log tails, there is the more specific auto-revert-tail-mode, and that if you like it as a general feature (my case), you can turn on global-auto-revert-mode, to revert all buffers. Beware of remote files in that case.
If you want auto-revert to apply everywhere you can also use global-auto-revert-mode. Add
(global-auto-revert-mode 1)
to your .emacs
Here's my preference, FWIW: I do not use auto-revert. Instead, I bind f5 to this command:
(defun revert-buffer-no-confirm ()
"Revert buffer without confirmation."
(interactive) (revert-buffer t t))
Sounds silly, but that simple change makes all of the difference. This is what f5 does anyway on MS Windows, so it's a habit that works in all applications (on Windows).
Note that I do not change (e.g. remap) any bindings for revert-buffer. I use this only when I explicitly want to revert without confirming (which is quite often, in practice).
HTH.