If you enjoyed this site, please consider donating $3. Any amount is appreciated. Thanks!

How To Make Emacs Use Modern User Interface

Xah Lee, 2008-06, 2009-07, 2009-09-16

This page gives some tips on setting up emacs so its user interface is more compatible with modern text editors.

The following guide assume you are using plain emacs version 22 (released in 2007). Emacs 23 (released on 2009-07-30) will be mentioned specifically.

If you want a out-of-the-box solutions, you can download: EmacsW32 for Windows, Aquamacs Emacs for Mac, or try ErgoEmacs (alpha).

Keybard Shortcuts

How to have standard keys for Copy and Paste?

In emacs, turn on the CUA Mode, using menu “Options‣C-x/C-c/C-v Cut and Paste (CUA)”, then use menu “Options‣Save Options”.

The CUA mode will achieve 4 things: (1) Cut/Copy/Paste has shortcuts with x/c/v keys; (2) Text selection will be highlighted; (3) Typing while a region is selected will delete/over-ride it. (4) Text selection can be done by holding down the Shift key and press a arrow key.

How to have standard shortcut keys like “Ctrl+o” for Open, “Ctrl+w” for Close, and “Ctrl+s” for Save, “Ctrl+a” for Select All?

Use the package ErgoEmacs Keybinding.

How to have redo?

You need to install the redo mode by Kyle E Jones at Source For some discussion, see emacswiki at Source. Once you install it, put the following code in your emacs init file:

(global-set-key (kbd "C-z") 'undo) ; Ctrl+z
(global-set-key (kbd "C-S-z") 'redo) ;  Ctrl+Shift+z

Text Highlighting

How to have emacs highlight text selections?

Use the menu “Options‣Active Region Highlighting”, then “Options‣Save Options”. Or, put this in your emacs init file:

(transient-mark-mode 1) ; highlight text selection
(delete-selection-mode 1) ; delete seleted text when typing

When delete-selection-mode is on, it turn on transient-mark-mode automatically. So, turning on transient-mark-mode before is not necessary. Also, when cua-mode is on, the behavior of both transient-mark-mode and delete-selection-mode are there, but cua-mode has its own implementation and does not actually rely on them.

Note: Emacs 23 has transient-mark-mode on by default, but does not have delete-selection-mode on by default.

How to have syntax coloring on by default?

This is on by default in emacs 22 and 23.

If not on, you can use the menu “Options‣Syntax Highlighting”. If you always want this on, then pull “Options‣Save Options”.

Alternatively, put the following code in your emacs init file:

(global-font-lock-mode 1) ; turn on syntax coloring

How to have matching parenthesis highlighted?

Use the menu “Options‣Paren Match Highlighting” then “Options‣Save Options”.

Or, you can put the following code in your emacs init file:

(show-paren-mode 1) ; turn on paren match highlighting

show-paren-mode can be setup so that it highlight the entire paren enclosed text, instead of just the two paren chars. For how to, see: Tips For Editing Lisp Code With Emacs.

How to have the current line highlighted?

Type “Alt+x hl-line-mode”. Type that again to turn it off. To have it on always, put this:

; highlight current line
(add-hook 'text-mode-hook (lambda () (hl-line-mode 1)))

Font

How to change the default font?

Use the menu: “Options‣Set Font”, then “Options‣Save Options”.

Displaying Line Numbers and Column Number

How to show line numbers?

Type “Alt+x line-number-mode”. After that, the line number the cursor is on shows in the status bar at the bottom.

In emacs 23, you can have line numbers displayed in the left vertical margin. To turn it on, type “Alt+x linum-mode”.

To have these on by default, put the following:

; show line number the cursor is on, in status bar (the mode line) 
(line-number-mode 1)

; display line numbers in margin (fringe). Emacs 23 only.
(global-linum-mode 1) ; always show line numbers
emacs line numbers

Emacs 23 with linum-mode on.

How to show the cursor's column position?

Alt+x column-number-mode”. To always have it on, put the following code in your emacs init file.

(column-number-mode 1)

Backup & Files

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

Put the following in your emacs init file.

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

How to have a menu of recently opened files?

You need to turn on the recentf mode. Put the following in your emacs init file:

(recentf-mode 1)

Once turned on, you can open recently opened files under menu “File‣Open Recent”, or by typing “Alt+x recentf-open-files”.

Line Margin and Line Spacing

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

This is default with emacs 23.

For emacs 22, see: emacswiki.org move by visible line.

How to have lines soft wrapped at word boundary?

In emacs 23, pull the menu “Options‣Line Wrapping in this Buffer”, or type “Alt+x visual-line-mode” toggles the behavior.

emacs23 word wrap

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

To toggle globally, type “Alt+x global-visual-line-mode”. To set it on or off permanently, use:

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

How to set the right margin to something like infinity, so that long lines runs off screen instead wrapping at the window border?

Use the menu “Options‣Truncate Long Lines in this Buffer”.

In Emacs 23, there's a builtin command toggle-truncate-lines. You can set a shortcut for it, like this:

(global-set-key (kbd "<f7>") 'toggle-truncate-lines)

If you are not using emacs 23 or later, you can define it like this:

(defun toggle-truncate-lines ()
"Toggle whether to wrap lines at right window border."
(interactive) 
   (if (eq truncate-lines 't) 
     (set-variable 'truncate-lines nil)
     (set-variable 'truncate-lines 't)
    ) )

How to set the default spacing between lines?

Type “Alt+x setq-default”, then set line-spacing with value 4. This will set line-spacing to 4 pixels. You can also use a decimal. If you set line-spacing to 0.4, then it means the line spacing will be 40% of normal line height.

You can put the following elisp code in your emacs init file to make life simpler.

(defun toggle-line-spacing ()
"Toggle line spacing between 1 and 5 pixels."
(interactive)
(if (eq line-spacing 1)
  (setq-default line-spacing 5)
  (setq-default line-spacing 1))
)
(global-set-key (kbd "<f7>") 'toggle-line-spacing)

With the above code, pressing F7 will toggle line spacing to small and large. This is useful for example, when you often switch between coding and novel reading.

Reformatting Lines

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

Type “Alt+q” (fill-paragraph) will reformat the current block of text your cursor is on. Type “Alt+x fill-region” to reformat a selection of text. To have lines automatically cut as you type, use auto-fill-mode.

You can set the width used in the above commands. Type “Alt+x 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 for display purposes should be avoided. See: The Harm of hard-wrapping Lines.

Is there a way to delete the hard-wrapped line endings in a paragraph?

There is no built-in function for this purpose directly. However, you can do it by setting the fill-column variable to a large value and use the line reformatting commands fill-paragraph or fill-region to reformat it.

Here are 2 elisp code that does the above. One of them has “Alt+Shift+Q” as the shortcut.

(defun remove-hard-wrap-paragraph () 
  "Replace newline chars in current paragraph by single spaces."
  (interactive) 
  (let ((fill-column 90002000)) 
    (fill-paragraph nil)))

(defun remove-hard-wrap-region (start end)
  "Replace newline chars in region by single spaces." 
  (interactive "r")
  (let ((fill-column 90002000)) 
    (fill-region start end)))

(global-set-key (kbd "M-Q") 'remove-hard-wrap-paragraph)

Misc

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)

Related essays:

2008-06
© 2008 by Xah Lee.