reddit / emacs

Why is it hard to get started with elisp in emacs

Alternative-Ant-4741 · 2022-01-30 19:40
I'm have wanted to learn elisp for a while ever since I got started with Emacs using doom. I downloaded Programming with elisp book, but I found it very hard to follow along because since I'm new to emacs I couldn't manage to run a standalone elisp script in emacs. Basically I created a new .el file but fly check gives me a lot of errors like I need to add a bunch of comments to the start of the file. I don't remember what they were but adding them didn't fix anything. So I'm wondering is there an easy way to get started with elisp programming in emacs? Edit: thanks a lot for all the kind suggestions. All this makes me want to jump on elisp and learn it. I didn't expect such a positive response to my naive question. Thanks a lot guys/gals
featherthefish · 2022-01-30 19:47
You can run code in the scratch buffer, you might want to invoke emacs-lisp-mode when in the scratch buffer. You can also try org-babel and run your snippets in there. Which has the benefit of being alongside your notes.
TheTratz · 2022-01-30 19:54
I've been using Emacs for a couple of years now and only really started putting in the effort to understand elisp myself. Lisp is the second oldest high level interpreted language aside from Fortran, and like most old spoken languages there are a few divergent dialects. For over 40 years elisp hackers have been adding/changing things, adding macros, adding confusing abstractions and reinforcing a specific pattern of functional programming. Once it clicks it gets easier. I still get confused sometimes and feel uncertain about when xyz function requires quoted symbols (so that they're not immediately evaluated) and when they don't, or when/how autoloading happens and when it doesn't. Speaking of autoloading, elisp is imperative and things like timing are import. There's not lexical scope until you ask for one...so undeclared variables may get called if you don't watch your timing, or observe an autoload properly. Anyway, good luck.
Alternative-Ant-4741 · 2022-01-30 20:14
Being a total beginner, what is a scratch buffer? And how to open one?
xenodium · 2022-01-30 20:19
If you like [REPLs](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop), try `M-x ielm` (elisp) (+ 5 10) 15 (elisp) (setq my-sum (+ 5 10)) 15 (elisp) (format "the sum is %d" my-sum) "the sum is 15" ps. Mickey's [Evaluating Elisp in Emacs](https://masteringemacs.org/article/evaluating-elisp-emacs) may be of interest. edit: adding links
Imaltont · 2022-01-30 21:01
There is always one there. Just C-x C-b and it should be there, named \*scratch\*.
TheBB · 2022-01-30 21:02
I honestly can't think of a language that was less difficult to get into, for better or worse. I don't recognize myself in your difficulties.
mtk000 · 2022-01-30 21:35
if you start emacs without any customization (alternatively if you run it with the command line argument "-Q"), it will begin with one open buffer named "\*scratch\* and that buffer's mode will be "lisp interaction mode". "lisp interaction mode" is special (and fun!). you can type lisp programs (lisp expressions) into it and run them by typing C-j immediately after the expression. e.g. if you entered "(car '(a b c))" and then immediately typed C-j in the \*scratch\* buffer, it would print "a" (because "car" is a function that returns the first element of a list). if you entered "(cdr '(a b c))" and then C-j, it would print "(b c)" because "cdr" returns the tail (everything but the head) of a list. if you typed "(defun head(x) (car x))" and C-j, it would print "head" to tell you that it just defined a new function named "head" which just runs the "car" function. if you then typed "(head '(a b c))" it would print "a" again, just like it did for "car" earlier. if you're paying close attention, you may have noticed the single quote character in these lisp expressions. "'(a b c)" is the same as "(quote (a b c))". it tells emacs lisp not to interpret the head of the list "(a b c)", the "a", as the name of a function to call. it means that "(a b c)" is just pure data. you might want to track down a beginner lisp (not necessarily elisp) tutorial and use an emacs \*scratch\* buffer in lisp-interaction mode to explore.
agumonkey · 2022-01-30 21:35
don't mind flycheck for now, try to use ielm as other people mentioned
TheTratz · 2022-01-30 21:43
As you are the maintainer of evil, it's reassuring to know you feel this way
7890yuiop · 2022-01-30 21:43
> I couldn't manage to run a standalone elisp script in emacs. What did you try?
[deleted] · 2022-01-30 22:01
[deleted]
AimForTheAce · 2022-01-30 22:03
Let's unpack this. First, if you are struggling with basic Lisp part, there is no getting around. Learn it. It shouldn't be too hard. Symbols, property, lambdas, cons cells, condition control structures, loops, variable binding, scope. The basics. Having said that, [35y ago when I first learnt Lisp](https://en.wikipedia.org/wiki/Allegro_Common_Lisp), up until I met a lisp book that explained Lisp as programming language, I was confused. The book explained what symbol, the cons cells, the list, function, etc and how they are laid out as a programming language. I assume you know other programming language. If so, we may able to map and contrast the basic language concept. Second, there are a lot of Emacs the editor specific concepts which is somewhat unique. Buffers, Cursor positions, the kill rings, etc. If you are familiar with Emacs, the construct of buffers, etc. are pretty straight forward, but it needs some practice to be able to efficiently manipulate these.
paretoOptimalDev · 2022-01-30 22:06
I'd recommend: - https://www.masteringemacs.org/ - [An Introduction to Programming in Emacs Lisp AKA eintr](https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html) - https://learnxinyminutes.com/docs/elisp/ - Compare [elisp examples on RosettaCode](http://rosettacode.org/wiki/Category:Emacs_Lisp) to another language you know Oh, and don't forget actually writing elisp code. Here's a fun video live coding a simple spotify client: https://youtu.be/XjKtkEMUYGc The other important thing to know is that knowing Emacs architecture and how buffers are used is the harder part of things than actually learning elisp in my experience. IIRC mastering emacs goes over that.
arthurno1 · 2022-01-30 22:06
"I couldn't manage to run a standalone elisp script in emacs." Press M-: and at the prompt type (message "Hello World!"). Press enter. Open new file, in any mode, at the top, type (message "Hello World"), place your cursor after the last parenthesis or on a line below and type C-x C-e. You have just run an elisp script in two different ways. In both cases you should have seen "Hello World!" message in your minibuffer. For more intro, you can [try this tutorial](https://learnxinyminutes.com/docs/elisp/) and then retry with the "An Introduction to Programming in Emacs Lisp", a tutorial-book on Emacs Lisp programming that comes with Emacs. You can type C-h i, and then choose "Emacs Lisp Intro" from the menu, you don't need to download it from the internet.
stayclassytally · 2022-01-30 22:11
Care to elaborate ? How is it not a lisp?
diagonal-d · 2022-01-30 22:27
You also might want to look at the Emacs tutorial C-h t (Help>>Emacs tutorial) that will help you with terminology. The emacs lisp tutorial is also in the help menu at Help>>More Manuals >>Introduction to Emacs Lisp.
TheTratz · 2022-01-30 22:40
Wikipedia seems do disagree with you. Also there are aspects of common lisp (a popular dialect) adopted by emacs in elisp. It's not apples and oranges. It's the Honey Crisp and Pink Lady
dpoggio · 2022-01-30 22:46
Wow! Really? I don’t know what to say against Wikipedia dude.
dpoggio · 2022-01-30 22:47
I said it’s not Lisp, not “a Lisp”. Read carefully.
TheTratz · 2022-01-30 22:48
Nice. You should go edit it since you're sure you're right 😊
stayclassytally · 2022-01-30 22:52
👍
thepoetlogician · 2022-01-30 23:11
I used to be in the same spot as you a while back but everything changed when I stopped using doom emacs and just used vanilla emacs and installed evil and related packages within it. It feels so much easier to work from the ground up rather than to work within an environment where things are already highly configured. It may sound difficult but it suprisingly really isn't and will help you understand doom emacs more aswell if you decide to go back to it
avindroth · 2022-01-30 23:18
The level of interactivity in your emacs determines how easy trying emacs-lisp becomes. I suggest checking out [https://github.com/abo-abo/lispy](https://github.com/abo-abo/lispy), it makes it easy to look up documentation (C-c 1 I believe) and evaluate S-expressions on the fly (keybinding is e). Also C-h f, C-h k, C-h v are always very helpful. Also check out helpful (the package), selectrum, marginalia, prescient, etc. I ended up learning just by trying and searching stuff within emacs. Perhaps this is why there is less material on the web; most of it is so readily available within emacs. It was such a joy to learn in a fluid manner.
avindroth · 2022-01-30 23:20
You really need to meet someone like yourself on reddit for some mirror therapy ;)
Alternative-Ant-4741 · 2022-01-30 23:37
I created a .el file to try out the code snippets in "An introduction to emacs lisp", but I got a bunch of error messages to add some comment blocks to the beginning and end of the file. I added them but some messages never went away. So basically I'm having hard time understanding why those comment snippets are needed and if there's a better way to do it and how to run the elisp code itself like I would run a python script for example
ftrx · 2022-01-31 00:03
You *can* but not need to save "elisp" to a .el file. Emacs is a live environment, think something like Jupyter for Python, A REPL, only not made as a limited shell, but as a full editor. You can write elisp anywhere in Emacs and execute anywhere, an entire buffer (i.e. the "textarea" you are using to write things down), a single sexp (i.e. a bit of elisp between parenthesis, a region (a selected text area). In Emacs anything is a lisp function, any *callable* (i.e. "interactive") function can be run via `M-x` "shell", any function can be run writing it down and executing it with a simple `C-x C-e` (`eval-last-sexp`) after the closed parenthesis. Almost all function have meaningful names. Many are documented, sometimes well/extensively, sometimes less well, in any case `C-h f` ask you for a function name (eventually proposing one if your point, i.e. the cursor is on one) and show the relevant doc. With helpful package show also the source code. `C-h v` do the same for variables etc. `C-h h` is "the master help". Debugging is a bit cumbersome respect of classic/modern debuggers but works well enough. Honestly IMVHO before trying elisp try Emacs, see a series of video tutorials on YT, they are many, from Mike Zamanski to Stavrou Protesilaos passingh through David Wilson and many others, when you have your Emacs up and running as an initial new part of your workflow elisp came along naturally. Emacs config is elisp, so to build your Emacs you learn Elisp at least a bit. When you start using it you naturally want to automate something, and then you start looking for elisp and so on. Elisp is not means like a generic lisp (to name one SBCL), it's part of Emacs, you can write script using Emacs as a mere shell to batch run them, but that's more an absurd demo than something useful. Emacs like classic Small Talk environments is: - an operating environment, so an end-user AND developer desktop - a virtual machine for a language - an editor/set of tools to develop in that language .el files are just tools to save&share code, not much "the most basic thing" you do for a hello world. To be end-user programmable the modern/classic workflow of write a file, run it with an interpreter or pass through a compiler is overburden. You just write your code in your environment, a bit at a time, and you learn it a bit at a time since the code is also the live environment and your personal functions are the same of any other Emacs function so your config is not different than any other Emacs package or Emacs itself.
7890yuiop · 2022-01-31 00:08
It sounds like you're getting confused because you're using `flycheck` and it's trying to get you to format your files as if you were planning to release them to the public, which is obviously not the case. Just disable flycheck. If you're only experimenting with and learning about Emacs Lisp there's no need for any of that formality.
dpoggio · 2022-01-31 00:11
Would be nice, a bold comment for someone who don’t know me.
BeetleB · 2022-01-31 00:50
Just want to say: I was a power user for a decade before I finally learned elisp. Don't feel inadequate if it doesn't click. You can do plenty with Emacs without it, and can always attempt learning it again in the future.
Sethaman · 2022-01-31 01:00
recommend "System crafters emacs from scratch" tutorial. It'll get you up to speed and familiar with some of the "how to emacs"
Both-Level4667 · 2022-01-31 01:30
Hi, I am someone who is in the same boat as you. My suggestion is - do not copy the configuration files. You may take a look at them but write them yourself. I am doing this for awhile now and my proficiency is increasing - slowly but surely.
mike1111111111111 · 2022-01-31 02:12
okkay, im in kind of the same boat. Something that really opened my mind was some youtube videos. The one i saw was Xah lee's. He is kind of long winded, so maybe there's something better, but I learned some stuff by passively watching it.
AuroraDraco · 2022-01-31 02:16
Thats weird, as for me, who is not so familiar to programming, I have to say, elisp (inside the emacs ecosystem) has got to be one of the easiest languages to learn. To start learning, open emacs, invoke the info program, with M-x info or C-h i and find the section called "An introduction to emacs lisp (eintr)". Its an exceptional book that takes you step by step and explains everything. I can't recommend it enough. The great thing about learning emacs lisp is the fact that every variable/function is well documented inside emacs, the interactive repl, and the fact that you can evaluate elisp and get real time results in the minibuffer are all invaluable. Also, although I get that for people used to programming the Lisp syntax might not make much sense, for me it makes a lot of sense, which makes it easier.
ramin-honary-xc · 2022-01-31 03:52
(I am going to copy-paste [a reply I made to a similar question](https://www.reddit.com/r/emacs/comments/seklzr/comment/hul5lh5/?utm_source=share&utm_medium=web2x&context=3 ) the other day.) ---- I've been meaning to write a blog post about how you can quickly get started experimenting writing Emacs Lisp programs. But I want it to be a high-quality article with an accompanying YouTube video and just haven't got around to writing it yet. #### Learn how to navigate the Emacs documentation: - C-h f <function name> describes a function and gives you a link to the code that defines it. It is a fuzzy match with tab completion so you don't need to know the exact name, so you can search for functions that do stuff by keyword. - C-h v <variable name> describes a global variable you can configure. - C-h w <function> tells you if there is a key binding that you can use to call the function you typed - C-h k <any key> tells you what function is called when you press a key binding. Great for writing macros. #### Browse through the Emacs Lisp documentation. - It is [available online](https://www.gnu.org/software/emacs/manual/html_node/elisp/index.html#SEC_Contents ) - But it is better to read it in Emacs Info mode. If it isn't installed, try to get it installed using whatever package manager your operating system provides. #### Play around with writing your own code Use the scratch buffer, and use the `C-M-x` key (or I think `<leader> d` in Doom normal mode), to evaluate a `(progn ...)` block of code: (progn (switch-to-buffer-other-window "*elisp-output*") (erase-buffer) ;; This is a dangerous function, use it with care. ;; Now do whatever you want. (insert "Hello, world!\n") (insert (format "123 + 546 = %S\n" (+ 123 456))) ;; Prompt the user for input: (let ((name (read-from-minibuffer "What is your name? "))) (insert (format "Hello, %s, nice to meet you.\n" name))) ;; Build data structures, prompt the user: (let*((colors (apply #'vector (split-string "red orange yellow green blue indigo violet" " "))) (num (read-minibuffer "Type any number: ")) (lucky (elt colors (% num (length colors))))) (insert (format "Your lucky color of the day is %s.\n" lucky)) (insert (format "Your lucky number today is %i.\n\n" (1+ (random 100)))) (insert "-------------\n") (insert "Other colors could have been:\n") (mapc ; This is like a "foreach" loop (lambda (color) (insert (format "%s\n" color))) colors)) ;; Programatically find and replace text (search-backward "Hello") (replace-match "Goodbye") (search-forward "nice to meet") (replace-match "it was nice meeting") ;; Apply colors and styles (i.e. "faces") to text (let ((line-start (move-beginning-of-line 1)) (line-end (move-end-of-line 1))) (set-text-properties line-start line-end '(face (bold :foreground "blue")))))
[deleted] · 2022-01-31 07:06
My starting steps: 1. Emacs Tutorial (start emacs, then "C-h t") 2. An Introduction to Programming in Emacs Lisp, by Robert J. Chassell 3. Config the working environment in `~/.emacs` 4. GNU Emacs Lisp Reference Manual, by Bil Lewis, Dan LaLiberte, Richard Stallman, the GNU Manual Group, et al. 5. Writing your own library For each step, when it feels "too much", we can stop there. And when it feels "necessary", we can continue and move on. I feel like taking emacs as an integrated working environment but not just an editor, and emacs lisp as a general purpose programming language (except GUI programming).
basicfn · 2022-01-31 07:18
[This](https://exercism.org/tracks/emacs-lisp) might help you.
holgerschurig · 2022-01-31 07:32
> Basically I created a new .el file but fly check Maybe start with a more vanilla Emacs configuration. To the best of my knowledge, flycheck isn't enabed automatically. Alternatively, just turn it out. Flycheck is extremely opinionated when it comes to elisp, almost like a cultist. It assumes that your *.el file should be given to the FSF, so that they can publish it. Therefore it asks for a lot of pointless comments, in the very specific form that is customary for Emacs modules. It's even so opinionated that it tries to enforce US-style "two spaces after a dot" in docstrings, as if this would be used anywhere in the world. But these are comments ... they don't influence program run at all. So, as a beginner, just turn it off.
holgerschurig · 2022-01-31 07:32
Run `emacs -Q`. This is a vanilla, unconfigured Emacs. And you'll be right into an elisp-mode buffer called "*scratch*". Also totally undisturbed by flycheck.
holgerschurig · 2022-01-31 07:35
> Lisp is the second oldest high level interpreted language aside from Fortran, To the best of my knowledge, FORTRAN was never interpreted. It was always compiled. So, if i look at https://devskiller.com/history-of-programming-languages/ than Lisp is the oldest language originally interpreted. Some Lisps are compiled, btw. And even Emacs Lisp can be compiled in the newest version.
TheTratz · 2022-01-31 07:40
Thanks! I should've dropped the interpreted part, my bad
InfinitivesUnite · 2022-01-31 09:59
I highly recommend this YouTube playlist from System Crafters. [Learning Emacs Lips](https://youtube.com/playlist?list=PLEoMzSkcN8oPQtn7FQEF3D7sroZbXuPZ7). He does such a nice job with pacing and presentation.
T_Verron · 2022-01-31 10:11
Amazing for interactive development. You can also use directives such as "set-buffer" to run commands as if the current buffer was another one.
WonderLemming · 2022-01-31 10:22
If you're familiar with Org mode then I recommend taking notes in that and then saving code examples that relate to the note like so: #+BEGIN_SRC elisp :results output drawer (print (+ 4 5)) #+END_SRC If your cursor is in that SRC block you can `C-c C-c` to evaluate it and it will produce this in your Org file: #+BEGIN_SRC elisp :results output drawer (print (+ 4 5)) #+END_SRC #+RESULTS: :results: 9 :end: That's how I practice my `elisp` or any other language, really.
Alternative-Ant-4741 · 2022-01-31 12:45
That sounds nice. I am getting started with orgmode. So this is indeed nice
Alternative-Ant-4741 · 2022-01-31 12:46
I have seen this video. I wanted to do it. Just need to find time. He has a lot of nice videos on emacs
andreyorst · 2022-01-31 12:51
honestly, two spaces after dot is just the most reliable way for distinguishing sentences in plaintext. Even though I don't live in US, I find this convention pretty useful, and make use of it for text processing quite often.
wadawalnut · 2022-01-31 14:40
I've been using the emacs shell for this, what's better about ielm? I've never heard of it.
wadawalnut · 2022-01-31 14:50
Emacs is basically an interactive elisp interpreter, so my suggestion is just to dive straight into the deep end and play around with it, and eventually it'll become natural. The elisp manual online is quite helpful when you get stuck. Rather than trying to write a program like you would with other languages, I suggest trying to think of a shortcut that you would like to make text editing faster. For example, one of the first things I wanted to do when I started using emacs was to make a function that automatically creates LaTeX theorem blocks for me in org-mode. So now when I run `M-x insthm`, I'm prompted to enter a block type (which will usually be theorem, lemma, definition, etc), and then it inserts #+begin_<block type> <cursor goes here> #+end_<block type> Very simple, but it saves me lots of time. Eventually you'll be more comfortable and adventurous and that's when the fun starts.
brotzeitmacher · 2022-01-31 14:55
I started with this https://www.youtube.com/watch?v=QRBcm6jFJ3Q&t=1s years ago. Helped me a LOT. Have fun.
Alternative-Ant-4741 · 2022-01-31 19:12
This is amazing. Just what I wanted with the buffer. Thanks a lot
ideasman_42 · 2022-02-01 14:46
Most tutorials focus on elisp basics, but to do something useful - you end up getting into details of emacs's lisp API. Some of these are quite spesific and it's not obvious which approach is "normal" when your new. Suggest breaking your problem down and using https://emacs.stackexchange.com - ask new questions if they have not been asked before.
tgerdino · 2022-02-01 21:04
Compared to something like VSCode, I would say that it's incredibly easy to get started with elisp programming. Just do M-x emacs-lisp-mode, and then C-M-x any expression! (M-: is also very useful)
redguardtoo · 2022-02-02 05:00
Try to use Emacs for one year. Maybe you could learn Emacs Lisp without reading any tutorial. In you case, you could just turn off `flyspell-mode` by adding `(flyspell-mode -1)` into the end of `~/.emacs.d/init.el`
Alternative-Ant-4741 · 2022-02-02 18:56
It's easy once you know :) The gnu tutorial and the reference book make no mention of this and I only figured it out once people on this thread told me. But there seems to be many possible ways, which is nice
holgerschurig · 2022-02-05 15:05
The most reliable way is to use dot ("full stop"), question and exclamation marks. That is the job of those signs, after all. And they are universally understood to have this function. For two spaces such a worldwide understood connotation don't exist. It's taste, nothing more.
andreyorst · 2022-02-05 21:22
full stop comes at a disadvantage of requiring additional steps to insert it, as it's not available by default on most (if not all) keyboard layouts. Tow spaces on the other hand doesn't. Not to mention that you'll have to juggle ordinary dot and full stop dot if you use a lot of abbreviations that use it, like in "Mr. Smith". Two spaces deal with both problems at the cost of extra press of the space key. On a side note, two spaces to end a sentence is a world-wide known practice, as it is a default shortcut on phone keyboards to insert a dot. It doesn't use two spaces, but I believe it is related.