在Emacs 中开发Python --编辑器相关

CPUG联盟::

CPUG::门户plone

BPUG

SPUG

ZPUG

SpreadPython Python宣传

1. 用 Emacs 22 编写 Python 最初级指南

{{{Ben Luo <[email protected]> hide details 1:55 pm (25 minutes ago)

}}}

1.1. 缘起

因为要 ssh 做一些 server 管理的工作,平时又对编程有一点兴趣,公司的笔记本只能装 Windows. 所以要找一个可以在 GNU/Linux console 和 Windows 下面都可以用的编辑器。 原来是用 vim, 后来发现 Emacs 上有一个 nxml-mode 对 xml, xhmtl 的支持不错。所以转向 Emacs。 有很多人都说emacs难用,我觉得用多了还是很方便的。下面给一个非常基础的入门。

1.2. 配置 Emacs

1.2.1. Windows 下

在 My Computer 下设置 $HOME 参数。我的是 d:\home 将下面的配置保存到 d:\home\.emacs 中

我的 .emacs 文件

;; .emacs

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(column-number-mode t)
 '(cua-mode nil nil (cua-base))
 '(current-language-environment "UTF-8")
 '(default-directory "~/" t)
 '(display-time-24hr-format t)
 '(frame-title-format "%b" t)
 '(global-font-lock-mode t nil (font-core))
 '(prefer-coding-system (quote utf-8-unix))
 '(rcirc-default-nick "benluo")
 '(safe-local-variable-values (quote ((TeX-master . t))))
 '(senator-highlight-found t)
 '(show-paren-mode t)
 '(transient-mark-mode t))

(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )


;; Set load path
(setq load-path (append load-path '("~/.emacs.d/mmm-mode")))
(setq load-path (append load-path '("~/.emacs.d/cedet")))
(setq load-path (append load-path '("~/.emacs.d")))

;;set 下面的这个设置可以让光标指到某个括号的时候显示与它匹配的括号
(show-paren-mode t)
(setq show-paren-style 'parentheses)

;(utf-translate-cjk-mode)
(set-buffer-file-coding-system 'utf-8)

;; deny tool bar
(tool-bar-mode -1)

;; deny temple file
(setq-default make-backup-files nil)

;; Chinese Font set up

(if (not (member '("-outline-Bitstream Vera Sans Mono-normal-r-normal-normal-14-*-96-96-*-*-fontset-chinese"
   . "fontset-chinese") fontset-alias-alist))
   (progn
     (create-fontset-from-fontset-spec
      "-*-Bitstream Vera Sans Mono-normal-r-normal-normal-14-*-96-96-*-*-fontset-chinese,
     chinese-gb2312:-outline-Arial Unicode MS-normal-r-normal-normal-*-*-96-96-*-*-gb2312*-*,
     chinese-big5-1:-outline-Arial Unicode MS-normal-r-normal-normal-*-*-96-96-*-*-big5*-*,
     chinese-big5-2:-outline-Arial Unicode MS-normal-r-normal-normal-*-*-96-96-*-*-big5*-*" t)
      (setq default-frame-alist
            (append
             '((font . "fontset-chinese"))
             default-frame-alist))
      )
)

;; recent file
(require 'recentf)
(recentf-mode 1)

;; Close Emacs start up dialuege
(setq inhibit-startup-message t)

;; set default major mode to text-mode
(setq default-major-mode 'text-mode)

;; set up user information
(setq user-full-name "Ben Luo")
(setq user-mail-address " [email protected]")

;; syantax highlight
(global-font-lock-mode t)

;; Make sure nxml-mode can autoload
(load "~/.emacs.d/nxml-mode/rng-auto.el")

;;--
;; Load nxml-mode for files ending in .xml, .xsl, .rng, .xhtml, .html
;;--
(autoload 'nxml-mode "nxml-mode" "xml editing mode" t)
(setq auto-mode-alist
     (cons '("\\.\\(xml\\|xsl\\|rng\\|xhtml\\|html\\|htm\\)\\'" . nxml-mode)
       auto-mode-alist))
(add-to-list 'auto-mode-alist '("\\.html\\'" . nxml-mode))
(add-to-list 'auto-mode-alist '("\\.jsp\\'" . genricx))
(add-to-list 'auto-mode-alist '("\\.xml\\'" . nxml-mode))
 
;; tarbar
(require 'tabbar)
(tabbar-mode)
(global-set-key (kbd "") 'tabbar-backward-group)
(global-set-key (kbd "") 'tabbar-forward-group)
(global-set-key (kbd "") 'tabbar-backward)
(global-set-key (kbd "") 'tabbar-forward)

;; color-theme-aliceblue start
(require 'color-theme)
(color-theme-deep-blue)

;;(global-hl-line-mode 1)

;; complete switch buffer name
(require 'iswitchb)
(iswitchb-default-keybindings)

;;po-mode
(require 'po-mode)

;; pabbrev mode.
;;
(require 'pabbrev)
(setq pabbrev-idle-timer-verbose nil)

;;set type y instead of yes
(fset 'yes-or-no-p 'y-or-n-p)

;; End of .emacs

nxml-mode 大家可以在网上下载。

1.2.2. GNU/Linux 下

将上面的配置保存到 ~\.emacs 中。中文字体的设置与上面的不一样。

1.3. 使用 Emacs 基础

1.3.1. 打开文件

ctr-x 然后 ctr-f

1.3.2. 关闭文件

ctr-x 然后 k

1.3.3. 关闭 Emacs

ctr-x 然后 ctr-c

1.3.4. 保存文件

ctr-x 然后 ctr-s

1.3.5. 另存文件为

ctr-x 然后 ctr-w

1.3.6. 选择

ctr-@ 标记开始,然后用键盘移动光标到你想要的位置。 或者 用鼠标选择

1.3.7. 复制

alt-w

1.3.8. 剪切

ctr-w

1.3.9. 粘贴

ctr-y

1.3.10. 撤消

ctr-_

1.3.11. 移动到行头

ctr-a

1.3.12. 移动到行尾

ctr-e

1.3.13. 多个打开的文件间切换

ctr-x 然后输入 b, 然后输入你要切换过去的名字可以用"TAB"键补全

1.3.14. 分成上下两个窗口

ctr-x 然后输入 2

1.3.15. 分成左右两个窗口

ctr-x 然后输入 3

1.3.16. 两个窗口合并成一个窗口(不是内容合并)

ctr-x 然后输入 0

1.4. 为 python 使用 Emacs

1.4.1. 打开shell or command 窗口

alt-x 然后输入 shell

1.4.2. 打开python console

在python菜单中选择"start interpreter"

1.4.3. 缩进

在一行的任意地方用"TAB"键

1.4.4. 一次为多行做注释/反注释

注释,C-shift-2,然后选择多行,C-; 反注释,同上。

这些就是我日常用emacs写python的代码了。

1.4.5. 还可以用 Emacs 干什么

  1. 我写一点 html
  2. 用irc聊天
  3. 传说,除了无法帮人修理自行车,Emacs 可以作一切! ;-)

2. 反馈

修改 -- flyinflash [2010-01-11]

EmacsPy (last edited 2010-01-11 17:25:32 by flyinflash)