Contents
Install the latest emacs on Ubuntu
- Add Kevin Kelley's PPA for the latest emacs stable releases
sudo add-apt-repository ppa:kelleyk/emacs
sudo apt update
- Then remove the existing emacs installation
sudo apt remove emacs emacs-common
- Then install the new emacs with native compilation
sudo apt install emacs28-nativecomp
Run emacs on Windows
- Emacs, when installed using the scoop package manager, doesn't receive a desktop or start menu shortcut
- Instead, to run emacs, bring up the Run menu with
Win + R
and type runemacs.exe
Movement:
- Up:
C-p
- Down:
C-n
- Forward:
C-f
- Back:
C-b
- Page down:
C-v
- Page up:
M-v
- End of file:
M-S->
- Beginning of file:
M-S-<
- Matching parenthesis:
C-M-n
/ C-M-p
Other common controls
- Copy:
M-w
- Cut:
C-w
- Paste:
C-y
- Delete:
C-d
- Save file:
C-x C-s
- Quit emacs:
C-x C-c
- Find:
C-s
(search)
- Find with regexp:
C-u C-s
- Replace:
M-x replace-string
- Replace regexp:
M-x replace-regexp
Repeat last command
C-x z
for first repeat, then z
to continue repeating
Set font
(set-face-attribute 'default nil :font "Cascadia Code PL-12")
- On Windows, we need to specify the usage of ClearType anti-aliasing:
(set-face-attribute 'default nil :font "Cascadia Code PL-12:antialias=natural)
(set-face-attribute 'fixed-pitch nil :family 'unspecified)
Set up the package manager
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
(add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t))
(package-initialize)
My Emacs packages
Better buffer navigation with ibuffer
(global-set-key (kbd "C-x C-b") 'ibuffer)
Disable the bell
(setq ring-bell-function 'ignore)
Smooth scrolling
(setq scroll-conservatively 9999)
Disable the startup message
(setq inhibit-startup-message t)
Set the mode of the scratch buffer
(setq initial-major-mode 'text-mode)
Set the scratch buffer message to blank
(setq initial-scratch-message "")
Disable scroll bars
Disable menu bar
Disable tool bar
Show column numbers
Enable display of parentheses
Set up the vscode-dark-plus
theme
(load-theme 'vscode-dark-plus t)
- Put this at the very end of
init.el
, after the autogenerated code
Projectile notes
Choose which browser emacs uses on a per-website basis
Enable auto-revert of files
(global-auto-revert-mode 1)
Spacemacs Notes
Switch to the emacs editing style
- Using the emacs editing style seems more performant and less subject to random lags than vim
- Instead of hitting
SPC SPC
to activate the spacemacs menu, hit M-m
Make spacemacs use fd
for projectile indexing:
Remove a project from Projectile
SPC SPC projectile-remove-known-project
Fix Spacemacs Markdown-mode
Remove current line highlight
- Add
(spacemacs\toggle-highlight-current-line-globally-off)
to dotspacemacs/user-config
Remove the prompt to install layers
- In
dotspacemacs/layers
, set dotspacemacs-enable-lazy-installation
to nil
Use spacemacs as the git editor
Markdown Mode
Fix Markdown-mode fixed pitch font
(set-face-attribute 'fixed-pitch nil :family 'unspecified)
Markdown toggle checkboxes
Markdown insert new list item
Configure Kotlin mode to use the kotlin language server
Associate markdown with text-mode
(add-to-list 'auto-mode-alist '("\\.md\\'" . text-mode))
Disable the fd
escape keyboard shortcut in Evil mode
- Add
evil-escape
to dotspacemacs-excluded-packages
Fix random hangs
Improve performance with long lines
- Enable
global-so-long-mode
with:
(global-so-long-mode 1)
in dotspacemacs/user-config
Spacemacs pomodoro
- Spacemacs' org-mode supports pomodoros
- To activate:
, C p
- However it requires you to use org-tasks, so not sure it's the right approach for me
Copy from terminal
- Select text
- MacOS:
M-| pbcopy
- Windows:
M-| clip.exe
- Note that, in a terminal,
meta
is option
on MacOS
Configure which browser org-mode uses when you select a link
Org-mode notes
Org change list type
Promote/demote heading/list item
- Promote:
M-S-left
- Demote:
M-S-right
Open link
Lists
- Insert list item:
M-RET
(in spacemacs, that's M-RET M-RET
)
- Insert to-do item:
M-S-RET
- Turn a list item into a todo:
C-u C-c C-c
- Toggle todo item:
C-c C-c
- Change list type:
C-c -
Disable automatic indentation
- To disable automatic indentation and stop org-mode from adjusting the indentation of child items when adjusting the level of headlines:
(setq org-adapt-indentation nil)
Disable blank lines between plain list items
Prevent org-mode from truncating
(setq org-startup-truncated nil)
Set everything to be visible by default
(setq org-startup-folded nil)
Code blocks
- Use
#+BEGIN_EXAMPLE
to format code instead of #+BEGIN_SRC
- Keyboard shortcut:
C-c C-, e
Convert to markdown
Org-markdown (ox-md) exporter issue
- Using
**
as the top level heading doesn't have the intended behavior in markdown export
- Just like the HTML exporter, the markdown exporter has the same behavior — treats
**
as the top level, and makes those #
headings
- Both HTML export and markdown export use
org-export-get-relative-level
to get the "relative" level of the headline when exporting
- This means that whatever level is highest, whether it be
*
, **
or whatever, is set as the highest header level in the exporter
- However, the HTML exporter has a variable
org-html-toplevel-hlevel
that sets "top level" headings to <h2>
, reserving <h1>
for use as the page title
- I need to implement a similar variable
org-md-toplevel-hlevel
, that sets a "top level" heading for markdown
- Building org-mode is problematic on Windows
- The best way to build is to push the code to a git repo and check it out under WSL
- Run
make all
- Run
make autoloads
- Copy the contents of the
lisp
directory to a directory in the home folder: C:/Users/quant/org-mode-build
- Add that directory to the load path:
(add-to-list 'load-path "C:/Users/quant/org-mode-build")
- Restart emacs
- Make sure that
(require 'ox-md)
is in init.el
, otherwise the markdown exporter won't load
Convert to HTML
- To convert to HTML:
C-c C-e h h
- To set the title property add
#+TITLE:
to the top of the file, and get rid of the top-level heading
- Keep using
**
as the primary heading level — the HTML exporter can deal with having the highest heading level in the file be a second-level heading
- This keeps things consistent between HTML and Markdown export
Convert to PMWiki
Insert structure template
- To insert a new structure template:
C-c C-,
Edit structure template
- To edit the contents of a template in a new window:
C-c '
Add checkboxes to a simple list
- On the first item of a list,
C-u C-c C-c
Don't fold subtrees when yanking
(setq org-yank-folded-subtrees nil)
Convert a plain list into a heading
Sort headings
- Select the range of headings that you want to sort, then hit
C-c ^
to bring up the sort dispatcher
Fix org-mode indentation
Better org-mode formatting
Escape formatting marks
- To escape formatting (for example, equals signs in code blocks), add a zero-width space before the character being escaped
- To add a zero-width space:
C-x 8 RET zero width space
Add a newline without indenting
Task Management
Building org-mode
- If
make autoloads
fails with an Org version mismatch
error, run make clean
Set the cursor color
(set-cursor-color "#<hex color>")
→ be sure to do this after the (load-theme)
call, otherwise the theme will overwrite your cursor color
Set the background color of highlighted text
(set-face-attribute 'region nil :background "#<hex color>")
Make Cascadia Code Italics work properly on Linux
- Make sure to install the OTF versions of Cascadia Code rather than the TTF versions
- Install the versions from the
static
directory
- Make sure you've installed the GTK version of emacs, not emacs-lucid
Disable backup and autosave files
(setq make-backup-files nil)
(setq auto-save-default nil)
(setq create-lockfiles nil)
Magit
- Install Magit with
M-x package-install RET magit RET
- Run magit with
C-x g
Insert spaces instead of tabs
Better Text editing
(add-hook 'text-mode-hook
'(lambda ()
(setq indent-tabs-mode nil)
(setq tab-width 4)
(setq indent-line-function 'insert-tab)
(electric-indent-mode -1)))
Windows
- Emacs calls its splits
windows
, for historical reasons
- What the rest of us call windows, Emacs calls
frames
- Split horizontally:
C-x 2
- Split vertically:
C-x 3
- Increase the height of a window:
C-x ^
- Increase the width of a window:
C-x }
- Equalize windows:
C-x +
- Close a window:
C-x 0
- Close all other windows:
C-x 1
Replace string inside hidden variables
- In org-mode, by default,
replace-string
will not replace strings inside org-mode links
- To alter this behavior, set the
search-invisible
variable to t
- To avoid matching hidden text, set
search-invisible
to nil
- The default value of this variable is
open
, which expands links, etc, when their content matches the search text
Set frame title to buffer title
(setq frame-title-format "%b")
Sensible buffer titles
- To make emacs prepend the folder name to the filename when opening multiple files with the same name:
(setq uniquify-buffer-name-style 'forward)
Disable C-z
Display date and time in the mode line
- Set the
display-time-format
variable to control how the time is displayed
(setq display-time-format "%l:%M %p %Y-%m-%d")
- To remove the load average, customize
display-time-string-forms
(setq display-time-string-forms
'((if
(and
(not display-time-format)
display-time-day-and-date)
(format-time-string "%a %b %e " now)
#1="")
(propertize
(format-time-string
(or display-time-format
(if display-time-24hr-format "%H:%M" "%-I:%M%p"))
now)
'help-echo
(format-time-string "%a %b %e, %Y" now))))
- Finally enable the time display with
(display-time-mode 1)
Prevent the transfer of text properties when yanking
- Set
yank-excluded-properties
to t
in order to have emacs strip all text properties when yanking
Make emacs work like other text editors when it comes to highlighted test
- Add
(delete-selection-mode 1)
to init.el
Delete all lines that match or don't match a regex
- To delete all lines that match a regex:
M-x delete-matching-lines
- To keep lines that match a regex and delete everything else:
M-x delete-non-matching-lines
Pretty-print XML
- To pretty print XML, we have to counterintuitively use
sgml-mode
M-x sgml-mode
C-x h
to select the entire buffer
M-x sgml-pretty-print
Calc
- To activate
M-x calc
- To move the trail pointer:
t p
→ moves the pointer up
t n
→ moves the pointer down
- To grab the value that the trail pointer is pointing to:
t y
- To change radix
d<radix>
d2
→ binary
d8
→ octal
d6
→ hexadecimal
d0
→ decimal
- To enter hex numbers use
#
(e.g. #2F
)
- To enter numbers in a different radix than the current one, do
<radix>#<number>
(e.g. 10#15
)
Make auto-fill-mode behave better
- By default auto-fill expects double spaces after sentence ends
- To make auto-fill-mode respect single spaces after sentence endings set
sentence-end-double-space
to nil
Indent to tab stop
- To indent to the next tab stop:
M-i
Force update of version control info for current buffer
Remove version control info from the mode line
(setq vc-handled-backends 'nil)
Save all files without prompting
Fringe settings
- To set fringe widths, call
(set-fringe-mode '(<left fringe width> . <right fringe width>))
- Fringe widths that work on my laptop:
(set-fringe-mode '(8 . 12))
- To disable fringes entirely and use
\
as a continuation marker: (set-fringe-mode '(0 . 0))
- To set fringe colors:
(set-face-attribute 'fringe nil :background "#000000" :foreground "#000000")
Find out which keys are bound to a command
Better promotion and demotion keyboard shortcuts
Paredit
Load a file
- To load an elisp file:
M-x load-file RET <filename>
Reduce GC pauses
Make emacs use UTF-8 by default
(prefer-coding-system 'utf-8)
Set the home directory correctly
Toggle a buffer as read-only
C-x C-q
- Useful when viewing log files
Lisp Evaluation
- Evaluate an Emacs Lisp expression and display it in the message buffer:
M-:
- Evaluate an Emacs Lisp expression and insert the result in the current buffer:
C-u M-:
List all lines matching a regular expression
URL Decode
(url-unhex-string "STRING")
Make dired kill buffers associated with deleted files
- Load dired-x
- Set
dired-clean-up-buffers-too
to non-nil
(with-eval-after-load 'dired
(require 'dired-x)
(setq dired-x-hands-off-my-keys nil)
(setq dired-clean-up-buffers t))
Emacs built-in version control
- To enable, undo
(setq vc-handled-backends nil)
- To open a project, use
C-x v d
to open the directory in vc-dir-mode
- To mark files
m
to mark an individual file
M
to mark all files with the same status as the current file
x
refresh the buffer and hide up-to-date and ignored files
q
to quit vc-dir
C-x v v
to commit
- After entering a commit message
C-c C-c
to confirm and commit the change
- Recommended workflow
- Use
vc
to open projects and check in files
- Use command line
git
for everything else
Emacs built-in project management
Adding and removing projects
C-x p p
— switch project or open new project
M-x forget-known-project
— remove a known project
Project commands operating on files
C-x p f
— visit file in the current project
C-x p g
— find matches for a regular expression in the current project
Project commands operating on buffers
C-x p b
— visit a buffer associated with the current project
C-x p k
— kill all buffers associated with the current project
Language Server
Minimal emacs config for new machines
(global-unset-key (kbd "C-z"))
(global-unset-key (kbd "C-x C-z"))
(prefer-coding-system 'utf-8)
(require 'pacakge)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
(add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t))
(package-initialize)
(setq frame-resize-pixelwise t)
(set-face-attribute 'default nil :font "Cascadia Code PL-12")
(set-face-attribute 'fixed-pitch nil :family 'unspecified)
(setq ring-bell-function 'ignore)
(setq scroll-conservatively 9999)
(setq inhibit-startup-message t)
(setq initial-major-mode 'text-mode)
(setq-default initial-scratch-message "")
(setq-default indent-tabs-mode nil)
(scroll-bar-mode -1)
(menu-bar-mode -1)
(tool-bar-mode -1)
(column-number-mode 1)
(show-paren-mode 1)
(delete-selection-mode 1)
(setq make-backup-files nil)
(setq auto-save-default nil)
(setq create-lockfiles nil)
(global-auto-revert-mode 1)
Magit error
error in process sentinel: magit-section-maybe-cache-visibility: Symbol's value as variable is void: alist-get
- Do a
M-x byte-force-recompile RET ~/.emacs.d/ RET
and restart emacs