Emacs: Misc Init Tips

By Xah Lee. Date: .

standard keyboard shortcuts for Copy and Paste

Alt+x cua-mode to toggle it on/off.

Put this in your emacs init file:

;; make {copy, cut, paste, undo} have {C-c, C-x, C-v, C-z} keys
(cua-mode 1)

standard keyboard shortcuts for {Open, Close, Save, Save As, Select All, …}

You can put the following in your emacs init, but they don't work well, because some major mode or minor will override these keys, and you lost some default emacs cursor movement keys.

 ;; make emacs use standard keys

  ;; Select All. was move-beginning-of-line
  (global-set-key (kbd "C-a") 'mark-whole-buffer-buffer)

  ;; Find. was forward-char
  (global-set-key (kbd "C-f") 'isearch-forward)

  ;; New. was next-line
  (global-set-key (kbd "C-n") 'xah-new-empty-buffer)

  ;; New Window. was nil
  (global-set-key (kbd "C-S-n") 'make-frame-command)

  ;; Open. was open-line
  (global-set-key (kbd "C-o") 'ido-find-file)

  ;; Save. was isearch-forward
  (global-set-key (kbd "C-s") 'save-buffer)

  ;; Save As. was nil
  (global-set-key (kbd "C-S-s") 'write-file)

  ;; Paste. was scroll-up-command
  (global-set-key (kbd "C-v") 'yank)

  ;; Close. was kill-region
  (global-set-key (kbd "C-w") 'kill-buffer)

  ;; Redo. was yank
  (global-set-key (kbd "C-y") 'redo)

  ;; Undo. was suspend-frame
  (global-set-key (kbd "C-z") 'undo)

For the command xah-new-empty-buffer, get it at Emacs: New Empty Buffer.

For the command redo, install redo mode. [see Emacs: Undo/Redo Saga]

A robust solution is to install the package ergoemacs-mode. https://ergoemacs.github.io/

or xah-fly-keys. [see Emacs: Xah Fly Keys]

make typing overwrite selected text

;; make typing delete/overwrites selected text
(delete-selection-mode 1)

set default file encoding

;; UTF-8 as default encoding
(set-language-environment "UTF-8")
(set-default-coding-systems 'utf-8)

Do not add a bunch of other variables such as set-terminal-coding-system, unless you know emacs's encoding system well.

[see Emacs: Unicode Tutorial]

[see Unicode Basics]

refresh file automatically

Sometimes other program may have changed file that is already opened in emacs.

To refresh, Alt+x revert-buffer

To always refresh automatically, put this in your emacs init file:

;; when a file is updated outside emacs, make it update if it's already opened in emacs
(global-auto-revert-mode 1)

Line Margin, Line Wrap, Line Spacing

have the down arrow key move by screen lines

This is default with emacs 23. [see Emacs 23.1 Features (released 2009-07)]

(setq line-move-visual nil)
;; t means true, nil mean false

adjust margin

You can use this command:

(defun xah-toggle-margin-right ()
  "Toggle the right margin between `fill-column' or window width.
This command is convenient when reading novel, documentation."
  (if (eq (cdr (window-margins)) nil)
      (set-window-margins nil 0 (- (window-body-width) fill-column))
    (set-window-margins nil 0 0) ) )

make lines NOT soft-wrap

emacs toggle truncate lines 61581
emacs Alt+x toggle-truncate-lines

Alt+x toggle-truncate-lines.

When toggle-truncate-lines is on, lines runs off and disappears on the right window margin. You have to horizontal scroll right to see them. This is useful when you just want to get an overview of lines.

Note: the command name is misleading. It doesn't really “truncate”. Proper name would be “toggle-line-softwrap”.

Format Lines

reformat lines so that lines are not longer than 70 chars

emacs fill paragraph 80995
emacs Alt+x fill-paragraph

Alt+x fill-paragraphAlt+q】 to reformat the current block of text your cursor is on.

Alt+x fill-region to reformat a text selection.

To have emacs automatically insert a newline char when your line reaches to the right, Alt+x auto-fill-mode.

The variable fill-column controls the width used in the above commands. Alt+x set-variable, to change value.

Note: these commands insert newline characters into your file. This type of wrapping is called hard-wrap. Hard-wrap convention of 80 chars came from punched card. You should avoid hard-wrap when possible; add newline char only at logical positions. (Rant: The Harm of hard-wrapping Lines.)

make sentence ending by single space

When Alt+x fill-paragraph etc, emacs will reformat it so that there are 2 spaces after a period.

;; make sentence ending by single space
(setq sentence-end-double-space nil )

[see Emacs: Hard Wrap Lines]

have fixed scroll

Alt+x scroll-lock-mode. When on, up/down arrow keys move the page, instead of the cursor.

This only turns it on in current buffer.

Type y/n instead of yes/no

Many emacs commands will ask you a “yes/no” question, and you have to type the full word “yes” or “no”. (such as when deleting a file)

You can make emacs just ask “y/n” instead.

(defalias 'yes-or-no-p 'y-or-n-p)

disable warnings

Emacs by default will warn you when you use some commands for the first time. You have to answer yes or no. They are annoying.

Here's how to disable them:

  ;; stop warning prompt for some commands. There's always undo.
  (put 'narrow-to-region 'disabled nil)
  (put 'narrow-to-page 'disabled nil)
  (put 'upcase-region 'disabled nil)
  (put 'downcase-region 'disabled nil)
  (put 'erase-buffer 'disabled nil)
  (put 'scroll-left 'disabled nil)
  (put 'dired-find-alternate-file 'disabled nil)

save/store minibuffer history

Many emacs commands lets you use key to use last command or parameter. But after you restart emacs, command history is forgotten.

You can make emacs remember command history.

;; save minibuffer history
(savehist-mode 1)

Command history is saved as a file, by default at ~/.emacs.d/history

set minibuffer height

;; big minibuffer height, for ido to show choices vertically
(setq max-mini-window-height 0.5)


stop cursor blinking

;; make cursor not blink
(blink-cursor-mode 0)

set cursor to i-beam

(modify-all-frames-parameters (list (cons 'cursor-type 'bar)))

Note: this will not work if you run emacs in terminal.

set up emacs so that each file opens in a new window

Put this code in your emacs init file:

(setq pop-up-frames t)

hippie-expand setup

(setq hippie-expand-try-functions-list
        ;; try-expand-dabbrev-from-kill
        ;; try-expand-all-abbrevs
        ;; try-expand-list
        ;; try-expand-line

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

Emacs Init

Init Basics


text editing