I am new to Emacs and ELISP
A file (message "Hello World")
saved as hello.el
.
Now, how to run (evaluate) this file on Eshell?
~/Documents $ ....... hello.el
I am new to Emacs and ELISP
A file (message "Hello World")
saved as hello.el
.
Now, how to run (evaluate) this file on Eshell?
~/Documents $ ....... hello.el
You asked:
A file (message "Hello World") saved as 'hello.el'. Now, how to run (evaluate) this file on Eshell?
~/Documents $ ....... hello.el
I just fill in the command at the dots:
~/Documents $ load hello.el
t
~/Documents $
The reply t
of the load
command says that the file was loaded successfully Type C-h f load
RET to obtain a more detailed description for the load
command.
You find the actual message Hello World
in the *Messages*
buffer.
Note that there are many ways to execute elisp code. I list some of them:
*Messages*
buffer and in the view-area (i.e., the inactive minibuffer).*scratch*
buffer. lisp-interaction
mode is active in *scratch*
. You can input your form and press C-j right behind it. The form is evaluated with eval-print-last-sexp
and the result is printed in *scratch*
at the point where you pressed C-j. If you place point in the form and press C-u M-C-x the lisp form is instrumented for edebug and evaluated. That shows you what is going on step-by-step.hello.el
actually stands for a fully fledged library which you want to test and if this library is in load-path
you can use M-x load-library
RET, input the name of the library, and press RET. This loads (i.e., executes) the library.emacs-lisp
mode for the file buffer. If you visit that buffer there is a menu item Emacs-Lisp
in the menu bar where you find Evaluate Buffer
. Sometimes it is easier to use the find-library
command (M-x find-library
RET) to open a library instead of find-file
(the usual C-x C-f). Another important item of the Emacs-Lisp
menu is Instrument Function for Debugging
.ielm
-- an interactive emacs-lisp repl where you can input your lisp forms and get emacs' answer as response in the same buffer.babel
package from org-mode
offers an interesting way for literate programming. There you can write your documentation and embed the corresponding lisp snippets in source code blocks. These lisp snippets can be evaluated by C-c C-c on the header lines of the source blocks.hello.el
exists in the current directory the error message indicates that nil
is not in load-path
. The name of the current directory is printed with pwd
. The content of the current directory is listed with ls
. You can inspect load-path
with echo $load-path
. nil
in load-path
stands for the current directory. You can add nil
to load-path
with add-to-list `load-path `nil
or with (add-to-list 'load-path nil)
.
– Tobias
Jul 17 '17 at 20:34
load hello.el
working from the eshell
-prompt? Note, eshell
is a good shell. Its primary goal is to provide you with a shell that only depends on emacs but not directly on the platform. Elisp evaluation in the eshell is possible and it helps to get more complicated shell-jobs done with eshell
but eshell
is not intended to be a lisp-repl.
– Tobias
Jul 18 '17 at 10:44
The one word you request can also be just a dot .
.
After running that command you get:
~/Documents $ . hello.el
Hello World
~/Documents $
If this is too strange for you you can also use:
~/Documents $ eshell-source-file hello.el
Hello World
~/Documents $
Why that works:
The dot runs the eshell/\.
command which in turn runs eshell-source-file
.
The current eshell sources the file hello.el
, i.e., it interprets its contents as eshell-commands.
Since message
returns the generated message as string and eshell
prints the return values of the executed commands you get "Hello World" printed out in the eshell buffer.
Note that this is not the same as load
ing hello.el
! The content of hello.el
is not interpreted as sequence of elisp forms but as sequence of eshell-commands! eshell
just happens to understand elisp forms.
Be aware that the comment character of eshell is the hash-sign #
and not the semi-colon ;
as for elisp!
Because of the difference in the comment syntax this method will fail for most elisp-files.
Your example file hello.el
is both - a valid elisp file and a valid eshell file.
Note: I wrote this as a separate answer since it is a completely different approach (no elisp but eshell interpretation).
To run program written in Emacs Lisp from shell like other general languages, use (elisp) Batch Mode:
~ $ cat ~/Documents/hello.el
(message "Hello World")
~ $ emacs --batch --load ~/Documents/hello.el
Hello World
~ $
However, it is just much easier to run Emacs Lisp code within Emacs, see (emacs) Lisp Eval. since Emacs is the IDE of Emacs Lisp.
This worked:
~/Documents $ eval-buffer hello.el
Hello World
hello.el
is already opened in some buffer. Otherwise you get the answer No such buffer
. If you want to stick to eval-buffer
and hello.el
is not open yet you need something like find-file-noselect hello.el; eval-buffer hello.el
. Furthermore, I am almost sure that you did not try very hard to follow the other answers. Note, I did not down-vote your answer and I am very much against down-voting without commenting why.
– Tobias
Jul 21 '17 at 18:59
(load "/path/to/hello.el")
– lawlist Jul 17 '17 at 19:12