Essential Emacs Tips and Workflows
Emacs provides a built-in tutorial. Access it with:
C-h t
For structured learning, the GNU Emacs Manual (built-in via C-h i) is the most authoritative resource. The Emacs Wiki also maintains community-driven guides and examples.
Emacs configuration
Store your configuration in ~/.emacs.d/init.el rather than the legacy ~/.emacs file. This keeps your Emacs directory cleaner and is the modern standard. Example structure:
;;; init.el --- Main Emacs configuration
;; Basic settings
(setq inhibit-startup-screen t)
(setq make-backup-files nil)
;; Load other configuration files
(load-file "~/.emacs.d/bindings.el")
(load-file "~/.emacs.d/development.el")
Navigation and movement
Jump to a specific line
M-g M-g
Enter the line number and press RET.
Navigate function definitions
Jump to the beginning or end of a function with:
C-M-a ; beginning-of-defun
C-M-e ; end-of-defun
This works across most programming modes.
Move through parenthetical structures
Navigate balanced expressions and nested structures:
C-M-f ; forward-sexp
C-M-b ; backward-sexp
C-M-d ; down-list
C-M-u ; backward-up-list
C-M-n ; forward-list
C-M-p ; backward-list
Use C-M-u to jump out of nested parentheses, brackets, or braces.
Position the screen
C-l
Pressing it repeatedly cycles the current line between top, middle, and bottom of the window.
Text editing operations
Go to read-only files
Toggle a buffer’s read-only mode:
C-x C-q
This allows you to edit read-only files if you have permission. After editing, save with C-x C-s.
Remove Windows line endings (^M characters)
Convert CRLF to LF line endings:
M-x replace-string RET C-q C-m RET RET
Alternatively, set the file’s line ending mode explicitly:
C-x RET c unix RET
Then save the file. For batch conversion across files, dos2unix is more efficient on the command line.
Kill a whole line
C-S-backspace ; kill-whole-line
This removes the entire line and newline regardless of cursor position. If your terminal doesn’t support this key combination, define your own:
(global-set-key (kbd "C-c C-x") 'kill-whole-line)
Duplicate a line
Method 1: Kill and yank
C-S-backspace ; kill the line
C-y ; yank at target location
Method 2: Copy line without deleting
(defun copy-line (arg)
"Copy lines (as many as prefix argument) in the kill ring."
(interactive "p")
(kill-ring-save (line-beginning-position)
(line-beginning-position (+ 1 arg)))
(message "%d line%s copied" arg (if (= 1 arg) "" "s")))
(global-set-key (kbd "C-c C-k") 'copy-line)
Find and replace
Interactive replace (prompted for each match):
M-% ; query-replace
Batch replace all occurrences:
M-x replace-string
Rectangle operations
Rectangle commands work on rectangular regions defined by column ranges across multiple lines. Set mark at one corner, point at the opposite corner, then apply commands:
C-x r k ; kill-rectangle
C-x r d ; delete-rectangle
C-x r y ; yank-rectangle
C-x r o ; open-rectangle (insert blanks)
C-x r c ; clear-rectangle (replace with spaces)
C-x r N ; rectangle-number-lines
C-x r t STRING ; string-rectangle (replace each line)
Example: Add a prefix to multiple lines. Select the rectangle at column 0, then:
C-x r t "// "
This prepends // to each line in the rectangle.
Spell checking
Check spelling in the buffer:
M-x ispell
Enable continuous spell-checking in the current buffer:
M-x flyspell-mode
For programming, check only comments and strings:
M-x flyspell-prog-mode
When Ispell highlights a misspelled word, press ? for correction options.
TAGS for code navigation
Generate TAGS file recursively in the current directory:
(defun regen-tags ()
"Regenerate TAGS file for the current directory."
(interactive)
(let ((tag-file (concat default-directory "TAGS")))
(shell-command "find . -type f -name '*.el' -o -name '*.c' -o -name '*.h' | xargs etags")
(visit-tags-table tag-file)
(message "TAGS regenerated")))
(global-set-key (kbd "C-c t") 'regen-tags)
Then navigate to definitions with:
M-. ; find-tag
M-* ; pop-tag-mark (return to previous location)
Modern projects should use lsp-mode instead of TAGS for better IDE-like navigation.
Undo and redo
Emacs has a linear undo system tracking all buffer states:
C-/ ; undo
C-? ; redo (undo the undo)
Alternatively:
C-x u ; undo
Performing regular commands after undo clears the redo stack. To recover undone changes, use C-/ multiple times.
Display and appearance
Show line numbers
Modern Emacs (29+) includes display-line-numbers-mode:
M-x display-line-numbers-mode
Enable globally by default:
(global-display-line-numbers-mode)
The older linum-mode is slower and should be avoided.
Set font and size
Configure the default font in your init file:
(set-face-attribute 'default nil
:family "Monospace"
:height 120)
The height is in 1/10 of a point. Adjust font size in the current session:
C-x C-+ ; increase
C-x C-- ; decrease
C-x C-0 ; reset to default
Hide interface elements
Disable the toolbar:
(tool-bar-mode -1)
Disable the menu bar (useful for terminal Emacs):
(menu-bar-mode -1)
Toggle either at runtime with M-x tool-bar-mode or M-x menu-bar-mode.
Compilation and error navigation
In compile mode, jump between errors:
C-x ` ; next-error
M-g M-p ; previous-error
M-g M-n ; next-error (alternative)
Configure compile command:
(global-set-key (kbd "C-c c") 'compile)
(setq compile-command "make -j4")

Several Emacs cheat sheets:
http://doors.stanford.edu/~sr/computing/emacs.html
http://refcards.com/docs/gildeas/gnu-emacs/emacs-refcard-a4.pdf
Auto completion in Emacs:
# yum install emacs-auto-complete*
Complete by
TAB
or RET
Useful keys:
M-n
M-p
M-1
M-2
Generate ETAGS in Emacs:
Add to ~/.emacs
;; =================== etags ===================== (defvar tags-cmd "etags -R ./* 2>/dev/null") (defun regen-tags () "Regenerate the tags file for the current working directory" (interactive) (let ((tag-file (concat default-directory "TAGS"))) (shell-command tags-cmd) (visit-tags-table tag-file))) ;; =================== end etags =====================To regnerate ETAGS:
M-x regen-tags
Goto a specific line:
Emacs supports jumping to a specific line:
Then enter the line number and RET.
Jump to next and previous error
In the compile mode, you can jump to the next/previous error in the compile buffer by:
and
Jump to beginning and end of a function
The functions beginning-of-defun and end-of-defun works with most programming modes in Emacs that jumps to the beginning and end of the function/class definition.
The commands are
and
This will make editing code much easier.
Moving in the Parenthesis Structure
Move over groupings delimited by parentheses (or whatever else serves as delimiters in the language you are working with):
C-M-n
Move forward over a parenthetical group (forward-list).
C-M-p
Move backward over a parenthetical group (backward-list).
C-M-u
Move up in parenthesis structure (backward-up-list).
C-M-d
Move down in parenthesis structure (down-list).
Move between a brackets pair:
C-M-n for forward match and C-M-u for backward match.
Operating rectangles.
Rectangle commands operate on rectangular areas of the text: all the characters between a certain pair of columns, in a certain range of lines.
To specify a rectangle for a command to work on, set the mark at one corner and point at the opposite corner. If point and the mark are in the same column, the region-rectangle is empty. If they are in the same line, the region-rectangle is one line high.
C-x r k string
Kill the text of the region-rectangle, saving its contents as the “last killed rectangle” (kill-rectangle).
C-x r d
Delete the text of the region-rectangle (delete-rectangle).
C-x r y
Yank the last killed rectangle with its upper left corner at point (yank-rectangle).
C-x r o
Insert blank space to fill the space of the region-rectangle (open-rectangle). This pushes the previous contents of the region-rectangle to the right.
C-x r N
Insert line numbers along the left edge of the region-rectangle (rectangle-number-lines). This pushes the previous contents of the region-rectangle to the right.
C-x r c
Clear the region-rectangle by replacing all of its contents with spaces (clear-rectangle).
M-x delete-whitespace-rectangle
Delete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.
C-x r t string
Replace rectangle contents with string on each line (string-rectangle).
M-x string-insert-rectangle
Insert string on each line of the rectangle.
Spell checking
M-x ispell
Check and correct spelling of all words in the buffer. If the region is active, do it for all words in the region instead.
M-x flyspell-mode
Enable Flyspell mode, which highlights all misspelled words.
M-x flyspell-prog-mode
Enable Flyspell mode for comments and strings only.
Spell correcting
Skip this word—continue to consider it incorrect, but don’t change it here.
a
Accept the incorrect word—treat it as correct, but only in this editing session.
Undo and redo
Emacs has a powerful undo system. It allows you to recover any past state of a buffer.
C-_
undo
C-g C-_
redo after a undo
Performing some commands after a sequence of undo, all the undos are pushed to the operation stack, and the next undo undoes the last command. Multiple C-_ to redo what have been undone by C-_.
Display line numbers
Display line numbers:
or
To enable it by default
Add
to ‘~/.emacs’.
Position the screen
We can position the screen by
Invoking ‘C-l’ multiple times makes Emacs position the current line at the middle, top and bottom of the screen iteratively.
Replace in Emacs
Interactive find and replace
or
Batch replace
Set Emacs’s font size
To set the font size to 12pt, put this line into ‘~/.emacs’:
The value is in 1/10pt.
Increase/decrease current instance of Emacs’s font size:
and
Hide Emacs’ toolbar / menubar
To hide the toolbar completely, put this line into ‘~/.emacs’:
To hide the menubar:
You can also toggle it by
or
Kill a line without moving the cursor position
1. Using 2 ‘C-k’ with ‘C-a’ first can kill the current line. But there are easier methods as follows.
2. Use ‘kill-whole-line’
C-S-backspace (kill-whole-line) kills a whole line and its newline, no matter where the point within the line is.
However, many text terminals will prevent you from typing the key sequence C-S-backspace.
3. Define your own key bindings
I prefer ‘C-c C-x’ to kill a whole line by putting this line into ‘~/.emacs’:
Duplicate a line in Emacs
1. Using the keyboard
First, kill the current line (the line to duplicate)
Then, yank it at the position where it should be duplicated
2. Define a function and bind it to ‘C-c C-k’:
(defun copy-line (arg) "Copy lines (as many as prefix argument) in the kill ring" (interactive "p") (kill-ring-save (line-beginning-position) (line-beginning-position (+ 1 arg))) (message "%d line%s copied" arg (if (= 1 arg) "" "s"))) ;; optional key binding (global-set-key "C-cC-k" 'copy-line)Reference: http://emacswiki.org/emacs/CopyingWholeLines
How to avoid the pain of pressing Ctrl keys that are far away from your figure? I finally find that “maping win key to Ctrl” is the most convenient way for me: https://www.systutorials.com/qa/742/how-to-map-win-key-to-ctrl-on-linux