I wonder which one should I use.
Here are the definitions from Emacs:
with-eval-after-load is a Lisp macro in ‘subr.el’.
(with-eval-after-load FILE &rest BODY)
Execute BODY after FILE is loaded.
FILE is normally a feature name, but it can also be a file name,
in case that file does not provide any feature. See ‘eval-after-load’
for more details about the different forms of FILE and their semantics.
Probably introduced at or before Emacs version 24.4.
eval-after-load is a native-compiled Lisp function in ‘subr.el’.
(eval-after-load FILE FORM)
Arrange that if FILE is loaded, FORM will be run immediately afterwards.
If FILE is already loaded, evaluate FORM right now.
FORM can be an Elisp expression (in which case it’s passed to ‘eval’),
or a function (in which case it’s passed to ‘funcall’ with no argument).
If a matching file is loaded again, FORM will be evaluated again.
If FILE is a string, it may be either an absolute or a relative file
name, and may have an extension (e.g. ".el") or may lack one, and
additionally may or may not have an extension denoting a compressed
format (e.g. ".gz").
When FILE is absolute, this first converts it to a true name by chasing
symbolic links. Only a file of this name (see next paragraph regarding
extensions) will trigger the evaluation of FORM. When FILE is relative,
a file whose absolute true name ends in FILE will trigger evaluation.
When FILE lacks an extension, a file name with any extension will trigger
evaluation. Otherwise, its extension must match FILE’s. A further
extension for a compressed format (e.g. ".gz") on FILE will not affect
this name matching.
Alternatively, FILE can be a feature (i.e. a symbol), in which case FORM
is evaluated at the end of any file that ‘provide’s this feature.
If the feature is provided when evaluating code not associated with a
file, FORM is evaluated immediately after the provide statement.
Usually FILE is just a library name like "font-lock" or a feature name
like ‘font-lock’.
This function makes or adds to an entry on ‘after-load-alist’.
See also ‘with-eval-after-load’.
Unfortunately I did not find any info on it in the official manual.
According to ChatGPT (I know, not very reliable):
The key difference is that eval-after-load requires you to load the library explicitly using require before using it, while with-eval-after-load does not require a separate require statement because it automatically ensures that the library is loaded before executing the code inside the block. with-eval-after-load is more convenient and concise and is the preferred way to handle library loading and code execution in modern versions of Emacs.
But eval-after-load also does not require a separate require
, so I do not know what is going on and which one to use.
Can anyone please the differences between those two?
with-eval-after-load
accepts many brackets (special expressions?) while witheval-after-load
one can only get one. Are there any other differences?