A collection of efficient editing commands in Emacs

This is a summary of useful commands/keybindings in Emacs for efficient editing. Note that it may rely on certain configurations (mostly in init-edit.el). 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-=

Use M-x whitespace-mode to show white spaces as dots

Windows

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

Open the Messages buffer :: C-h e

File Formatting

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

Remove all except 1 space between characters :: ESC SPC (due to Alfred) just-one-space (built-in: M-SPC)

Delete all trailing whitespaces: delete-trailing-whitespace M-s k

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 --

Change encodings: utf-8-unix, utf-8-dos

  • change when saving using C-x RET f
  • force it immediately by using C-x RET c, 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

Jumping

Use mark rings to jump:

  • 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 (position-register) to jump:

  • record the position of point in register [r]: C-x r SPC [r]
  • jump to the position saved in register [r]: C-x r j [r]
  • use counsel-register to show the list: M-g r

Use bookmark to jump:

  • 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)
  • C-x r l to list all bookmarks (list-bookmarks)
  • M-x bookmark-save to save all bookmark values in the default bookmark file

Advanced editing

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

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 <

Quick functions

Insert date/time

  • insert-date like “06 Sep 2019”
  • insert-date-digits like “2019/09/06”
  • insert-time like “18:37:23”

Add incremental numbers to lines: C-x r N

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