Emacs常用设置
作者从网上发现了一些使用Emacs时经常需要的设置贴在这里备查。
MS Windows下使用注意事项
中文环境设置
;; for Chinese environment(set-language-environment 'Chinese-GB)
;;(set-keyboard-coding-system 'chinese-iso-8bit-dos)
(set-keyboard-coding-system 'euc-cn)
(set-clipboard-coding-system 'euc-cn)
(set-terminal-coding-system 'euc-cn)
(set-buffer-file-coding-system 'euc-cn)
(set-selection-coding-system 'euc-cn)
(modify-coding-system-alist 'process "*" 'euc-cn)
(setq default-process-coding-system
'(euc-cn . euc-cn))
(setq-default pathname-coding-system 'euc-cn)
标记开始命令
在中文Windows中,Ctrl+Space已被用作切换输入法,不能再作为Emacs的标记切换命令,所以定义Ctrl+2作为标记切换命令。;; for using C-2 as marking command
(global-set-key (quote [67108914]) (quote set-mark-command))
使用RCS进行版本控制
下载Windows用的RCS的执行文件放到Windows路径可以查到的地方,比如Emacs安装的执行文件目录bin。然后在控制面板-系统-高级-环境变量那里设置环境变量TZ(时区),如TZ=GMT8。
调用ESS编辑SAS, R程序
;; For R and SAS mode
(load "ess-site")
Python编辑模式
;; for python mode
(setq auto-mode-alist
(cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
(cons '("python" . python-mode)
interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)
Matlab编辑模式
;; for matlab mode
(add-to-list 'load-path "e:/matlab7/java/extern/EmacsLink/lisp")
(autoload 'matlab-eei-connect "matlab-eei"
"Connects Emacs to MATLAB's external editor interface.")
(autoload 'matlab-mode "matlab" "Enter Matlab mode." t)
(setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist))
(autoload 'matlab-shell "matlab" "Interactive Matlab mode." t)
(setq matlab-indent-function t) ; if you want function bodies indented
(setq matlab-verify-on-save-flag nil) ; turn off auto-verify on save
(defun my-matlab-mode-hook ()
(setq fill-column 76)
(imenu-add-to-menubar "Find")) ; where auto-fill should wrap
(add-hook 'matlab-mode-hook 'my-matlab-mode-hook)
RST文本编辑
;; For reStructuredText.
(require 'rst)
(add-hook 'text-mode-hook 'rst-text-mode-bindings)
(setq auto-mode-alist
(append '(("\\.rst$" . rst-mode)
("\\.rest$" . rst-mode)) auto-mode-alist))
彩色语法内容
可以用不同彩色显示程序的不同部分。;; for global font lock mode
(global-font-lock-mode t)
临时标记模式
可以使标记的区域不总是高亮显示。
;; transient mark mode
(setq transient-mark-mode t)
显示匹配括号
输入右括号时显示匹配的左括号。;; show paren mode
(show-paren-mode t)
提供最近访问文件信息
;; For recent files feature
(require 'session)
(setq session-save-file "c:\\.emacsd\\.session")
(add-hook 'after-init-hook 'session-initialize)
用百分号匹配括号
(global-set-key "%" 'match-paren)(defun match-paren (arg)
"Go to the matching parenthesis if on parenthesis otherwise insert %."
(interactive "p")
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1)))))
打印
NT Emacs打印总是不成功。现在只好用一个麻烦的办法: 用命令M-x ps-spool-buffer-with-face可以生成 一个包含了当前缓冲区内容的PS文件,找到这个PS文件对应的缓冲区并把它存为一个PS文件然后再打印这个PS文件。在网上查到了设置用gs直接连接打印的办法但是显示打印完毕却没有任何实际动作。
Using Abbreviations(使用简写)
Emacs can automatically correct your spelling mistake as you type (such as correcting "thier" with "their"), or expand your own abbreviations for full word (such as replacing "Indie" with "Independent"). Emacs can do this when you enable the "Abbrev" minor mode.
Add the following code to your ~/.emacs file to enable the Abbrev minor mode, to load in abbreviations from ~/.abbrev_defs and to save changes you make to the abbreviations table when you exit Emacs.
;; ===== Automatically load abbreviations table =====
;; Note that emacs chooses, by default, the filename
;; "~/.abbrev_defs", so don't try to be too clever
;; by changing its name
(setq-default abbrev-mode t)
(read-abbrev-file "~/.abbrev_defs")
(setq save-abbrevs t)
To display a list of the current abbreviations Emacs uses, enter the command list-abbrevs.
Highlight Current Line(高亮当前行)
To make Emacs highlight the line the curosr is currently on, add the following to your ~/.emacs :
;; ===== Set the highlight current line minor mode =====
;; In every buffer, the line which contains the cursor will be fully
;; highlighted
(global-hl-line-mode 1)
Set Indent Size(设置缩进量)
To set the standard indent size to some value other than default add the following to your ~/.emacs :
;; ===== Set standard indent to 2 rather that 4 ====
(setq standard-indent 2)
Line-by-Line Scrolling(按行滚动)
By default Emacs will scroll the buffer by several lines whenever the cursor goes above or below the current view. The cursor is also returned to the middle-line of the current view.
This can be confusing to work with since the cursor appears to jump around. If you prefer to have the cursor remain at the top or bottom of the screen as scrolling takes place then use:
;; ========== Line by line scrolling ==========
;; This makes the buffer scroll by only a single line when the up or
;; down cursor keys push the cursor (tool-bar-mode) outside the
;; buffer. The standard emacs behaviour is to reposition the cursor in
;; the center of the screen, but this can make the scrolling confusing
(setq scroll-step 1)
Turn Off Tab Character(取消制表符)
To stop Emacs from entering the tab character into your files (when you press the "tab" key) add the following to your ~/.emacs :
;; ===== Turn off tab character =====
;;
;; Emacs normally uses both tabs and spaces to indent lines. If you
;; prefer, all indentation can be made from spaces only. To request this,
;; set `indent-tabs-mode' to `nil'. This is a per-buffer variable;
;; altering the variable affects only the current buffer, but it can be
;; disabled for all buffers.
;;
;; Use (setq ...) to set value locally to a buffer
;; Use (setq-default ...) to set value globally
;;
(setq-default indent-tabs-mode nil)
Enable Wheel-Mouse Scrolling(打开鼠标滚轮)
By default Emacs does not respond to actions of a scroll button on a wheel mouse; however, it can be made to do so with a simple configuration entry:
;; ========== Support Wheel Mouse Scrolling ==========
(mouse-wheel-mode t)
Prevent Backup File Creation(禁止产生备份)
By default Emacs will automatically create backups of your open files (these are the files with the ~ character appended to the filename). Add the following to your ~/.emacs to prevent these backup files from being created :
;; ========== Prevent Emacs from making backup files ==========
(setq make-backup-files nil)
Saving Backup Files to a Specific Directory(指定备份目录)
Backup files can occassionally be usful, so rather than completely disabelling them, Emacs can be configured to place them in a specified directory. Do this by adding the following to your ~/.emacs files:
;; ========== Place Backup Files in Specific Directory ==========
;; Enable backup files.
(setq make-backup-files t)
;; Enable versioning with default values (keep five last versions, I think!)
(setq version-control t)
;; Save all backup file in this directory.
(setq backup-directory-alist (quote ((".*" . "~/.emacs_backups/"))))
Enable Line and Column Numbering(显示行号列号)
Emacs can display the current line and column number on which the cursor currently resides. The numbers appear in the mode-line :
;; ========== Enable Line and Column Numbering ==========
;; Show line-number in the mode line
(line-number-mode 1)
;; Show column-number in the mode line
(column-number-mode 1)
Set Fill Column(设置列宽)
The fill column influences how Emacs justifies paragraphs. For best results choose a value less than 80:
;; ========== Set the fill column ==========
(setq-default fill-column 72)
Enable Auto Fill mode(设置自动填充)
Auto fill is useful when editing text files. Lines are automatically wrapped when the cursor goes beyond the column limit :
;; ===== Turn on Auto Fill mode automatically in all modes =====
;; Auto-fill-mode the the automatic wrapping of lines and insertion of
;; newlines when the cursor goes over the column limit.
;; This should actually turn on auto-fill-mode by default in all major
;; modes. The other way to do this is to turn on the fill for specific modes
;; via hooks.
(setq auto-fill-mode 1)
Treat New Buffers as Text(新缓冲区作为文本)
Specify that new buffers should be treated as text files:
;; ===== Make Text mode the default mode for new buffers =====
(setq default-major-mode 'text-mode)
Set Basic Colours(设置基本颜色)
Emacs does allow the various colours it uses for highlighting code to be configured by the user. However a quick way to set the basic colours used f or all buffers is:
;; ========= Set colours ==========
;; Set cursor and mouse-pointer colours
(set-cursor-color "red")
(set-mouse-color "goldenrod")
;; Set region background colour
(set-face-background 'region "blue")
;; Set emacs background colour
(set-background-color "black")
Delete the Current Line(删除当前行)
In order to provide Emacs with a key for deleting the current line an appropriate delete-line function has to be first defined, and then a key-sequence binding defined to invoke it :
;; ===== Function to delete a line =====
;; First define a variable which will store the previous column position
(defvar previous-column nil "Save the column position")
;; Define the nuke-line function. The line is killed, then the newline
;; character is deleted. The column which the cursor was positioned at is then
;; restored. Because the kill-line function is used, the contents deleted can
;; be later restored by usibackward-delete-char-untabifyng the yank commands.
(defun nuke-line()
"Kill an entire line, including the trailing newline character"
(interactive)
;; Store the current column position, so it can later be restored for a more
;; natural feel to the deletion
(setq previous-column (current-column))
;; Now move to the end of the current line
(end-of-line)
;; Test the length of the line. If it is 0, there is no need for a
;; kill-line. All that happens in this case is that the new-line character
;; is deleted.
(if (= (current-column) 0)
(delete-char 1)
;; This is the 'else' clause. The current line being deleted is not zero
;; in length. First remove the line by moving to its start and then
;; killing, followed by deletion of the newline character, and then
;; finally restoration of the column position.
(progn
(beginning-of-line)
(kill-line)
(delete-char 1)
(move-to-column previous-column))))
;; Now bind the delete line function to the F8 key
(global-set-key [f8] 'nuke-line)
没有评论:
发表评论