Today I learned, Emacs version. More of a mini blog. Inspired by jbranchaud/til.
svg screenshots
Starting version 27, Emacs let's you take svg screenshots of itself.
signals
You can bind
SIGUSR1
andSIGUSR2
to custom functions.SIGUSR2
is also tied todebug-on-event
that makes it pretty handy.org repeaters
There are more types of repeaters for tasks in org mode than just plain
+1d
style. Check here.calling convention docstring
At times, you want to specify a different calling function signature to show up when inspecting a function. This is useful in macros (as the doc says here) and dynamic module definitions (in C for example) where you don't have a way of specifying lambda list properly. For doing this, you need to extend docstrings as:
The last line of the documentation string can specify calling conventions different from the actual function arguments. Write text like this:
\(fn arglist)
following a blank line, at the beginning of the line, with no newline following it inside the documentation string. (The ‘\’ is used to avoid confusing the Emacs motion commands.) The calling convention specified in this way appears in help messages in place of the one derived from the actual arguments of the function.
docstring keys
When you want up to date keymaps to be shown in docstrings, you can use special sequences as specified here which are substituted depending on current bindings.
ede
EDE tries to add IDE like project level abstractions and actions to Emacs. Going through the README makes me feel as if I am not missing anything substantial by not using it. Probably there are other piecemeal tools which make up for everything EDE tries to support (similar to CEDET).
One reason I might be wrong in estimating its usefulness is that I personally haven't tried using a lot of IDEish features like debugging etc. in Emacs itself.
org-edna
Edna allows adding dependencies in org mode headings. The doc has good examples. I have tried it a little but not sure if I will be using it regularly.
Mouse tracking
track-mouse
is a neat macro for things that depend on mouse movements. Probably artist mode uses it. I got to know about this from the awesome epaint program.abnormal hooks
There is a naming convention for variables holding hook functions. One such is abnormal hooks which I never thought of as something with specific meaning. The doc explains.
eval-defun
While working with elisp, resetting
defcustom
like variables usingeval-last-sexp
doesn't work. I used to write a quicksetq
to do these things but found that I could useeval-defun
with the same setq effect. Check this answer.org radio-targets
Another hidden gem from Cheong Yiufung's series (post here).
If you want to make a particular phrase link to someplace, for example a definition, without adding a lot of manual links, you can put that word within triple
<<<word>>>
angles along with the definition. All other presences will become links pointing to this place. These links are also there in exports which is really nice.link abbreviations
A much neater solution for creating custom org links if you only want a fixed string based expansion (exports also work as you would expect). Setting
org-link-abbrev-alist
let's you write a shorthand link like[[yt:dQw4w9WgXcQ][some-nice-video]]
which can act like a fuller link.There is a more convenient and local shorthand for this that works by putting something like
#+LINK: yt https://www.youtube.com/watch?v=
in the buffer.Taken from here.
org-catch-invisible-edits
At times your cursor ends up adding unwanted edits in folded org section. This variable has solutions. Got to know from the post Org-mode Hidden Gems - 01 Document Structure.
A very interesting thing about this particular setting is that I wasn't expecting a solution for this issue, even though I had been facing this for a long time.
emacs regex case
By default elisp regex match functions are case insensitive. Setting
case-fold-search
to nil changes the behavior. This is a til mostly because I find this behavior very surprising. Probably more surprising is that nothing bad happened, making me think that I probably don't need case sensitivity that much.systemd unit
Starting from v 26.1, you can use Emacs daemon as a systemd unit. See discussions here.
future history
Check this out.
drag and drop
Not sure if I have ever used this feature in any text editor, but here it is.
registers
Registers are one of the little known features of Emacs. They essentially act like variables which are made quick to access by mapping single chars to them. Also their default impermanence across sessions probably makes them a little different from other stores.
Similar to unsaved keyboard macros, thees can probably be useful for temporary, single session tasks. For example the text registers for working with temporary templates, the number register for counting things etc.
enriched-mode
Emacs has a tiny markup language that keeps things like face info along with text to show rich text in buffers.
directory-local vars
There is a way to set variables for a directory. Not sure if I will be wanting to use this but since it also allows
eval
, there is a lot of potential.profiling
Along with
benchmark-run
, the emacs profiler is a handy way to check part of your lisp code is eating up the major time.dbus
Emacs has proper dbus support (check the manual). Someone made a notification daemon on it. Also this answer is helpful if you are starting.
progress bar
There is a progress reporter. A unicode version is here.
charts
Emacs comes with a small and decent charting library. See this article for an introduction.
edebug
I am trying to use debuggers more than prints. In the process found
edebug
for emacs lisp, which is really easy to use and works nicely on the source code level.macro indentation
Custom macros show up really badly when indented in elisp code. I didn't try to search for a solution earlier but we can allow arbitrary indentation in macros. Adding
(declare (indent defun))
works for me in most of the cases.buffer local hooks
add-hook
takes an argumentlocal
to set buffer local hooks. This is really useful in cases like:(add-hook 'after-save-hook #'org-babel-tangle nil t) ; Last t is for local
This can probably be plugged in eval section at the top of files, though I am not sure if thats the right way.
inline code blocks
Inline executable code blocks can be written in org mode as:
src_LANG[HEADERS]{CODE}
A snippet like the following then shows up like this
(+ 1 1)
2
:src_emacs-lisp[:exports both :results value]{(+ 1 1)} {{{results(=2=)}}}
dump-emacs
Similar to most Common Lisp implementations, emacs also provides a dump function which dumps the state. Although this is mostly for development purpose and not really useful. See this thread.
magic file names
You can define specific handlers for certain file name patterns which can then override the behavior in interesting ways. openwith exploits this by associating external programs with specific file formats. This capability might allow creating hacks like ytfs in elisp. Documentation is here.
reader macros
Unlike common lisp, the reader for Elisp is not open for extension (in elisp that is; of course you can modify the C sources). Here is a custom reader reader in elisp which lets you define custom reader macros.
org macros
Org mode allows simple text replacement macros with arguments for export. See more here.
benchmark-run
Just what you expect. Learnt from here. Also check
benchmark-run-compiled
if you want to compare compiled elisp performance.calc
Emacs has a very elaborate calculator capable even of symbolic mathematics and manipulation. Check this blog post by Chris to get a feel. I used to call insect to perform unit conversions, but looks like calc is enough.
tail calls
Emacs lisp doesn't optimize tail calls. This forces you to use loops (like
while
) instead of recursion. Two workarounds are tco and recur.two columns
Two column editing lets you split the current buffer in two columns (obviously) which are finally merged in one. I am trying to use it for writing sidenotes in org files. Will have issues with org-export though.
gap buffers
Emacs internally uses gap buffers to represent text (in buffers). Current gap values can be accessed using functions
gap-position
andgap-size
. See the page on manual for more.reverse variable search
Weird but possibly useful.
apropos-value
lets you search for variables with provided value.literate configuration
Using
org-babel-load-file
, you can load emacs-lisp snippets from an org file. This is great for maintaining readable configuration like done in emacs24-starter-kit.image tooltips
Its possible to show image inside the regular emacs tooltips. This is handy for documents with linked images.
capturing buffer bookmarks
Org capture has templates (
%a
,%A
etc.) for capturing the location where the org-capture was called. This comes in handy with emails, todos in code etc.narrow region
Narrowing allows showing only a certain part of a buffer instead of full content. This lets you work on the narrowed region without affecting other stuff.
org-edit-special
Special chunks in org mode like babel source blocks can be edited in a separate dedicated buffer using
org-edit-special
.rectangle insert
Iedit has a
iedit-rectangle-mode
which lets you edit vertical chunks. Handy for enforcing indentation over a section.undo-tree
undo-tree maintains a tree instead of a linear undo/redo history. Wish there was something similar for krita/gimp.
org-store-link
Special bookmarks to different modes can be saved in org files using
org-store-link
andorg-insert-link
.common lisp emulation
Elisp can simulate many features of common lisp, including full fledged function arguments, using the
cl
package.winner-mode
Probably too late to know about it. Winner mode lets you undo/redo window configuration.
file-watch
file-notify-add-watch
allows hooking up callbacks for file changes.re-builder
Emacs lets you check regex on the fly in current buffer using
re-builder
.obarray
obarray
refers to a vector of symbols which are interned and is used for looking up value of symbols.latex fragment preview
Latex code snippets can be previewed directly in org-mode using
org-toggle-latex-fragment
.dired drag and drop
Dropping a file to a dired buffer copies stuff to it.
advising functions
Elisp lets you add a custom behavior on an already existing function using the advising facility.
highlight-regexp
Highlight a regexp in buffer with certain color using
highlight-regexp
.eieio
Emacs Lisp has an Object Oriented Programming system called EIEIO mimicking Common Lisp Object System.
org-protocol
org-protocol intercepts calls from emacsclient to trigger custom actions without external dependencies. Only one protocol has to be configured with your external applications or the operating system, to trigger an arbitrary number of custom actions. Just register your custom sub-protocol and handler with the variable `org-protocol-protocol-alist'.
Taken from here. Probably the best surprise feature I found till date.
comint mode scroll
Comint mode (all the shell-ish processes), by default (in spacemacs at least), shows everything possible whenever a shell output comes out. This results in pushing (scrolling) the current line to the bottom even when you were up somewhere. This can be fixed by setting
comint-scroll-show-maximum-output
tonil
.custom agenda views
Org agenda view allows custom built views by customizing the variable
org-agenda-custom-commands
. The customization lets you work with separate files, tags, todo-states and much more.current-prefix-arg
Setting
current-prefix-arg
variable works asC-u
key for the command in scope. This allows wrapping up some useful commands with prefix arguments.apropos
Apropos commands (
apropos
,apropos-command
etc.) do full text searches over corresponding components (symbols, commands, etc.) and are powerful and useful than the regular ways.caching sudo password in eshell
Eshell can use tramp's sudo instead of
/usr/bin/sudo
and then cache passwords. Setting alias byalias sudo 'eshell/sudo $*'
in eshell after setting up cache does the trick.(use-package em-tramp :config (setq eshell-prefer-lisp-functions t) (setq password-cache t) (setq password-cache-expiry 3600))
syntax table
Emacs maintains what are called syntax table for modes which define which character is going to be treated as what. As an example, you could set
$
as a whitespace character to assist jumping around in text by using(modify-syntax-entry ?$ " ")
ditaa & dot
Babel has great support for ditaa and dot snippets, allowing creation of graph, drawings and flowcharts easily.
lexical scoping
Adding
;;; -*- lexical-binding: t -*-
to the header enables lexical binding in an elisp source file.inserting unicode by name
C-x 8 RET
lets you search and insert unicode character by Unicode name.getting mouse position
There are functions to get current position of mouse both in terms of pixels
mouse-pixel-position
and charactersmouse-position
relative to current frame (or absolutemouse-absolute-pixel-position
).smerge
smerge-mode
allows easy conflict resolution for merges. Move pointer to the conflict and usesmerge-keep-mine
/smerge-keep-other
.edit file properties in dired
Dired allows a special mode, wdired (
C-x C-q
), that lets dired buffer editings – like renaming files, changing permissions, etc. – get reflected to the file system. More details here.external functions in org tables
Any babel code block returning value can be referenced by its
name
and called as a function for org tables. More details here.form feed lines
Many popular elisp source files get rendered in spacemacs with sections separated by neat horizontal lines. TIL those are form feed characters
^L
and are rendered using packages like page-break-lines.kill-ring
Recently stopped using CUA keybindings. This opened me up to use the kill-ring, which is a list with kill (cut, copy) history. Simple cut, copy, paste are
C-w
(kill),M-w
(kill-save) andC-y
(yank). Cycle through the ring while yanking usingM-y
. Usehelm-show-kill-ring
for a better kill-ring browsing experience.elisp scripts
Using Emacs for general purpose scripting has multiple issues. Many are documented here. For me, package loading is a major one. A lot of not-inside-emacs scripts wont be helpful without packages like f, dash and s. cask provides a way out. Init a caskfile (
cask init
), install dependencies (cask install
), run (cask emacs --script something.el
).data structures
I never went beyond list. Knew about alists, but didn't know there are hash tables and vectors too. In case you are working with key-value pairs in either hash table or alist form, try ht.el.
org-contacts
You can use org-contacts from org-contrib to manage contacts. This is really neat considering it integrates with gnus.
mpc
There is a pretty nifty mpd client built into Emacs. Use
M-x mpc
.