How to Set Emacs's User Interface

By Xah Lee. Date: . Last updated: .

This page shows you how to set up many preferences in emacs.

The following assume you are using GNU Emacs version 24.1 or later. 〔➤see Emacs 24.1 New Features (released 2012-06)

Starting Emacs Without Loading Init File

When you start to customize, you might make a mistake. You want to know how to start emacs without loading your init.

How to startup emacs without loading any customization?

To run emacs without loading your personal init file, start emacs like this: emacs -q.

To not load any site-wide startup file, start emacs with emacs -Q. The site-wide startup file is usually part of your emacs distribution, such as from Carbon emacs, Aquamacs, ErgoEmacs. Starting emacs with “-Q” is like running a bare bone GNU Emacs.

emacs launch command line options
-q--no-init-fileDon't load your personal init file.
-Q--quick same as --no-init-file --no-site-file --no-splash.

For Microsoft Windows, you can create a alias of the emacs icon, and in properties, set the launch options. 〔➤see Emacs and Microsoft Windows Tips

(info "(emacs) Initial Options")

Standard Copy Paste Keys

How to have standard keyboard shortcuts for Copy & Paste?

Turn on cua-mode. Put the following in your emacs init file:

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

How to have standard keyboard shortcuts for {Open, Close, Save, Save As, Select All, …}?

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

(progn ; standard keys
  (global-set-key (kbd "C-a") 'mark-whole-buffer-buffer) ; Select All. was move-beginning-of-line
  (global-set-key (kbd "C-f") 'isearch-forward) ; Find. was forward-char
  (global-set-key (kbd "C-n") 'xah-new-empty-buffer) ; New. was next-line
  (global-set-key (kbd "C-S-n") 'make-frame-command) ; New Window. was nil
  (global-set-key (kbd "C-o") 'ido-find-file)        ; Open. was open-line
  (global-set-key (kbd "C-s") 'save-buffer)  ; Save. was isearch-forward
  (global-set-key (kbd "C-S-s") 'write-file) ; Save As. was nil
  (global-set-key (kbd "C-v") 'yank)         ; Paste. was scroll-up-command
  (global-set-key (kbd "C-w") 'kill-buffer)  ; Close. was kill-region
  (global-set-key (kbd "C-y") 'redo)         ; Redo. was yank
  (global-set-key (kbd "C-z") 'undo)         ; Undo. was suspend-frame

For the command “xah-new-empty-buffer”, find it at Emacs: New Empty Buffer. You also need to install a redo mode. 〔➤see Emacs: Best Undo/Redo Mode

A more robust solution is to install the package ergoemacs-mode.

How to have redo?

Emacs: Best Undo/Redo Mode

How to make the copy key copy the current line when there's no selection?

Emacs: How to Copy/Cut Current Line

How to automatically insert right brackets when left one is typed?

(electric-pair-mode 1)

〔➤see Emacs: Insert Parenthesis by Pair: electric-pair-mode

Text Highlighting

How to have matching parenthesis highlighted? (when the cursor is on one)

Put the following code in your emacs init file:

(show-paren-mode 1) ; turn on bracket match highlight

〔➤see Emacs: Auto Highlight Parenthesis: show-paren-mode

How to have auto syntax color CSS hex color?

Emacs: Working with CSS Color Values


How to change the default font?

Emacs: Set Font

Displaying Line Numbers and Column Number

How to show line numbers?

emacs line numbers
Emacs: Display Line Numbers linum-mode

How to show the cursor's column position?

Call column-number-mode. After you turned it on, the cursor's line position and column position will show in the status bar, like this: (166,3). The first is line number, the second is position from the beginning of line.

To always have it on, put the following code in your emacs init file.

(column-number-mode 1)

Stop Backup File

How to disable emacs's automatic backup~ file?

Put this code in your emacs init file:

(setq make-backup-files nil) ; stop creating those backup~ files

How to disable emacs's “#auto-save#”?

(setq auto-save-default nil) ; stop creating those #auto-save# files

Note: emacs's “auto-save” isn't automatic save in the normal sense. When you are editing a file, emacs automatically creates a “backup” copy of it, named “#your_file_name#”, and periodically updates it. When you actually save the file, this “#auto-saved#” file is then deleted.

This emacs “auto-save” feature, is for in case there's a power failure.

How to set emacs so that all backups are placed into one backup folder?

Emacs: Set Backup into a Directory

How to stop emacs's backup changing the file's creation date of the original file?

Put this code in your emacs init file:

(setq backup-by-copying t)

Explanation: when emacs does a backup, by default it renames the original file into the backup file name, then create a new file and insert the current data into it. This effectively destroys the creation date of your file. (If a file is created in 2001, and you modified it today, the file's creation date will become today. Note: unixes (including Linux & BSD) do not record file creation date, so this doesn't matter. (ctime is not creation date.) Microsoft Windows and Mac OS X do record file creation date.).

Open Recently Opened Files

How to open recently opened file in emacs?

Call recentf-mode to turn it on. Then, call recentf-open-files to list and open recently opened file. You can give it a keyboard shortcut. 〔➤see Emacs: How to Define Keys

To turn it on for future sessions, put this in your emacs init file:

(recentf-mode 1) ; keep a list of recently opened files

〔➤see Emacs: Open Recently Opened File: recentf-mode

How to restore opened files from last session?

Turn desktop-save-mode on.

(desktop-save-mode 1) ; save/restore opened files
〔➤see Emacs: Save/Restore Opened Files, Windows Configuration: desktop-mode

Tabs, Space, Indentation, Keyword Completion

How to setup tabs, space, indentation?

Emacs: Tabs, Space, Indentation Setup

How to turn on ruler?

Call ruler-mode.

emacs ruler-mode screenshot 2013-04-20
Emacs ruler-mode

How to show tabs as in web browser?

You need to install tabbar-mode. 〔➤see Emacs: How to Install Packages Using ELPA, MELPA〕.

You can also install “tabbar-ruler” to make it pretty.

emacs tabbar screenshot 2013-04-20
Emacs tabbar screenshot

Note: from my experience, tabbar-mode slows down emacs noticeably. This is so at least from 2008 to 2014.

Line Margin, Line Wrap, Line Spacing

How to have the down arrow key move by screen lines?

This is default with emacs 23. 〔➤see Emacs 23.1 New Features (released 2009-07)

(setq line-move-visual nil) ; use t for true, nil for false

How to have lines soft wrapped at word boundary?

Call visual-line-mode.

emacs23 word wrap
visual-line-mode off (top) and on (bottom) in emacs 23.

To toggle globally, call global-visual-line-mode.

To turn on global-visual-line-mode always, put this in your init:

(global-visual-line-mode 1) ; 1 for on, 0 for off.

〔➤see Emacs: Visual Line Mode, Wrap Lines at Word Boundaries

〔➤see Emacs: Novel Reading Mode

How to 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) ) )

How to make lines NOT soft-wrap?

Call toggle-truncate-lines.

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

How to set the spacing between lines?

Emacs: Set Line Spacing

Reformatting Lines

How to reformat paragraphs so that lines are not longer than 70 chars?

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

Call fill-region to reformat a text selection.

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

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

Note: these commands actually 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.)

Is there a unfill-paragraph? I want to remove line-break in a paragraph.

Emacs: Hard Wrap Lines, fill-paragraph, unfill-paragraph

Fixed Scroll

How to have cursor line always highlighted?

emacs hl-line-mode screenshot 2015-08-12
emacs global-hl-line-mode

Call hl-line-mode to toggle on/off. Call global-hl-line-mode to toggle globally.

(global-hl-line-mode 1) ; turn on highlighting current line

How to have fixed scroll?

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

This only turns it on in current buffer.


How to save/store minibuffer history?

;; save minibuffer history
(savehist-mode 1)

How to save/store cursor position?

Emacs: Save Cursor Position

How to set cursor to i-beam?

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

How to 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) ; t for true, nil for false

How to make emacs use dark background color?

Emacs: Set Color Theme

Thanks to Ivan Kozik for a tip.

Stop Cursor Going into Minibuffer Prompt

Emacs: Stop Cursor Going into Minibuffer Prompt

Configure Mouse Buttons

Emacs: How to Set Mouse Buttons.

Emacs: Make Whitespaces Visible

Emacs: Make Whitespaces Visible

Emacs: Set Default Window (frame) Size

Emacs: Set Default Window (frame) Size

Like it? Buy Xah Emacs Tutorial. Thanks.