This article keeps collecting useful commands/keybindings in Emacs for efficient editing. If you are experienced users, jump to the last section to see if anything is interesting.

⚠️ Warning: It may rely on certain configurations (see my .emacs.d on github). If you found some keybindings are not defined, please refer to the configuration files.

Basic Operations

Cursor movement

Move cursor to the top/middle/bottom of the current window: M-r

Make words in upper case/lower case/capitalized: M-u, M-l, M-c

Swap words: M-t

Open lines:

  • move to the next line and then opens a line: open-next-line C-o

  • open a new line before the current one: open-previous-line M-o

Vertical editing:

  • use set-goal-column to do vertical editing: C-x C-n

  • remove vertical mark: C-u C-x C-n

Move point to the first non-whitespace character on this line: back-to-indenentation M-m

Buffer operations

Default in undo-tree:

  • undo: C-/, C-_

  • redo: C-?, M-_

Mark regions:

  • expand region: C-=

  • mark the whole buffer: C-x h

Kill texts/region/buffers:

  • kills from the current point to a character: zap-to-char M-z [e]

  • kill line backwards: M-0 C-k (built-in) OR C-<backspace>

  • kill one sentence backwards: C-x Backspace

  • kill/yank to kill-ring:

    • C-w / M-w to kill or copy the mark region or the current line

    • C-y to yank

    • list contents in kill-ring and select to yank: M-y

Join the current line to the above one: M-^

Fill/unfill paragraph/region/buffer:

  • fill/unfill paragraph/region: M-q=/=M-Q

  • set fill-column: C-x f

  • automatically split long lines up to a prefixed length: use auto-fill mode

  • set fill-column: C-x f

Files

Insert contents of file FILENAME into buffer: insert-file C-x i

Write current buffer into file FILENAME (equiv. "Save As"): write-file C-x C-w

Reload the file from the disk: s-u (s: super/command); C-x C-v

Open file in the read-only mode: C-x C-r

Toggle overwrite mode: M-x overwrite-mode

Count words in region: M-=

Automatic correct word by flyspell: flyspell-auto-correct-word C-.

Windows

Toggle window split (horizontal <-> vertical) :: C-x |

Open the Messages buffer :: C-h e

Formatting and Editing

TAB and SPACE

Insert TAB: C-q TAB (TAB trigger complete or insert 4 spaces)

Insert 4-spaced TAB: tab-to-tab-stop or M-i

Converting TAB and 4-spaces of regions: tabify and untabify

Whitespaces (view and cleanup)

Enable whitespace-mode to show white spaces as dots or colourised blocks.

Remove all except 1 trailing whitespaces: M-x SPC or Esc SPC, to call just-one-space

Delete all trailing whitespaces in the whole buffer: M-s k to call delete-trailing-whitespace

Remove all following blank lines: C-x C-o to call delete-blank-lines

Encoding

Encoding: use utf-8-unix by default

  • open a file with specific coding system: revert-buffer-with-coding-system

  • set a encoding system for saving file: set-buffer-file-coding-system

  • find out the current coding system used for opening and saving files: describe-current-coding-system

  • find out what encoding system was used to decode current file: describe-variable buffer-file-coding-system

  • list all available encodings with M-x list-coding-systems

  • declare a file with a particular character encoding: add the following line in the first line of your files:

    -- coding: utf-8 --

Typical procedure on converting other encoded files to UTF-8:

  1. reload the buffer with an alternative encoding to confirm the right one to choose: C-x RET r, e.g. gbk;

  2. two ways to change encoding to UTF-8, then save as a new file:

    • change when saving using C-x RET f, e.g., utf-8-unix, then C-x C-s;

    • load it immediately by using C-x RET c, e.g. utf-8-emacs-unix, and then write to a new file C-x C-w RET.

Change End-of-Line:

  • M-x set-buffer-file-coding-system utf-8-unix or use C-x RET f

Copy/Paste

Use register to copy/paste:

  • C-x r s [R] to copy the region in register

  • C-x r r [R] to copy the rectangle to register

  • C-x r g [R] to (paste) insert text saved in register

Rectangle editing

Rectangle editing:

  • mark rectangle: C-x SPC

  • kill rectangle: C-x r k

  • copy rectangle: C-x r M-w

  • yank the last killed rectangle: C-x r y

  • delete the region-rectangle: C-x r d

  • open rectangle, shifting text right: C-x r o

  • blank out rectangle: C-x r c

  • prefix each line with a string: C-x r t

Use multiple-cursors:

  • select one word C->, then hit C-g to place multiple cursors

  • to place cursors in front of each lines: select multiple lines, then hit C-S-l C-S-l to place cursors OR select nothing and hit C->, then edit

  • (disabled) use C-S-SPC to mark rectangular region

Add incremental numbers to lines: C-x r N

Programming Supports

Comment line or region: C-\; uncomment: C-|, M-\

Align comments: M-x align; M-x align-regexp (C-x M-a) (prefix C-u for options)

Use iedit: C-;

Indent region in python-mode:

  • python-indent-shift-right C-c >

  • python-indent-shift-left C-c <

Jumping

Use mark-rings to jump within buffer

  • set mark: C-SPC C-SPC

  • jump to previous mark: C-u C-SPC after it, just C-SPC to continue jumping

  • jump to mark saved in global-mark ring: C-x C-SPC

  • exchange the cursor and the previous mark: C-x C-x

  • use counsel-mark-ring to show mark ring: M-g SPC

Use register to jump between files (within current Emacs session)

  • record the position of point in register [x]: C-x r SPC [x] (where [x] denotes any single letter)

  • jump to the position saved in register [x]: C-x r j [x]

  • use counsel-register to show the list: M-g r

Use bookmark to jump between files persistently

Most frequently used:

  • M-g b (or C-x r b): call counsel-bookmark to create or jump to bookmarks from a list

  • C-x r l: list all bookmarks (list-bookmarks). In this temporary buffer, you can manage bookmarks like in Dired.

More operations:

  • C-x r m RET to set the bookmark for the visited file, at point

  • C-x r m NAME RET to set the bookmark named NAME at point (bookmark-set)

  • C-x r b NAME RET to jump to the bookmark named NAME (bookmark-jump)

  • M-x bookmark-save to save all bookmark values in the default bookmark file

Advanced Skills

Using ivy-occur to allow multi-file editing by searching

  1. Search by counsel-rg (or any others), which lists results in the minibuffer.

  2. Call ivy-occur by C-c C-o, which freezes the minibuffer like a buffer to keep the results. You may use C-x 1 to view in a full window. In this mini-buffer window, you are allowed with second search and editing. Use w h or C-x C-q to call ivy-wgrep-change-to-wgrep-mode to allow editing the search results.

  3. In the editing mode, you may do replace or other operations. After C-c C-c to save your editing, all operations will be applied to and in effect in these original files.

Editing multiple file/folder names in Dired

  1. Open this folder in Dired, then M-x dired-toggle-read-only.

  2. Edit file name in whatever way you prefer.

  3. C-x C-s (or C-c C-c) to save.

Example: We like to change all files names like "abc20201003.mp4" to "20201003abc.mp4". Follow the above procedure, and, during editing, use M-x query-replace-regexp with ^\([^0-9]*\)\([0-9]*\)\.mp4, then type \2\1.mp4.