Emacs cookbook

Core configuration

Exit prompt

In Emacs, the prompt “Really exit Emacs? (y or n)” is designed to prevent accidental exits, ensuring that you don’t lose unsaved work. However, if you find this prompt unnecessary and would like to bypass it, you can customize Doom Emacs to remove or alter this confirmation behavior.

To remove this prompt, you need to add a custom configuration to your Emacs config files. Specifically, you will override the default behavior of the save-buffers-kill-emacs function, which is what gets called when you attempt to exit Emacs. Add the following Lisp code to your config.el file:

(setq confirm-kill-emacs nil)

This line tells Emacs not to ask for confirmation when the kill-emacs command is invoked.

This change will disable the exit confirmation for all exit scenarios in Emacs. Be mindful that removing this prompt removes a layer of protection against accidental exits, so ensure this is the behaviour you want before applying the change.

If you ever decide to restore the exit confirmation prompt, simply remove or comment out the line you added ((setq confirm-kill-emacs nil)) from your config file and reload your configuration.

Packages

Load local packages

To load an Emacs package locally, you’ll need to do a few things. Here’s a step-by-step guide:

  1. Install the package manually: First, you’ll need to have the package files somewhere on your system. You may download it from a source or create it yourself. Let’s say you have a package named mypackage in a directory at ~/emacs/mypackage.

  2. Add the package directory to your load path: Emacs uses a variable called load-path to determine where to look for packages to load. You can add your package’s directory to this path by adding the following line to your Emacs configuration file (.emacs or init.el):

(add-to-list 'load-path "~/emacs/mypackage")

Note that Emacs does not expand the ~ character in paths, so you’ll need to use the full path to your package directory or use the expand-file-name function to expand the path.

  1. Require the package: Once the directory is in your load path, you can require the package in your Emacs configuration file:
(require 'mypackage)

This tells Emacs to load the mypackage package when it starts up.

Note: The package’s main file should be named mypackage.el, and there should be a (provide 'mypackage) expression at the end of this file for the require function to work.

This approach is for simple, single-file packages. More complex packages that have a structure and come with an -autoloads.el file will require a different approach. You might need to use the built-in package manager (package.el) or a third-party package manager like straight.el or use-package to handle autoloading and dependencies properly.

Ordering JSON by key value

Run json-pretty-print-buffer-ordered[^spacemacslayer]

[^spacemacslayer] If using spacemacs, it requires the Javascript layer

Projects

Creating a projectile project

To create a projectile project either use a valid Git repository or create the special .projectile file in root.

Do a SPC p f, but make sure the projectile project is created.

Split screen

The standard Emacs keys are valid here (even for Spacemacs).

  • SPC w h

Indent a block of text

Select the block with V and then press Ctrl-x TAB. Then use h or l to move it along