In this post, I will be discussing how to set up emacs.

The best editor is neither Emacs nor Vim, it’s Emacs and Vim! - Spacemacs

Installation

Refer GNU Emacs for updated installation details.

MacOS

brew install --cask emacs

Note: By default all folders will not be accessible through emacs, you need to allow ruby to Restore Access to File System for Emacs on MacOS

Ubuntu

Installing latest emacs using flatpak

sudo apt install flatpak
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
flatpak install flathub org.gnu.emacs

Note: With flatpak version, emacsclient is not working.

Windows

Download binary file from here

Default KeyBindings

Please note that emacs key bindings are used by default in all shells. Refer Important shortcuts in Bash/Zsh

Dealing with files

Below is how you can open, save and exit the file

KeyBindings Action
C-x C-f To find the file to load into buffer
C-x C-s To save buffer to file
C-x s To save some buffers
C-c C-x To exit emacs

Extended commands

Most of the extended commands are followed by below prefix keys

  • C-x followed by additional keys
  • C-c followed by additional keys
  • M-x followed by a long command

Buffer Navigation

Below is how you can navigate a buffer once the file is loaded.

KeyBindings Action
C-a Start of the line
C-e End of the line
C-f Forward one cursor
C-b Backward one cursor
C-p Previous line
C-n Next line
M-f Forward one word
M-b Backward one word
C-v Forward one page
M-v Backward one page
M-a Forward one sentence
M-b Backward one sentence
C-g Kill current command
C-u n To repeat command n times
M-n To repeat command n times
C-l Move the current line to the top-middle-bottom of the buffer.
C-u 0 C-l To move the current line to the top of the buffer

Undo

C-_ to undo
C-x u to undo
C-z to undo

KeyBindings Action
C-s Increment search forward
C-r Increment search backward
C-M-s regex increment search forward
C-M-r regex increment search backward

Replace

KeyBindings Action
M-x replace-string RET xyz RET abc RET To replace xyz with abc
M-x query-replace RET xyz RET abc RET To replace xyz with abc
M-% To replace same as M-x query-replace

Below are the options that are given for replacement confirmation

<SPACE> to replace
<DEL> to skip
! to replace others 

Cut, Copy & Paste

Below is how to cut, copy and paste is done in a file.

KeyBindings Action
C-k Cut from current cursor to end of the line (no newline)
M-k Cut from current cursor to end of sentence
C-d To delete next character
To delete previous character
M-d To delete next word
M- To delete previous words
C-y Paste
C-@ or C-Space Mark set
C-w Cut from the mark set to cursor
M-w Copy from the mark set to cursor
C-a C-Space C-n M-w To copy whole line (including newline)
C-k C-k To cut the whole line (including newline)

Buffers

Below is how we can switch between buffers.

KeyBindings Action
C-x k To kill current buffer
C-x Go to next buffer
C-x Go to previous buffer
M-< Move to the beginning of the buffer
M-> Move to end of the buffer
C-< Select from the current cursor to beginning of the buffer
C-> Select from current cursor to end of the buffer
C-x b To switch to that buffer
C-x C-b To list all buffers
C-x h Select everything in buffer

Changing case

KeyBindings Action
M-u To make upper case
M-l To make lower case
C-x C-u To make the region to upper case
C-x C-l To make the region to lower case

Windows

KeyBindings Action
C-x 1 Kill all other windows
C-x 2 To split horizontally
C-x 3 To split vertically
C-x 4 C-f To find a file and open in another window
C-x 5 C-f To open a file in separate window
M-x Make-frame Same as above
C-x o To switch between windows
C-M-v To scroll the other window

Modes

M-x <mode-name> To change mode
For example: To set the line width to 40, M-x auto-fill-mode to change to auto-fill mode, and C-u 40 C-x f to change the line width to 40

Column Editing

  • M-x cua-mode to enable column editing (toggles)
  • C-RET to start the selection (toggles)
  • Once a mark is set, you can traverse using C-n/p/f/b/a/e to select a region
  • After the selection is done
  • C-x to cut the selected region
  • C-c or M-w to copy the region
  • C-y or C-v to paste the region
  • Start typing having mouse at the start of the region to insert at the start of the region
  • Same goes for end of the region
  • Ret to move around the corners of the region.

Using custom keys

(global-set-key "\Key-Command" 'command)

Example : (global-set-key "\C-xr" 'replace-string)

Indenting XML

To indent xml you can follow steps

  • M-x nxml-mode
  • M-x replace-regexp RET > *< RET >C-q C-j< RET
  • C-M-\ to indent

Macros

KeyBindings Action
C-x ( To start recording macro
C-x ) To end recording macro
C-x e To execute the last macro
M-x name-last-kbd-macro a To name the macro as “a”
M-x a To execute macro “a”
M-x insert-kbd-macro “a” Paste the macro a to file so that you can save and use that macro for future

Configuration

If you want pre-configured emacs, please refer Spacemacs or Doom Emacs.

However, if you want to configure from scratch, follow the below steps. Initial configuration file is found at ~/.emacs.d/init.el (if not you can create one). Most of these configurations are taken from config.daviwil.com. You can add configuration in .org file and load that file in init.el like below

;;; package --- Emacs configuration
;;; Commentary:
;;; Emacs custom configuration
;;; Code:
(org-babel-load-file "~/.emacs.d/config.org")
(provide 'init)
;;; init.el ends here

Create ~/.emacs.d/config.org file. Here is where you add most of the configuration. Whenever you want to evaluate a buffer use M-x eval-buffer to run the lisp in current buffer. You can also use C-x C-e to evaluate list exp (make sure you are at the end of the lisp code.) You can also use C-h to describe commands.

You can refer to my config.org or config.daviwil.com. I suggest you go through each heading and pick what configuration is needed for your use case.

Below is how my emacs looks like in my Mac, Windows and Ubuntu with above configuration.

Emacs on Mac
Emacs on Mac
Emacs on Linux

Fonts

Regarding fonts, below is the setting which sets the font. Based on the platform, you can use the below steps to install fonts.

(pcase system-type
  ((or 'gnu/linux 'windows-nt 'cygwin)
   (set-face-attribute 'default nil
                       :font "JetBrains Mono"
                       :weight 'light
                       :height 150))
  ('darwin (set-face-attribute 'default nil :font "Fira Mono" :height 170)))

MacOS

brew tap  homebrew/cask-fonts
brew install --cask \
  font-fira-code \
  font-fira-mono \
  font-fira-mono-for-powerline \
  font-fira-sans

Linux

sudo mv JetBrainsMono-*.ttf /usr/share/fonts/
sudo mv FiraMone-*.ttf /usr/share/fonts/

Windows

Download fonts from ryanoasis nert fonts and copy ttf files to C:\Windows\Fonts

System wide emacs bindings

On macOS, you can use emacs bindings system bindings by having below content in ~/Library/KeyBindings/DefaultKeyBinding.dict

{
    "~f"      = "moveWordForward:";              /* M-f */
    "~b"      = "moveWordBackward:";             /* M-b */
    "^a"      = "moveToBeginningOfLine:";        /* C-a */
    "^e"      = "moveToEndOfLine:";              /* C-e */
    "~d"      = "deleteWordForward:";            /* M-d */
    "^w"      = "deleteWordBackward:";           /* C-w */
    "~<"      = "moveToBeginningOfDocument:";    /* M-< */
    "~>"      = "moveToEndOfDocument:";          /* M-> */
    "~w"      = "copy:";                         /* M-w */
    "^y"      = "paste:";                        /* C-y */
}

Conclusion

By following the above steps, I think you got an idea of navigating and configuring emacs. Please check the official documenation to familiarize yourself with emacs and emacs lisp. – RC