论坛: UNIX系统 标题: 贴几个emacs编辑技巧 复制本贴地址    
作者: amr [amr]    论坛用户   登录
代码:

;;这几个来自Textmate

;;默认COPY,CUT整行
(defadvice kill-ring-save (before slickcopy activate compile)
"When called interactively with no active region, copy a single line instead."
(interactive
  (if mark-active (list (region-beginning) (region-end))
    (list (line-beginning-position)
  (line-beginning-position 2)))))
(defadvice kill-region (before slickcut activate compile)
  "When called interactively with no active region, kill a single line instead."
  (interactive
  (if mark-active (list (region-beginning) (region-end))
    (list (line-beginning-position)
  (line-beginning-position 2)))))

;;删除语法快
(global-set-key [(control w)] 'kill-syntax-backward)
(global-set-key [(control d)] 'kill-syntax-forward)

(defun kill-syntax-forward ()
  "Kill characters with syntax at point."
  (interactive)
  (kill-region (point)
              (progn (skip-syntax-forward (string (char-syntax (char-after))))
                      (point))))

(defun kill-syntax-backward ()
  "Kill characters with syntax at point."
  (interactive)
  (kill-region (point)
              (progn (skip-syntax-backward (string (char-syntax (char-before))))
                      (point))))


;;可以给选中的region加个外包比如"region"
(require 'wrap-region)

;;移动整行
(global-set-key [(meta up)] 'move-line-up)
(global-set-key [(meta down)] 'move-line-down)

(defun move-line (&optional n)
  "Move current line N (1) lines up/down leaving point in place."
    (interactive "p")
    (when (null n)
      (setq n 1))
    (let ((col (current-column)))
      (beginning-of-line)
      (next-line 1)
      (transpose-lines n)
      (previous-line 1)
      (forward-char col)))
(defun move-line-up (n)
  "Moves current line N (1) lines up leaving point in place."
  (interactive "p")
  (move-line (if (null n) -1 (- n))))

(defun move-line-down (n)
  "Moves current line N (1) lines down leaving point in place."
  (interactive "p")
  (move-line (if (null n) 1 n)))


;;这几个来自VIM

;;括号匹配,当 % 在括号上按下时,那么匹配括号,否则输入一个 %。
(global-set-key "%" 'match-paren)
         
(defun match-paren (arg)
  "Go to the matching paren if on a paren; 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)))))


;;go-to-char,C-z a
(defun wy-go-to-char (n char)
  "Move forward to Nth occurence of CHAR.
Typing `wy-go-to-char-key' again will move forwad to the next Nth
occurence of CHAR."
  (interactive "p\ncGo to char: ")
  (search-forward (string char) nil nil n)
  (while (char-equal (read-char)
    char)
    (search-forward (string char) nil nil n))
  (setq unread-command-events (list last-input-event)))

(define-key global-map (kbd "C-z a") 'wy-go-to-char)




;; 先用 highlight-regexp,再用 hi-lock-next 移动到
;; 下一个匹配。比较类似 vim 的 n 和 N。
(global-set-key (kbd "C-z h") 'highlight-regexp)
(global-set-key (kbd "C-z n") 'hi-lock-next)
(global-set-key (kbd "C-z p") 'hi-lock-previous)
;; view-mode 是用于浏览文件的一个很好用的 mode。可以加一些 vim normal
(global-set-key (kbd "C-z vi") 'view-mode)
;; 为 view-mode 加入 vim 的按键。
(setq view-mode-hook
      (lambda ()
        (define-key view-mode-map "h" 'backward-char)
        (define-key view-mode-map "l" 'forward-char)
        (define-key view-mode-map "j" 'next-line)
        (define-key view-mode-map "k" 'previous-line)))
;; 自动导入hi-lock+ 插件。autoload 的用处在于它不是启动时导入这
;; 个 elisp,而是在你调用这个命令时才导入。这样可以加快启动速度。你也可
;; 以类似的加入一些要自动导入的命令。
(dolist (cm '(hi-lock-previous
              hi-lock-next
              toggle-tabs-font-lock
              toggle-trailing-whitespace-font-lock
              toggle-whitespace-font-lock))
  (autoload cm "hi-lock+" "Hi-lock assist command" t))

;;ann77 的一个 elisp,用于跳转到前一个大幅度跳转的位置。类似于 vim 的 C-o 和 C-i。
(require 'recent-jump nil t)
(when (featurep 'recent-jump)
  (global-set-key (kbd "M-o") 'recent-jump-jump-backward)
  (global-set-key (kbd "M-i") 'recent-jump-jump-forward))   


;;类似 vim 的字典
(setq ywb-dict-file "~/.emacs.d/elisp/my.dict")
(defun ywb-read-dict-file ()
  "Read dictionary file"
  (interactive)
  (if (file-exists-p ywb-dict-file)
      (save-excursion
        (let ((buffer (find-file-noselect ywb-dict-file))
              (done nil)
              mode beg end)
          (set-buffer buffer)
          (goto-char (point-min))
          (re-search-forward "^\\*\\s-*\\(.*-mode\\)" nil t)
          (setq mode (buffer-substring (match-beginning 1)
                                      (match-end 1)))
          (setq beg (1+ (match-end 0)))
          (while (progn
                  (if (re-search-forward "^\\*\\s-*\\(.*-mode\\)" nil t)
                      (setq end (match-beginning 0))
                    (setq end (point-max))
                    (setq done t))
                  ;; (message "mode: %s, beg: %d, end: %d" mode beg end)
                  (with-current-buffer  (get-buffer-create (format " %s-dict" mode))
                    (erase-buffer)
                    (setq major-mode (intern mode))
                    (insert-buffer-substring buffer beg end))
                  (setq mode (match-string 1))
                  (setq beg (1+ (match-end 0)))
                  (not done)))
          (kill-buffer buffer)))
    (message "file %s is not exits!" ywb-dict-file)))
(ywb-read-dict-file)







[此贴被 尤里西斯(amr) 在 04月18日19时51分 编辑过]

地主 发表时间: 08-04-18 19:49

回复: amr [amr]   论坛用户   登录
代码:

;;align-regexp,按字符列对齐
(global-set-key (kbd "C-z q") 'align-regexp)
;;设置标记
(global-set-key (kbd "M-<SPC>") 'set-mark-command)

;;代码折叠
;(add-hook 'python-mode-hook 'hs-minor-mode)
;;(add-hook 'javascript-mode-hook 'hs-minor-mode)

;;是自动缩进
(mapcar
(lambda (mode)
  (let ((mode-hook (intern (concat (symbol-name mode) "-hook")))
        (mode-map (intern (concat (symbol-name mode) "-map"))))
  (add-hook mode-hook
  `(lambda nil
      (local-set-key
      (kbd "RET")
      (or (lookup-key ,mode-map "\C-j")
          (lookup-key global-map "\C-j")))))))
'(c-mode c++-mode cperl-mode emacs-lisp-mode java-mode html-mode lisp-mode ruby-mode sh-mode))

;;格式化整个文件
(defun iwb ()
  "indent whole buffer"
  (interactive) (delete-trailing-whitespace) (indent-region (point-min) (point-max) nil))
(global-set-key (kbd "C-c 2" ) 'iwb)
;;块状区域选择
(require 'rect-mark)
(define-key ctl-x-map "r\C-@" 'rm-set-mark)
(define-key ctl-x-map [?r ?\C-\ ] 'rm-set-mark)
(define-key ctl-x-map "r\C-x" 'rm-exchange-point-and-mark)
(define-key ctl-x-map "r\C-w" 'rm-kill-region)
(define-key ctl-x-map "r\M-w" 'rm-kill-ring-save)
(define-key global-map [S-down-mouse-2] 'rm-mouse-drag-region)
(autoload 'rm-set-mark "rect-mark"
  "Set mark for rectangle." t)
(autoload 'rm-exchange-point-and-mark "rect-mark"
  "Exchange point and mark for rectangle." t)
(autoload 'rm-kill-region "rect-mark"
  "Kill a rectangular region and save it in the kill ring." t)
(autoload 'rm-kill-ring-save "rect-mark"
  "Copy a rectangular region to the kill ring." t)
(autoload 'rm-mouse-drag-region "rect-mark"
  "Drag out a rectangular region with the mouse." t)


 
;; 我经常用 *scratch*,临时放些文本、写一些函数之类。所以专门写了一个命令
(global-set-key (kbd "C-c b") 'ywb-create/switch-scratch)
(defun ywb-create/switch-scratch ()
  (interactive)
  (let ((buf (get-buffer "*scratch*")))
    (switch-to-buffer (get-buffer-create "*scratch*"))
    (when (null buf)
      (lisp-interaction-mode))))

;;该配对的配对
(setq skeleton-pair t)
(global-set-key (kbd "[") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "(") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "{") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "<") 'skeleton-pair-insert-maybe)

;;以当前单词开始搜索,准确说是移动
(global-set-key (kbd "C-<f1>") 'isearch-backward-current-word-keep-offset)
(global-set-key (kbd "C-<f2>") 'isearch-forward-current-word-keep-offset)

;;M-j重新定义
;;(global-set-key (kbd "M-J" 'indent-new-comment-line)
;;补全hippie
(global-set-key (kbd "M-j") 'hippie-expand)
(setq hippie-expand-try-functions-list
      '(try-expand-dabbrev
try-expand-dabbrev-visible
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-complete-file-name-partially
try-complete-file-name
try-expand-all-abbrevs
try-expand-list
try-expand-line
try-complete-lisp-symbol-partially
try-complete-lisp-symbol))

;; 补全文件名。由于 hippie-expand 主要用于补全单词,补全文件名是很困难
;; 的,所以单独写了一个命令用于补全文件名。
(global-set-key (kbd "C-c f") 'ywb-hippie-expand-filename)
(defun ywb-hippie-expand-filename ()
  (interactive)
  (let ((hippie-expand-try-functions-list
        '(try-complete-file-name try-complete-file-name-partially)))
    (call-interactively 'hippie-expand)))


;;用相关命令对选中块求值并返回结果
;;evaluate a region via a shell command and replace the region
;; with the resulting output.you would access this command via C-u M-| 

(global-set-key (kbd "<f12>") 'custom-shell-command-on-region)

(defun custom-shell-command-on-region nil
  "Replace region with ``shell-command-on-region''.
By default, this will make mark active if it is not and then
prompt you for a shell command to run and replaces region with
the results.  This is handy for doing things like getting
external program locations in scripts and running grep and
whatnot on a region."
  (interactive)
  (save-excursion
    (if (equal mark-active nil)
        (push-mark nil nil -1))
    (setq string
          (read-from-minibuffer "Shell command on region: " nil nil nil
                                'shell-command-history))
    (shell-command-on-region (region-beginning) (region-end) string -1)
    ; Get rid of final newline cause I normally did by hand anyway.
    (delete-char -1)))


;; goto-line
(global-set-key (kbd "M-g g") 'ywb-goto-line)
(defun ywb-goto-line (percent)
  (interactive (list (or current-prefix-arg
                        (string-to-number
                          (read-from-minibuffer "Goto percent: ")))))
  (let* ((total (count-lines (point-min) (point-max)))
        (num (round (* (/ total 100.0) percent))))
    (goto-line num)))

;;Bookmarking - history stack,跳来跳去
(global-set-key (kbd "C-<f11>") 'point-stack-push)
(global-set-key (kbd "C-<f12>") 'point-stack-pop)

(defvar point-stack nil)

(defun point-stack-push ()
  "Push current location and buffer info onto stack."
  (interactive)
  (message "Location marked.")
  (setq point-stack (cons (list (current-buffer) (point)) point-stack)))

(defun point-stack-pop ()
  "Pop a location off the stack and move to buffer"
  (interactive)
  (if (null point-stack)
      (message "Stack is empty.")
    (switch-to-buffer (caar point-stack))
    (goto-char (cadar point-stack))
    (setq point-stack (cdr point-stack))))


;;按块缩进,就是 []{}() 这些来重排代码
(defun ywb-indent-accoding-to-paren ()
  "Indent the region between paren"
  (interactive)
  (let ((prev-char (char-to-string (preceding-char)))
        (next-char (char-to-string (following-char)))
        (pos (point)))
    (save-excursion
      (cond ((string-match "[[{(<]" next-char)
            (indent-region pos (progn (forward-sexp 1) (point)) nil))
            ((string-match "[\]})>]" prev-char)
            (indent-region (progn (backward-sexp 1) (point)) pos nil))))))




[此贴被 尤里西斯(amr) 在 04月18日19时55分 编辑过]

B1层 发表时间: 08-04-18 19:53

回复: amr [amr]   论坛用户   登录



[此贴被 尤里西斯(amr) 在 04月18日20时07分 编辑过]

B2层 发表时间: 08-04-18 20:02

回复: amr [amr]   论坛用户   登录



[此贴被 尤里西斯(amr) 在 04月18日20时07分 编辑过]

B3层 发表时间: 08-04-18 20:03

论坛: UNIX系统

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号