How can I make Emacs launch faster?

I am using Emacs v. 22 (console version, either remotely from PuTTY or locally from Konsole ) as the main text editor in Linux. It takes some time to load every time I start, although probably almost a second, although I never timed it. I very often open and close Emacs, because I prefer using the Bash command line to process files and directories and compile.

How to speed up startup time?

+66
performance emacs
Apr 22 '09 at 19:12
source share
17 answers

In addition to Adam Rosenfield 's solution , I recommend using Emacs in server mode . You can add (server-start) to your points and run emacsclient instead of emacs whenever you want to open a file in Emacs. Therefore, you only need to pay Emacs download costs once, after which clients will immediately appear.

Edit

You are right, v22 does not create a new frame. Create a shell script that does 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'"))' 
+37
Apr 22 '09 at 19:38
source share

Others have considered using gnuserve and emacsclient , and I would suggest compiling in emacs (the ability to go to compilation errors is a win).

But, especially .emacs acceleration can be done:

  • Byte compiling an .emacs file that you can do automatically using this piece of code

  • Replacing as many operators as possible (require 'package) autoload ed. This will delay lisp loading until it is needed. Using this technique allowed me to speed up my startup s> 6 seconds to <1. This requires a bit of work, because not all libraries are correctly labeled autoload .

  • Removing code / features that you no longer use.

  • Try running emacs with the --no-site-file option to avoid downloading unnecessary packages when installing site-start.el .

  • If you are really serious, you can roll your own emacs with your favorite functionality already loaded. This, of course, means that it makes more active changes to what you have in .emacs , because it is part of a binary file. 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 know what your .emacs loads? To remove functionality or to postpone it? Check the *Messages* buffer containing strings, for example:

 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 notice, the Loading statements can .emacs.tjackson.el : the first .emacs.tjackson.el ends with ... , and the last line shows the loading of .emacs.tjackson.el ...done . All of these files are downloaded from my .emacs.tjackson.el file. All other loads are atomic.

Note. If you have a large .emac, it is possible that the *Messages* buffer will lose some of the messages, since it only stores a fixed amount of information. You can add this option at an early stage to .emacs to save all messages:

 (setq message-log-max t) 

Note: the 'load command will suppress messages if its fourth argument nomessage not zero, so delete all such calls (or, we advise 'load and force the fourth argument to be nil ).

+56
Apr 22 '09 at 20:44
source share

Do not 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 to the front.

+26
Apr 22 '09 at 19:22
source share

A few tips:

  • Use startup

    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 increase in speed, although there are pitfalls if you work with version control, and your .emacs is newer than .emacs.elc. One common trick:

     (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 no need to close it. However, I note that you are still using emacs22. emacs23 supports multi-tty, which makes it much easier to run emacs on 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 are fantastic for such quick changes.

+21
Apr 23 '09 at 8:01
source share

One of

 Mx shell Mx eshell Mx term Mx ansi-term 

should satisfy your command line needs from Emacs.

You can also use M-! (aka Mx shell-command ) to perform a one-line translation without dropping into the shell.

+18
Apr 22 '09 at 20:46
source share

Check the .emacs file to see if you are downloading unnecessary packages. Downloading packages can take a considerable amount of time. For example, you might want to download the php-mode package if you are editing a PHP file. You can do this by setting the hook procedure, although I'm not sure about the details.

Also make sure that all packages you download are compiled ( .elc files). You can compile the elisp file by running

 emacs -batch -f batch-byte-compile thefile.el 

Compiled packages load much faster than uncompiled packages.

+14
Apr 22 '09 at 19:19
source share

"I very often open and close emacs because I prefer using the bash command line to manage files and directories and compile."

You describe how an editor like vim is used. Shoot at & out. Emacs usually stays open, and basically everything is done from within. hiena already answered what the correct approach would be here.

+13
Apr 22 '09 at 19:33
source share

The fastest way is to create a .emacs profile . I reduced the boot time from> 3s to 1s 5 minutes after I found that 4 lines in my .emacs took more than 80% of the boot time.

+11
Nov 05 2018-11-11T00:
source share

One thing that helped me reduce the load time of my .emacs , in addition to autoload (as others suggested), eval-after -load . In the following example, delaying the sql-set-product call eliminates the need to load sql into your .emacs , making sql autoload more efficient.

 (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 that you can do the same, but sometimes not, otherwise this method is just easier to think about.

+9
Apr 28 '09 at 17:17
source share

Emacs is designed to run all the time (or at least for long periods of time), so starting and stopping Emacs several times during the day is not recommended.

I would suggest using a screen. A screen is a terminal multiplexer that provides unlimited virtual terminals in one terminal.

After installation, just write "screen emacs" in your terminal. Emacs starts as usual, but pressing “ca c” (that is, press ctrl-a and then c) will open a new virtual terminal. You can return to emacs by pressing "ca ca" (this is two times ctrl-a).

You can even disconnect from the screen session, the key sequence is "ca d".

Rejoin the session by issuing "screen -R" and you will be returned to where you left. This allows you to start an emacs session at work, disconnect, return home and reconnect from home.

I have been running Emacs like this for several months in a row.

Here's the official website: http://www.gnu.org/software/screen/ , but try googling for on-screen guides and howtos

+8
Apr 22 '09 at 19:44
source share

One thing that others have not mentioned is to enable the elisp libraries that you use as part of dumped Emacs to move the library loading from the time Emacs starts until Emacs builds. This is not for the faint of heart, but if you load several libraries in .emacs , it may save you a few seconds of startup time.

+4
Apr 23 '09 at 9:26
source share

You can use benchmark-init to profile your Emacs startup. It will track which modules load and how much time is spent on each. The results can be presented either in the form of a table or in the form of a tree. The tree makes it easy to keep track of who downloads what might be useful when downloading a package with a lot of dependencies, and the tabbed form helps you quickly find where most of the time is spent.

Once you get these results, try to figure out if all the modules should load all the time or if you can load some of them on demand. For example, in my configuration, I download only Emacs extensions specific to certain modes when this mode is actually activated, since most of the time I use only a small subset of them in the session. eval-after-load , and here will be your mode hooks.

Using this method , my Emacs starts in 3-4 seconds , and I have installed next to 200 extensions . Most of the time is spent downloading Helm, which I always download, because it replaces find-file and other basic functions that are always needed, and CEDET, since I use the latest version and need to be downloaded before Emacs tries to load the older built-in version .

+4
May 2 '14 at 11:05
source share

This does not answer the question, but is relevant.

I don’t know how to do it faster, but there are a few things I could offer:

  • for most of the things you do on the command line, you can do them in emacs:

    • compile: compile Mx, then enter the command you use
    • my experience is only with C ++, but with g ++ you can press Cx `to go to the lines that the compiler complains about
    • execute shell commands: M- !, dumps the output to the buffer
    • interactive shell: Mx shell
  • you can run emacs as follows:

    • 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)
+3
Apr 22 '09 at 19:21
source share

I tried to solve the same problem when I came across this issue. I just wanted to add that the problem for me was not due to the loading time of emacs lisp packages, but that the host did not have a fully resolved host name

To check the package download time, do

 Mx emacs-init-time 

For me, it was 0.3 seconds, and yet the load time was extremely high. After changing the host name correctly, this fixed the problem.

To configure the fully resolved hostname / etc / hostname and / etc / hostsfile with:

 127.0.0.1 localhost localhost.localdomain 192.168.0.2 hostname hostname.domain 
+2
Oct 22 '14 at 15:04
source share

Try using the https://github.com/jwiegley/use-package macro to determine downloads and package settings. It handles lazy package loading for you, which makes it relatively easy to get a good start up time even with a lot of packages configured. I have almost 100 packages referenced by my .emacs, but the boot time is less than 2 seconds on Linux and 2.2 on Mac.

+2
Mar 16 '15 at 21:22
source share

I had a start time of 120 seconds. I managed to find a fix installing this:

https://github.com/dholm/benchmark-init-el put on top of 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 after starting emacs do:

Mx 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 , which speeds up my launch

see here: https://github.com/emacs-helm/helm/issues/1045

other that may be useful for you: https://www.emacswiki.org/emacs/ProfileDotEmacs

+2
Apr 03 '16 at 0:26
source share

I would need to check my setup, but there is a package called gnuserve or emacsclient. It migrates a lot, so you need Google to do this.

It starts one emacs session in the background. Any further emacs sessions are essentially just new frames for this session. One piece of advice is a quick launch time for your subsequent sessions.

+1
Apr 22 '09 at 19:40
source share



All Articles