2

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
  • 1
    How about (load "/path/to/hello.el") – lawlist Jul 17 '17 at 19:12
  • Yes, it shows that successfully loaded by displaying 't'. Now, how to get it display the message "Hello World"? ~/Documents $ (load "~/Documents/hello.el") t ~/Documents $ – Tom Jul 17 '17 at 19:52
  • 1
    If you are looking at writing elisp look at ielm and eval-region – eflanigan00 Jul 17 '17 at 20:03
  • I successfully did evaluation at direct buffer, ielm, and windows shell. Somehow, I feel very comfortable with Eshell. I was able to make it smoothly for Python, C, and C++ on Eshell, but when it came to ELISP, it was not working, and nowhere in google I could find a way out. – Tom Jul 17 '17 at 23:17
  • 1
    @Tom I guess you did just not see the message after the load. Check the Messages buffer. Hint: C-h e. – Marco Wahl Jul 18 '17 at 8:16
3

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:

  • If you just want to test a single elisp form you can type M-: input the lisp form in the minibuffer and press RET. The output is shown as message in the *Messages* buffer and in the view-area (i.e., the inactive minibuffer).
  • If you want to test small snippets of elisp you can use the *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.
  • If your example 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.
  • If you need to work on the library you can also open the library file. Emacs activates 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.
  • User eflanigan00 already mentioned ielm -- an interactive emacs-lisp repl where you can input your lisp forms and get emacs' answer as response in the same buffer.
  • The 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.
  • I get the following reply: ~/Documents/Code/El $ load hello.el Cannot open load file: No such file or directory, hello.el – Tom Jul 17 '17 at 19:41
  • @Tom If the file 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
  • Wow! Sir, this is way too much for a beginner like me!! I appreciate your very comprehensive reply. Let me try... – Tom Jul 17 '17 at 23:13
  • @Tom Did the receipt in my last comment help you to get 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
  • Sir, I just requested what is that ONE-WORD command to be filled on the DOTTED place. – Tom Jul 18 '17 at 19:25
2

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 loading 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).

1

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.

-1

This worked:

~/Documents $ eval-buffer hello.el

Hello World
  • This only works if the file 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
  • @Tobias You are RIGHT! I Checked your answer as the correct one. But it says that I am too new. What to do? – user16438 Jul 22 '17 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.