Example Emacs Lisp code.
How To Get Started With Emacs
So you're taking the plunge. You're learning Emacs.
First, the good news: you can be productive on the first day. Emacs (unlike vim) isn't something where you're helpless until you learn basic commands.
While you can get a leg up using this cheat sheet, you'll also be fine using the arrow keys until you learn the keystrokes.
On your first day, the keystrokes aren't something you need to ace.
Get used to moving around at your own pace. It's okay if you're a little slow.
You can still code productively even without total Emacs mastery.
The main thing you should concentrate on is syntax highlighting.
Syntax highlighting is basically coloring in the editor for your code files.
Open a file ending with one of those file extensions in Emacs, with (matching) code inside it. Is your text colored?
If it is, it may be that the language you're using already has it, or you're writing plain text and don't need it.
If you're not so lucky, you'll need to install the matching 'web mode' from the Emacs package repository.
Basically the process is:
- type m-x
- look for the mode you need
- type i to flag it for installation
- type x to install
You should become familiar with the m-x command, because you'll be using that a lot in the future, too.
In the beginning, a lot of your 'learning' of Emacs is going to center around the configuration file.
That's located at ~/.emacs.
If you want to install custom packages to enable custom functionality, go ahead. It's your life and your editor.
In the end, this is a highly individual decision that comes down to your personal workflow.
Coding in Emacs Lisp
I have some functions in my .emacs file that I wrote myself, and while I still use only a few, the learning process helped me to learn Lisp. And knowing another computer language is always a useful skill.
I used one of those Emacs functions when writing this blog post: a function that takes sentences in plain English and wraps them in paragraph tags when I select a region and activate it.
The thing about Lisp, including Emacs Lisp, is: everything is enclosed in parenthesis. You'll get used to it. Try following visually where the opening and closing parenthesis are, if you're confused.
Storing Abbreviations in the Abbrev Defs File
There's also a file called abbrev_defs, where you put abbreviation definitions that Emacs can automatically expand.
Example: I preface my commands with an 8. This is a convention I use for my abbreviated commands.
One of the commands I use the most looks 8eu, which expands to "cd ~/Example/Update."
I can type 8eu press return in the command line to put me in that frequently used directory.
I use that command in the shell, which brings me to my next topic.
Using The Shell in Emacs
Definition of the Shell
Emacs has 2 shells: the regular shell and Emacs shell.
These are basically terminals - where you type commands like cp (for copy) and ls (for list files).
The shell in Emacs is a 'gift' I encourage everyone to use.
The ability to move around, without limitations, in the shell, and scroll up and down, and even copy commands from the shell, is something I use all the time.
There's the regular shell, for things that require the regular shell - say, some commands that won't work anywhere else.
For everything else there's the Emacs shell.
Doing Math in the Shell
You can even do math in the Emacs shell with prefix notation.
Prefix notation is when you put the operator first, then the operation, in parenthesis (remember, Emacs is written in Lisp).
So, in Emacs shell, to multiply a few numbers, you could type:
(* 3 2 6)
Then press return to see the result - 36.
You can also, in a file that is open in Emacs, type control-x control-e at the end of a line with a command that's valid in Emacs Lisp, to see the result of that command.
If I'm working on my finances - what's 3.234 times 5? - I can write it out and spit out the answer in the same line - (* 3.234 5)=16.17.
This is another powerful feature of Emacs that you'll come to appreciate in time.
There are a variety of special modes you can use in Emacs, that are good for gettting specialized work done.
I'll focus on a famous one within the Emacs community: org mode.
I actually use the most simple, basic version of org mode: an asterisk to make a line a section heading, and another asterisk to start a sub-heading under that heading.
Keep adding asterisks to keep adding sub-sub-layers (as many as you want).
Think of each asterisk you insert in org-mode as a 'level'. An asterisk creates a level. Another asterisk beside it creates a sub-level. Sub-levels can have sub-sub-levels, unto infinity.
Press tab to expand a level, and tag again to contract it.
Just use the file extension .org and Emacs will know what to do with your file, and import org-mode's functionality.
Customizing Emacs with Emacs Lisp
For Emacs Lisp, if you want to go down the road of customization, search for code samples online.
You'll find a lot, though in my experience, a lot of functionality already exists in pre-written functions, and much of the work is really just learning how to string those together.
It took me about a year to learn Emacs. After that, I could do almost anything a computer language can do with Emacs.
It's easy to write custom functions, if for example you want to insert mildly customized JS and TypeScript snippets.
It's also useful for org mode, whenever you need to take notes.
With that being said, there are times when it will be easier to do something outside of Emacs.
You might want to transform a file using a language you know better (like Python), and just use the Emacs shell to run the command. I've done that.
But if you want Emacs to do something for you, Emacs has a way.
And by the time you've been using Emacs as long as I have (5+ years), you may never have to switch editors again.