How to Set Emacs's User Interface to Modern Conventions

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page shows you how to set up emacs so its user interface is more similar to modern applications.

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

If you want a out-of-the-box solutions, download: Aquamacs Emacs (Mac). or, install ergoemacs-mode at https://ergoemacs.github.io/.

Standard Copy Paste Keys

How to have standard keyboard shortcuts for Copy & Paste?

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

(cua-mode 1)

The CUA mode will do 4 things:

  1. {Cut, Copy, Paste, Undo} have {X, C, V, Z} keys.
  2. Text selection will be highlighted. (this is default starting with emacs 23) 〔➤ Emacs 23.1 New Features (released 2009-07)
  3. When there's a text selection, typing will override it.
  4. Text selection can be done by holding down the ⇧ Shift key and press a arrow key. (default behavior starting with emacs 23)

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. 〔➤ Emacs: Best Undo/Redo Mode

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

How to have redo?

You need to install a redo mode. See: Emacs: Best Undo/Redo Mode.

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

See: Emacs: How to Copy/Cut Current Line.

How to auto-insert bracket pairs?

(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 paren match highlighting
(setq show-paren-style 'expression) ; highlight entire bracket expression

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

How to have auto syntax color CSS hex color?

Install rainbow-mode or see: Emacs: Working with CSS Color Values.

Font

How to change the default font?

Emacs: How to List & 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)

Backup File, Autosaved Files, Recently Opened Files

How to make emacs stop creating those “backup~” files or “#autosave#” files?

Put the following in your emacs init file.

(setq make-backup-files nil) ; stop creating backup~ files
(setq auto-save-default nil) ; stop creating #autosave# files

〔➤ Emacs: Set Backups into a Directory

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. 〔➤ 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

Detail: Emacs: Open Recently Opened File: recentf-mode

How to restore opened files from last session?

You need to have 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?

See: Emacs: Tabs, Space, Indentation Setup.

How to turn on ruler?

Call ruler-mode.

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

How to show tabs as in web browser?

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

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

emacs tabbar screenshot 2013-04-20
Emacs tabbar screenshot

Line Margin, Line Wrap, Line Spacing

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

This is default with emacs 23. 〔➤ 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?

Pull the menu 〖Options ▸ Line Wrapping in this Buffer〗, or 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 set it on or off permanently, use:

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

For detail, see: Emacs: Visual Line Mode, Wrap Lines at Word Boundaries.

〔➤ 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."
  (interactive)
  (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 lines. Proper name would be “toggle-softwrap”.

How to set the spacing between lines?

See: 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.

You can set the width used in the above commands. Call set-variable, then given variable fill-column.

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.

See: Emacs unfill-paragraph, unfill-region.

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.

Misc

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 (reverse video)?

Start emacs with emacs --reverse-video. (-rv for short.)

This is useful when working in bright sunlight. See also: Emacs: Playing with Color Theme.

Thanks to Ivan Kozik for a tip.

Stop Cursor Going into Minibuffer Prompt

Emacs: Stop Cursor Going into Minibuffer Prompt

Save Cursor Position Across Sessions

Emacs: Save Cursor Position Across Sessions

Configure Mouse Wheel & Buttons

Emacs: How to Set Mouse Buttons.

Emacs: How to Use and Setup whitespace-mode

Emacs: How to Use and Setup whitespace-mode

Like it? Buy Xah Emacs Tutorial.
blog comments powered by Disqus