This page is an introductory tutorial for Emacs.
Getting started with Emacs in 15 minutes.
Introduction
Emacs is an editor that is very easy to get started with: it can be used with only a few short lines of configuration, but becoming highly proficient at using Emacs for various tasks still takes some time.
As an introductory tutorial, this page only introduces Emacs's basic functions and convenient ways to write and debug code with Emacs.
Getting started
Commands
Commands play a major role in Emacs.
The Application key1 (Emacs does not assign this key by default on Windows, so it must be set manually) or the M-x shortcut (Alt+x) can open command input; press Enter after entering a command to execute it.
The es or eshell command is usually used to open Eshell (which is similar to a terminal).
Commands can usually be replaced by keyboard shortcuts.
Buffer
A buffer is an opened file or process. Unless it is saved, modifying a buffer does not modify the file.
Click the buffer's name at the bottom of the buffer or use a keyboard shortcut to switch buffers.
Compilation, debugging, and execution
The entry points for compilation and debugging are in the Tools drop-down menu in the top menu bar. Users can also use commands or custom keyboard shortcuts for compilation and debugging.
Programs can be run using a terminal or Eshell.
After selecting debugging (gud-gdb) in Tools, enter the program name to start debugging (it is usually filled in automatically, but if the program was saved under another name midway or two programs requiring debugging were opened, the automatically filled-in filename may be incorrect).
Splitting windows
This feature lets users view the contents of multiple buffers at the same time without switching back and forth, making it convenient to test and debug code.
Window splitting can display multiple windows at the same time; drag a window's edge with the mouse to resize it.
Several shortcuts:
- Delete a split "C-x 0": delete this split
- Split horizontally "C-x 3": split this window horizontally into two halves
- Split vertically "C-x 2": split this window vertically into two halves
A recommended window layout divides the window into four parts: first split it horizontally, adjust one part to about 3/4 of the screen width, and use it as the editing window. Split the other part horizontally, using one part to display debugging and compilation information; then split the other part vertically, using one part to open the input file and the other to open the output file.

Keyboard shortcuts
Emacs has an extremely rich set of keyboard shortcuts that can greatly improve work efficiency. Users can customize shortcuts or set shortcut mappings in the configuration.
Because there are so many shortcuts, the use of Emacs shortcuts differs from that of the operating system.
For convenience, use the following conventions:
| Character | Key |
|---|---|
| C | Ctrl |
| M | Alt |
| ? | Any key |
There are generally three types:
F?,ESC: press the corresponding function key directly.M-?,C-?,C-M-?: press?while holding Alt or Ctrl.? ?: press the key represented by the first?, release it, and then press the key represented by the second?.
Here are some commonly used shortcuts:
C-x h: select allC-x left,C-x right: switch to the previous/next bufferC-x d: open a directoryC-x C-f: open a file (create it if it does not exist)
Personalization
A freshly installed Emacs looks unattractive and is inconvenient to use, so it needs to be personalized.
Because configuration is difficult to remember, it is recommended not to memorize settings that can be configured directly.
Direct settings
- Options:
Highlight Matching Parentheseshighlight matching parentheses - Options:
Blink Cursorconfigure cursor blinking - Options Show/Hide:
Tool Barshow/hide the toolbar (shown by default; hiding it is recommended) - Options:
Use CUA Keysafter checking this option, use shortcuts such as Ctrl + C and Ctrl + V to copy and paste - Options Customize-Emacs:
Custom Themeselect a color scheme; click save after making the selection - Options:
Save Optionssave the configuration
Configuration
Show hidden files in the home directory (on Windows, in the user directory's AppData\Roaming directory). ".emacs" is the configuration file (if it is absent, this means nothing has been saved yet); open it and edit it. If Emacs is already open, restart Emacs for the configuration to take effect.
The following configuration is recommended for use in contests.
;; Set one-key compilation. Parameters can be added manually. It is difficult to memorize, so it is not recommended for use in contests; relying on one-key compilation is not recommended.
(defun compile-file ()(interactive)(compile (format "g++ -o %s %s -g -lm -Wall" (file-name-sans-extension (buffer-name))(buffer-name))))
(global-set-key [f9] 'compile-file)
;;;; Set the compilation shortcut (if one-key compilation is set, do not make it conflict with one-key compilation).
;;(global-set-key [f9] 'compile)
(global-set-key (kbd "C-a") 'mark-whole-buffer) ;; Select-all shortcut
(global-set-key (kbd "C-z") 'undo) ;; Undo shortcut
(global-set-key [f10] 'gud-gdb) ;; GDB debugging shortcut
(global-set-key (kbd "RET") 'newline-and-indent) ;; Automatically indent new lines
(global-set-key (kbd "C-s") 'save-buffer) ;; Set the save shortcut
(setq-default kill-ring-max 65535) ;; Increase the undo history
;; C++ coding style; generally controls indentation rules
;;; "bsd" puts all braces on new lines
;;; "java" does not put braces on new lines. else follows the closing brace
;;; "awk" puts braces on new lines only beside namespaces and when defining classes or functions. else follows the closing brace
;;; "linux" puts braces on new lines only beside namespaces and when defining classes or functions. else follows the closing brace. Generally, this style should use an indentation of 8 spaces
(setq-default c-default-style "awk")
Complete configuration
;; Set one-key compilation
(defun compile-file ()(interactive)(compile (format "g++ -o %s %s -g -lm -Wall" (file-name-sans-extension (buffer-name))(buffer-name))))
(global-set-key [f9] 'compile-file)
;;;; Set the compilation shortcut (if one-key compilation is set, do not make it conflict with one-key compilation)
;;(global-set-key [f9] 'compile)
;; Essential for contests
(global-set-key (kbd "C-a") 'mark-whole-buffer) ;; Select-all shortcut
(global-set-key (kbd "C-z") 'undo) ;; Undo shortcut
(global-set-key [f10] 'gud-gdb) ;; GDB debugging shortcut
(global-set-key (kbd "RET") 'newline-and-indent) ;; Automatically indent new lines
(global-set-key (kbd "C-s") 'save-buffer) ;; Set the save shortcut
(setq-default kill-ring-max 65535) ;; Increase the undo history
;;(define-key key-translation-map [apps] (kbd "M-x")) ;; Set the command shortcut on Windows
;; Set indentation
;;; C++ code indentation length.
(setq-default c-basic-offset 4)
;;; Use tab indentation
(setq-default indent-tabs-mode t)
;;; Tab width. It must match the indentation length
(setq-default default-tab-width 4)
(setq-default tab-width 4)
;; Set the default coding environment
(set-language-environment "UTF-8")
(set-default-coding-systems 'utf-8)
;; Do not show the welcome page
(setq-default inhibit-startup-screen t)
;; Set the title
(setq-default frame-title-format "")
;; Show line numbers
(global-display-line-numbers-mode t)
;; Highlighting
(global-hl-line-mode 1);; Highlight the current line
(show-paren-mode t);; Highlight matching parentheses
(global-font-lock-mode t);; Syntax highlighting
;; Allow pasting between emacs and other external programs; seemingly allowed by default
(setq-default x-select-enable-clipboard t)
;; Set the font to Ubuntu Mono 16; an error will be reported if the font does not exist
(set-face-attribute 'default nil :font "Ubuntu Mono-16")
;(set-face-attribute 'default nil :font "Consolas-16") ;; Use this line on Windows
;; Mouse-wheel support
(mouse-wheel-mode t)
;; Set the cursor shape to a vertical bar (a block by default)
(setq-default cursor-type 'bar)
;; Change yes/no answers to y/n answers
(fset 'yes-or-no-p 'y-or-n-p)
;; Transparency
(set-frame-parameter (selected-frame) 'alpha (list 85 60))
(add-to-list 'default-frame-alist (cons 'alpha (list 85 60)))
;; Reduce the number of lines scrolled on a page to prevent scrolling by a whole page
(setq-default scroll-margin 3 scroll-conservatively 10000)
;; Optimize the file-tree structure
(ido-mode t)
;; Color scheme
(setq default-frame-alist
'((vertical-scroll-bars)
(top . 25)
(left . 45)
(width . 120)
(height . 40)
(background-color . "grey15")
(foreground-color . "grey")
(cursor-color . "gold1")
(mouse-color . "gold1")
(tool-bar-lines . 0)
(menu-bar-lines . 1)
(scroll-bar-lines . 0)
(right-fringe)
(left-fringe)))
(set-face-background 'highlight "gray5")
(set-face-foreground 'region "cyan")
(set-face-background 'region "blue")
(set-face-foreground 'secondary-selection "skyblue")
(set-face-background 'secondary-selection "darkblue")
(set-cursor-color "wheat")
(set-mouse-color "wheat")
(custom-set-variables
'(ansi-color-faces-vector
[default default default italic underline success warning error])
;; Start Ctrl-x Ctrl-c Ctrl-v = cut copy paste
'(cua-mode t nil (cua-base))
'(show-paren-mode t)
;; Hide the toolbar
'(tool-bar-mode nil))
;; Turn off cursor blinking
'(blink-cursor-mode nil)
(custom-set-faces)
Further reading
To start Emacs in terminal mode, add the -nw parameter when launching it. Emacs has several variants, such as GCC Emacs, which uses native-comp to reduce latency, its pure GTK variant, and Emacs Macport, optimized for macOS.
Emacs has a centralized software repository; after configuration, plugins can be installed with M-x package-install. Using a mirror site can speed up downloads.
Emacs can use the Language Server Protocol to improve the editing experience. The currently recommended C++ backend is Clangd. The frontend can be Eglot or Emacs LSP; consulting this entry may help with choosing a frontend.
Org Mode documents with the .org extension can be converted to Markdown documents through Pandoc.
References and notes
Footnotes
-
This key opens the right-click menu, and is generally the first key to the left of the right Ctrl key. ↩