Xah Lee, 2008-06, …, 2011-11-21
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 23.2 (released in 2009-07). (see: New Features in Emacs 23.)
If you want a out-of-the-box solutions, you can download: ErgoEmacs (Windows), Aquamacs Emacs (Mac).
How to have standard keys for Copy and Paste?
Turn on the CUA mode. Put the following in your emacs init file:
(cua-mode 1)
The CUA mode will do 4 things:
How to have standard shortcut keys for Open, Close, Save, Save As, Select All …?
Use the package ErgoEmacs Keybinding.
It supports {Open 【Ctrl+o】, Close 【Ctrl+w】, Save 【Ctrl+s】, New 【Ctrl+n】, Select All 【Ctrl+a】, …}.
How to have redo?
You need to install a redo mode. I recommend redo.el by Kyle E Jones et al, or redo+.el, or UndoTree. Any one of them will do the job. 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
How to make the copy key copy the current line when there's no selection?
How to have emacs highlight text selections?
Put this in your emacs init file:
(transient-mark-mode 1) ; highlight text selection (delete-selection-mode 1) ; delete seleted text when typing
Note: starting with Emacs 23.2, it 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 starting with emacs 22. If not on, put the following in your emacs init file:
(global-font-lock-mode 1) ; turn on syntax coloring
How to have matching parenthesis highlighted?
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?
Call hl-line-mode. Call it again to toggle off. Call global-hl-line-mode to toggle globally.
(global-hl-line-mode 1) ; turn on highlighting current line
How to change the default font?
Use the menu: 〖Options▸Set Font〗, then 〖Options▸Save Options〗.
See also: How to Quickly Switch Fonts in Emacs ◇ Best Fonts for Unicode.
How to show line numbers?
You can have line numbers displayed in the left vertical margin. To turn it on, call linum-mode. To have it on by default, put the following in your emacs init file:
(global-linum-mode 1) ; display line numbers in margin. Emacs 23 only.
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)
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) ; keep a list of recently opened files
Once turned on, you can open recently opened files under menu 〖File▸Open Recent〗, or call recentf-open-files.
How to have the down arrow key move by screen lines?
This is default with emacs 23. For detail on how to turn it on/off, see: New Features in Emacs 23.
For emacs 22, there's no robust solution. See: emacswiki.org move by visible line.
How to have lines soft wrapped at word boundary?
Pull the menu 〖Options▸Line Wrapping in this Buffer〗, or call visual-line-mode.
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.
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〗. Or, call toggle-truncate-lines.
How to set the spacing between lines?
Call setq-default, then “line-spacing” with value 5. This means there will be 5 pixels between lines. You can also use a decimal. If you set “line-spacing” to 0.5, then it means there will be a extra 50% of normal line height between lines.
You can put the following elisp code in your emacs init file for easy toggle.
(defun toggle-line-spacing () "Toggle line spacing between no extra space to extra half line height." (interactive) (if (eq line-spacing nil) (setq-default line-spacing 0.5) ; add 0.5 height between lines (setq-default line-spacing nil) ; no extra heigh between lines ) (redraw-display)) (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 switching between programing source code and document/novel reading.
Note that the spacing height between lines also depends on font. See: How to Quickly Switch Fonts in Emacs.
How to reformat paragraphs so that lines are not longer than 70 chars?
Call fill-paragraph 【Alt+q】 to reformat the current block of text your cursor is on. Call fill-region to reformat a selection of text. 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 for display purposes should be avoided. See: The Harm of hard-wrapping Lines.
Is there a unfill-paragraph? I want to delete the hard-wrapped line endings in a paragraph.
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)
2011-11-21 Thanks to Ivan Kozik for a tip.
blog comments powered by Disqus