Xah Lee, 2006, 2007
How to have emacs highlight selections?
Use the menu “Options‣Active Region Highlighting”, then “Options‣Save Options”. The equivalent command is transient-mark-mode. To have it always on, put this code: “(transient-mark-mode t)” in your emacs customization file. (“~/.emacs”)
How to turn on syntax-coloring in emacs?
Use the menu “Options‣Syntax Highlighting”. If you always want this feature, then do “Options‣Save Options”. To turn it for the current file only, do “Alt+x font-lock-fontify-buffer”.
How to change the default font?
Use the menu: “Options‣Set Font”, then “Options‣Save Options”.
How to have emacs display line numbers?
“Alt+x line-number-mode”. To always have it on, place the following in your emacs customization file:
(defun my-turn-on-line-number-mode () (line-number-mode 1)) (add-hook 'text-mode-hook 'my-turn-on-line-number-mode)
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” file: “(column-number-mode t)”.
How to set a margin on the right?
Use “Alt+x longlines-mode”, available in Emacs version 22. When this mode is on, lines will be wrapped at 72 chars length. To adjust the this margin, use “Alt+x set-variable”, and set fill-column a desired value.
To always have longlines-mode mode on, put in your emacs customization file: “(add-hook 'text-mode-hook (lambda () (longlines-mode t)))”.
Note: as of emacs 22 (released in mid 2007), longlines-mode is buggy, especially when you paste text.
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”. To do this without GUI, type: “Alt+set-variable RET truncate-lines RET nil”. To get it back, set truncate-lines to t.
You can put the following code in your emacs customization file (~/.emacs) so that pressing the F7 key will toggle the state.
(defun toggle-truncate-lines () "Toggle the variable truncate-lines between true and false" (interactive) (if (eq truncate-lines 't) (set-variable 'truncate-lines nil) (set-variable 'truncate-lines 't) ) ) (global-set-key (kbd "<f7>") 'toggle-truncate-lines)
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 customization 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.
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)
Related essays:
Page created: 2007-08. © 2007 by Xah Lee.