Emacsの設定 -機能拡張編-

必要なパッケージのインストール

$ sudo aptitude install migemo

文字列置換 - replace-string、query-replace -

文字列置換です。replace-stringが一括置換処理、query-replaceが毎回置換するかどうかを問い合わせてくる逐次置換処理です。置換処理前に範囲指定しておくことで、置換を行う範囲を限定させることができます。

[~/.emacs]
(global-set-key "\M-r" 'replace-string)
(global-set-key "\M-\C-r" 'query-replace)

先頭から1行分切り取る - kill-line-twice -

Emacsは、範囲指定することなくカーソルから行末まで切り取る機能を持っていますが、このとき、改行文字までは切り取ってくれません。しかし、行頭から行末まで切り取るときには、改行文字まで切り取ってくれた方が便利なことが多いです。これを実現してくれるのが、kill-line-twiceです。カーソル位置が行頭の場合のみ、自動的に改行文字まで切り取ってくれます。

[~/.emacs]
(defun kill-line-twice (&optional numlines)
  "Acts like normal kill except kills entire line if at beginning"
  (interactive "p")
  (cond ((or (= (current-column) 0)
             (> numlines 1))
         (kill-line numlines))
        (t (kill-line))))
(global-set-key "\C-k" 'kill-line-twice)

ファイル内の行末の空白をすべて削除 - delete-trailing-whitespace -

ファイル編集が何度も続くと、行末に余分な空白が入っていることがよくあります。そんな空白文字をすべて削除してくれるのが、delete-trailing-whitespaceです。

[~/.emacs]
(global-set-key "\M-s" 'delete-trailing-whitespace)

コメントアウトコメントアウト解除 - comment-region、uncomment-region -

範囲指定領域を一括でコメントアウトできます。逆に、範囲指定領域を一括でコメントアウト解除もできます。適用可能なファイルは、C/C++ファイルだけでなく、javarubypythonperlemacs-lispなど応用範囲は広いです。

[~/.emacs]
(global-set-key "\C-c;" 'comment-region)
(global-set-key "\C-c:" 'uncomment-region)

書き込み可・不可の切り換え - toggle-read-only -

多数のファイルを開いている際、編集対象ではないファイルを誤って編集してしまうことがあります。そんなミスを回避してくれるのが、toggle-read-onlyです。コマンドを実行する度に、書き込み可・不可状態を切り換えることができます。これと同様の機能として、書き込み不可状態でファイルを開くfind-file-read-onlyがあります。

[~/.emacs]
(global-set-key "\C-x\C-q" 'toggle-read-only)

指定行にジャンプ - goto-line -

コンパイルエラーの際、エラーの発生した行をコンパイラから受け取ることはよくあります。そんなときに即座に指定行にジャンプできるのが、goto-lineです。デバッグの際、大活躍します。

[~/.emacs]
(global-set-key "\M-g" 'goto-line)

elファイルをその場で評価 - eval-buffer -

編集した.emacsなどのelファイルをその場で評価します。これにより、Emacsを再起動することなく、その場で変更した設定を反映させることができます。

[~/.emacs]
(global-set-key "\C-x\C-e" 'eval-buffer)

マウスホイールでバッファをスクロール

[~/.emacs]
;; スクロールステップを 1 に設定
(setq scroll-step 1)
;; マウスホイールでスクロール
(defun scroll-down-with-lines ()
  ""
  (interactive)
  (scroll-down 5))
(defun scroll-up-with-lines ()
  ""
  (interactive)
  (scroll-up 5))
(global-set-key [mouse-4] 'scroll-down-with-lines)
(global-set-key [mouse-5] 'scroll-up-with-lines)
;; (define-key global-map [mouse-4] 'scroll-down)
;; (define-key global-map [mouse-5] 'scroll-up)
;; マウスカーソルがあるバッファをスクロール
(mwheel-install)
(setq mouse-wheel-follow-mouse t)

辞書検索

[~/.emacs]
;; sdic
(global-set-key "\C-cw" 'sdic-describe-word)
(global-set-key "\C-cW" 'sdic-describe-word-at-point)

;; look up
(define-key ctl-x-map "l" 'lookup)            ; C-x l   - lookup
(define-key ctl-x-map "y" 'lookup-region)     ; C-x y   - lookup-region
(define-key ctl-x-map "\C-y" 'lookup-pattern) ; C-x C-y - lookup-pattern
(setq lookup-search-agents '((ndic "/usr/share/dict")
                             ))
[~/.emacs]
[~/.emacs]
;; migemo: 日本語検索
;;; C-s, C-r: migemo isearch
;;; C-u C-s, C-u C-r: isearch
(require 'migemo)
(setq migemo-use-pattern-alist t)          ; キャッシュを利用
(setq migemo-pattern-alist-length 1000)    ; 履歴の数
(setq migemo-use-frequent-pattern-alist t) ; 頻出パターンをキャッシュ
(make-face 'migemo-message-prefix)         ; prefix の色
(face-spec-set 'migemo-message-prefix
               '((t (:foreground "gray"))))
(setq migemo-message-prefix-face 'migemo-message-prefix)
(migemo-init)                              ; 初期化

;; browse-yank: 過去の kill-ring の内容を表示しながら貼付
;;; p: previous
;;; n: next
;;; i: insert
(require 'browse-yank)
(global-set-key "\M-y" 'browse-yank)

;; undo
(global-set-key "\C-^" 'undo)
;; redo: undo のキャンセル
(require 'redo)
(global-set-key "\M-^" 'redo)

;; ibuffer: buffer-list の機能強化
(require 'ibuffer)
;;; ibuffer 中で buffer のスクロールを可能にする
(defun ibuffer-visit-buffer-other-window-scroll (&optional down)
  (interactive)
  (let ((buf (ibuffer-current-buffer)))
    (unless (buffer-live-p buf)
      (error "Buffer %s has been killed!" buf))
    (if (string=
         (buffer-name (window-buffer (next-window)))
         (buffer-name buf))
        (if down
            (scroll-other-window-down nil)
          (scroll-other-window))
      (ibuffer-visit-buffer-other-window-noselect))))
(defun ibuffer-visit-buffer-other-window-scroll-down ()
  (interactive)
  (ibuffer-visit-buffer-other-window-scroll t))
(define-key ibuffer-mode-map " " 'ibuffer-visit-buffer-other-window-scroll)
(define-key ibuffer-mode-map "b" 'ibuffer-visit-buffer-other-window-scroll-down)
;;; n, p で次 (前) のバッファの内容を表示する
(defadvice ibuffer-forward-line
  (after ibuffer-scroll-page activate)
  (ibuffer-visit-buffer-other-window-scroll))
(defadvice ibuffer-backward-line
  (after ibuffer-scroll-page-down activate)
  (ibuffer-visit-buffer-other-window-scroll-down))
(global-set-key "\C-x\C-b" 'ibuffer)

;; iswitchb: buffer をより容易に切り換え
;;; C-x b でバッファ検索中 C-s or C-r
(iswitchb-default-keybindings)
;;; バッファを表示させながら切り換え候補を検索
(defadvice iswitchb-exhibit
  (after
   iswitchb-exhibit-with-display-buffer
   activate)
  "display the selected buffer in the window"
  (when (and
         (eq iswitchb-method iswitchb-default-method)
         iswitchb-matches)
    (select-window
     (get-buffer-window (cadr (buffer-list))))
    (let ((iswitchb-method 'samewindow))
      (iswitchb-visit-buffer
       (get-buffer (car iswitchb-matches))))
    (select-window (minibuffer-window))))

;; mcomplete: mini-buffer で容易に補完
;;; mini-buffer 入力中で C-s or C-r
;;; さらに C-p or C-n で Prefix/Substring match の切り換え
(require 'mcomplete)
;; mcomplete-history: mcomplete 補完の履歴から候補を絞る
;;; C-p or C-n で History match に切り換え可能
(require 'cl)
(load "mcomplete-history")
(turn-on-mcomplete-mode)

;; recentf-mode: 最近開いたファイルを一覧から選択
(recentf-mode 1)
(global-set-key "\C-xf" 'recentf-open-files)

;; isearch-buffer: すべての buffer に対して isearch
;;; C-s 中に C-p or C-n
;;;(require 'isearch-all)

;; semantic: メンバ名補完
;;;(setq semantic-load-turn-everything-on t)
;;;(require 'semantic-load)

;; upcate-region and downcase-region
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)

;; word-count
(autoload 'word-count-mode "word-count" "Minor mode to count words." t nil)
(global-set-key "\M-+" 'word-count-mode)

;; wanderlust
;; (autoload 'wl "wl" "Wanderlust" t)

その他、ショートカットしてあると便利な機能

[~/.emacs]
(global-set-key [delete] 'delete-char)
;; (global-set-key "\C-h" 'delete-backward-char)
(global-set-key "\M-?" 'help-for-help)