Emacs
Using Emacs Quickstart and Guide on Mac OS
Install Emacs by downloading it and placing in the Application folder. You will need to give Emacs disk access in Security settings and run
$ cd /Applications/Emacs.app/Contents/MacOS
$ mv Emacs Emacs.old
$ ln -s Emacs-x86_64-10_14 Emacs
per comment on github.
This guide is meant as a quick-start to skim from start to finish. Using Emacs requires an intent. There is no desktop prompting me to open a browser or organize files. Before doing an action I need to decide what that action is. Maybe once I develop a muscle memory in Emacs I will end up wasting just as much time as I do in a desktop environment. If that happens I will update this text.
Emacs uses the CTRL, written as C, and ALT, written as M, keys to perform keyboard commands. On macOS you can use the mouse to access the menu, scrolling and selecting text.
Emacs screen has 3 areas in a frame, think a frame to a painting. In the frame is your work area (the buffer in a window, the painting), the mode line (title of painting), and the minibuffer at the bottom (helpful text description). The minbuffer shows you the commands you are typing when you are slow. The mode line has the file name, your position in the file as a percent and line number, and whether not the file has been saved, indicated with ** . Finally the mode line says which major mode you are in. The mode changes what some commands do. For example in Text mode ’’ are treated as characters not separators.
In the command lists below:
- C stands for the CTRL key
- M stands for the ALT key and reads as Meta key. Meta keys are used generally for language operations such as words, sentence and paragraphs.
- CMD stands for the command key, emacs interprets the CMD key as modifier ’s’. MacOS shortcuts carry over into emacs such CMD-p and CMD-v for example.
- S stands for the SHIFT key
- “-” press together
- SPC stand for Spacebar
- LEFT stands for Left arrow key
- RIGHT stands for right arrow key
In the command list you will often see C-x and M-x
- C-x : stands for Character eXtend. This means you prep emacs to receive a command with the next character you input. Used for less commonly used commands.
- M-x : Named command eXtend, means you’ve prepped emac to receive a text version of the command
Good to know from the start
- C-x C-c : Exit Emacs
- C-g : Cancels your current command entry
- M-x term : Opens a terminal buffer and window
- C-/ : Undo previous text manipulation. ‘C-x u’ also works
- C-g C-/ : Redo, as in cancel your undo
- C-h CHARACTER : Opens help window on command CHARACTER. Use C-x 1 to close it
Window splitting
A window shows a buffer, your painting in the frame. You can have the same buffer in two windows or different buffers in two different windows. You can have many buffers open at the same time under each other. Open a buffer in a window to see it.
-
C-x 3 : Split the window vertically, showing the same buffer
-
C-x 2 : Split the window horizontally, showing the same buffer
-
C-x 4 C-f FILE : Open FILE in a new window
-
C-x o : Move to the (o)ther window
-
C-x 0 : Close current window
-
C-M v : Scroll down in the other window
-
C-S-M v : Scroll up in the other window
-
C-x 1 : One window, close all other windows than the one you have selected
-
CMD-n : Open current buffer in new (frame) macOS window also can be done with C-x 5 2
-
C-x 4 b : Create new frame with buffer from but list
-
C-x C-b : List open buffers
-
C-x LEFT : Bring previous buffer to front (unless winner mode is enabled)
-
C-x RIGHT: Bring next buffer to front (unless winner mode is enabled)
-
C-x b : Type name of the buffer you want the current window to open. You can also type a name to create a new buffer with that name
For example you can open the buffer list C-x C-b in a new window, then use C-x o to move to the other window, and then press return to open that buffer in the new window. Then use C-x 1 to close all other windows.
The buffer with Name are from Emacs. Messages lists all the messages that appear in mini-buffer
Navigation
Cursor based
- C-a : Move to beginning of line
- C-e : Move to end of line
- C-p : Move to previous line
- C-n : Move to next line
- C-f : Move forward one character
- C-b : Move back one character
- M-f : Move forward one word
- M-b : Move back one word
- M-< : Start of file
- M-> : End of file
- M-} : Move to next paragraph start
- M-{ : Move to previous paragraph
- M-e : Move to end paragraph
- M-LEFT : Start of brace requires addition to .emacs
- M-RIGHT : Ending brace requires addition to .emacs
- M-v : Page up
- C-v : Page down
- CMD-l NUMBER : Jump to line NUMBER
Page based
- C-l : Move page down , (not cursor), so cursor is at center, top, bottom
File/Buffer Manipulation
File manipulation needs you to find a file, they call it visiting a file, in order to make changes to it. You can visit non existing files to create them. When you save a file it will be placed in your home directory ~/ allow to input a name or path in the mini-buffer. If you want to go to the file from root use //. You can open files on remote servers with user@host:path-to-file
- C-x C-f : Open an existing file, find a file in Emacs parlance.
- C-x C-s : Save current buffer to file that it was opened from
- C-x C-w : Save current buffer to new file
- C-x k : Kill current buffer
Text Manipulation
- C-k : (K)ill text on rest of line. Killing text is the equivalent to cutting text. It can be pasted back using yanking
- C-w : Cut text, kill in Emacs parlance
- M-k : (K)ill text to rest of sentence
- C-y : Yank (paste) killed text.
- M-y : Yank (paste) previously killed text, repeat to go further back
- Backspace : Called <DEL> deletes the character before the point.
- DELETE or c-d : to delete character in front of point.
- C-/ : Undo typing
- C-SPC : Set a mark. Selected text is between the mark and the point. C-x C-x swaps the point and the mark
- C-o : Insert return character
- M-c : Capitalize the word
- M-l : Lowercase the word
- M-w : kill-ring-save which I think is the same as a copy command. “Save the region as if killed but don’t kill it”
Make line warp on words by enabling (Visual Line Mode)[https://www.gnu.org/software/emacs/manual/html_node/emacs/Visual-Line-Mode.html]
- M-x visual-line-mode
You can make visual-line-mode at startup by changing ~/.emacs with following
(global-visual-line-mode t)
Other
- M-s w : Search for a word in the selected window
- C-u NUMBER C-n : NUMBER acts as a multiple on the next line command. Also works on other command
- C-h k C-f : Opens explantation of C-f in new window, but can’t explain how it works yet
- C-u NUMBER C-v/M-v : Do not work as expected should be page jump NUMBER of pages, but istead they jump that number of lines
- TAB : opens autocomplete menu
You may end up in Recursive Editing Levels, indicated with [] around the mode name. Press ESC three times to get out of it.
General Patterns
- C-x commands have to do with windows, files, buffers and related things. They can be up to four characters long
- M-x commands have to do with language, such as words, sentences and paragraphs
- C-h commands are for help.
Self Learning
- C-h c COMMAND : Gives the COMMAND name indicating what it does
- C-h k COMMAND : Gives a longer description of what a command does
- C-h f FUNCTION NAME : Describe what FUNCTION NAME does.
- C-h r : Open full Emacs manual
- C-h i : Info about the packages installed
Advanced usage
Once you have the basics it is time to configure Emacs to make it the best place to get work done
Add extras or enable options
Extra features in Emacs are called packages. You can see the list of currently install packages with
M-x list-packages : List the available packages
Some of the packages I use are in the Melpa archive which can be installed with:
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
;; Comment/uncomment this line to enable MELPA Stable if desired. See `package-archive-priorities`
;; and `package-pinned-packages`. Most users will not need or want to do this.
;;(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t)
(package-initialize)
see full melpa install instructions (here)[https://melpa.org/#/getting-started]
Winner Mode Option
Winner mode can be enabled since Emacs version 20. Winner Mode allows you to undo window configuration changes.
- C-x LEFT : Return to pervious windows configuration
- C-x RIGHT : Go to next windows windows configuration.
Read more including how to enable by default on the wiki
Enable terminal commands
M-x term : is a full terminal from emacs. M-x shell : enables the shell for basic commands such as ls and cat. For more sophisticated commands use term.
In order for your $PATH to be correct there are several steps. First install the exec-path-from-shell package from melpa.
Then You will need to update the .emacs file with
(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize))
see (github)[https://github.com/purcell/exec-path-from-shell] for details
Finally make sure that your PATH variables are set in .zshenv instead of .zshrc.
Useful Tips and Tricks
Buffer mode, major or add a minor.
C-h m : list major mode commands
You can associate a mode to load when opening a specific file type with the auto-mode-alist
variable.
Markdown preview mode
Setup Melpa and then install the ‘markdown-preview-mode’ package
C-c C-t 1 : Insert Header 1