I am new to Emacs and ELISP
(message "Hello World") saved as
Now, how to run (evaluate) this file on Eshell?
~/Documents $ ....... hello.el
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 $
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
You find the actual message
Hello World in the
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).
lisp-interactionmode is active in
*scratch*. You can input your form and press C-j right behind it. The form is evaluated with
eval-print-last-sexpand 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.elactually stands for a fully fledged library which you want to test and if this library is in
load-pathyou can use M-x
load-libraryRET, input the name of the library, and press RET. This loads (i.e., executes) the library.
emacs-lispmode for the file buffer. If you visit that buffer there is a menu item
Emacs-Lispin the menu bar where you find
Evaluate Buffer. Sometimes it is easier to use the
find-libraryRET) to open a library instead of
find-file(the usual C-x C-f). Another important item of the
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.
org-modeoffers 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.
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
The current eshell sources the file
hello.el, i.e., it interprets its contents as eshell-commands.
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
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.
~/Documents $ eval-buffer hello.el Hello World