[Emacs]
Emacsの設定
Emacsカスタマイズ
Emacsに便利なelispを追加して作業を効率化しよう
事前知識
- バイトコンパイル
- elispコードをコンパイルして実行速度を上げること
- .emacs
- Emacsカスタマイズ用の設定ファイル、下記の設定はすべて.emacsに記述する
- elispのパッケージ管理
- 今のところpackage.el, Cask, el-getがある。私はpackage.elを使ってる。機会があればel-getとかに乗り換えたい。
CUIで作業する場合
お使いの環境がGUIでない場合、以下のようにTERMの設定を変えると色がいろいろ使えるようになる。
$ cat ~/.bashrc # color export TERM="xterm-256color"
auto-install
- auto-installは、ネット上で配布されているelispファイルを半自動でバイトコンパイル&インストールできるすぐれもの。最初にこれをインストールできていれば後が楽です。
- ただしEmacs24以降はpackage.elが標準でインストールされているので、パッケージ管理的な用途ではあまり使用しない
インストール手順
$ mkdir ~/.emacs.d/auto-install $ cd ~/.emacs.d/auto-install $ wget http://www.emacswiki.org/emacs/download/auto-install.el $ emacs --batch -Q -f batch-byte-compile auto-install.el
上記が完了したら、以下のコードを.emacsに追記する
;; auto-install (add-to-list 'load-path "~/.emacs.d/auto-install") (require 'auto-install) (auto-install-update-emacswiki-package-name t) (auto-install-compatibility-setup) (setq ediff-window-setup-function 'ediff-setup-windows-plain)
以上を行った後で「C-x C-c」でEmacsを終了。再起動すれば「M-x auto-install」でauto-installの機能が使えるようになる。「M-x auto-install-from-emacswiki」でemacswiki上にあるelispコードをダウンロードできる。正常にファイルがダウンロードできたら「C-c C-c」でバイトコンパイル&インストール。
package
packageはEmacsのためのelisp管理用ツール。auto-installよりも良いかもしれない。
emacs23以前
emacs23以前はcl-libを先にインストールする
// emacs23にはcl-libが入っていないので手動で入れる // Emacs上で以下のようにして、ソースが入ったらC-c $ auto-install-from-url: https://raw.github.com/emacsmirror/cl-lib/master/cl-lib.el
package.elをインストールする
// 以下の文字列をスクラッチバッファでC-jして評価 // うまいことはいったらC-c(elispをコンパイル&インストール) (auto-install-from-url "http://repo.or.cz/w/emacs.git/blob_plain/1a0a666f941c99882093d7bd08ced15033bc3f0c:/lisp/emacs-lisp/package.el")
終わったら以下のelispを.emacsに追加する
; Add package-archives (require 'package) (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t) (add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/")) ; Initialize (package-initialize) (when (< emacs-major-version 24) ;; For important compatibility libraries like cl-lib (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))) (package-initialize)
「M-x package-list-packages」… packageのリポジトリから取得できるelispを一覧表示する
「M-x package-install <package-name>」… 実際にパッケージをEmacsにインストールする
package.elでのパッケージ管理
- パッケージが事前に入っていない環境でもEmacs使いたいとき役に立つのが以下の構文
- もし指定したパッケージが入っていなければ、自動でインストールしてくれる
(unless (package-installed-p 'magit) (package-refresh-contents) (package-install 'magit))
tabbar
;; タブを使う ;; http://www.emacswiki.org/emacs/tabbar.el (require 'tabbar) (global-set-key [(control shift tab)] 'tabbar-backward) (global-set-key [(control tab)] 'tabbar-forward) (tabbar-mode)
emacswikiからtabbar.elをインストール。eclipseやVisualStudioのようなタブ表示が高速に実現できる。Mac OSXの場合、キー配置を変えないと動かせないことに注意。
tabbar+
;; tabbar+ ;; https://gist.github.com/Johniel/4324127#file-tabbar-el (require 'tabbar+)
package.elを使って先にdashをインストールしておく。「M-x package-install dash」
auto-install-from-gistでtabbar+.elをインストール。chromeやfirefoxのように表示しているタブをすべて閉じたり、これより右を閉じるという操作が可能。
「M-x tabbar+remove-left」 … これより左を閉じる
「M-x tabbar+remove-right」… これより右を閉じる
redo+
;; リドゥー設定 ;; redoできるようにする ;; http://www.emacswiki.org/emacs/redo+.el (when (require 'redo+ nil t) (define-key global-map (kbd "C-z") 'redo))
emacswikiからredo+.elをインストール。やり直しがC-zで実現できる。逆はC-/。
→ Emacs24ではredo+が使えなくなった…
linum
;; 行数を表示させる (require 'linum) (global-linum-mode)
最近のemacsだと標準で入ってるはず。行数表示。XがないEmacs環境の場合nlinumを使うといいだろう。
delete-trailing-whitespace
;; 保存時に末尾の余分な空白を削除する ;; https://www.emacswiki.org/emacs/DeletingWhitespace (add-hook 'before-save-hook 'delete-trailing-whitespace)
cua
(cua-mode t) (setq cua-enable-cua-keys nil)
矩形範囲選択ができる。かなり便利。作業効率が非常に上がるのでおすすめです。起動は「C-Enter」。
magit
(require 'magit)
gitの各操作をすべてEmacsから実行できる。便利。基本的な操作は以下、すべてgitリポジトリ内で行うこと。
「M-x magit-status」… gitリポジトリの状態を表示する。変更をステージングするには「s」、「c」でコミットログを編集、「C-c C-c」でコミット実行
「M-x magit-push」 … gitのリモートリポジトリに変更をプッシュする
「M-x magit-pull」 … gitのリモートリポジトリから変更をpullする
「M-x magit-git-command」 … 何かしらのgitのコマンドを打ち込みたい場合実行「git clone」「git tag -l」とかはここから実行できる。
auto-complete
;; auto-complete (add-to-list 'load-path "~/.emacs.d/auto-complete") (require 'auto-complete-config) (add-to-list 'ac-dictionary-directories "~/.emacs.d/auto-complete/ac-dict") (ac-config-default) (global-auto-complete-mode t)
たしか設定は上記でいけたはず。Emacsでキーワードの補完を行う。
Javaなどの関数名の補完は設定しないとできないが、C/C++のようにリファレンスを見ながらのプログラミングの場合とても便利。
auto-revert-tail-mode
Emacs22以降であれば事前の準備は必要なし。「M-x auto-revert-tail-mode」で起動できる。UNIXコマンドの「tail -f」と同様にログの出力などをリアルタイムで見ることが出来る。
php-mode
phpとHTMLを混ぜて作業する際の設定 *emacs で PHP-mode と mmm-mode を使う
;; php-mode (require 'php-mode) ;; mmm-mode (require 'mmm-mode) (setq mmm-global-mode 'maybe) (mmm-add-mode-ext-class nil "\\.php?\\'" 'html-php) (mmm-add-classes '((html-php :submode php-mode :front "<\\?\\(php\\)?" :back "\\?>"))) (add-to-list 'auto-mode-alist '("\\.php?\\'" . xml-mode))
etags
- C/C++等の言語、スクリプト言語を書く時はetagsが一番手ごろにタグジャンプ出来るかもしれない。
- gtagをしばらく使っていたが、Haskellはgtag非対応なのでetagで統一したほうが良いかもしれない
- etags/ctags/gtagsはもう古くて、lspを使ったほうがよい Emacsの設定(lsp-mode) に書いた
- 準備
- CMakeでビルドする際にetagsを実行する, xxxが作成するバイナリの例
# https://stackoverflow.com/a/46940851/2565527 # For Emacs. if not necessary, please comment out it add_custom_target( etags COMMAND bash -c "find \"${xxx_SOURCE_DIR}\" -name \"*.cpp\" -print -or -name \"*.hpp\" -print | xargs etags -o \"${xxx_SOURCE_DIR}\"/TAGS" VERBATIM # "VERBATIM" <-- to escape args, see below # https://cmake.org/cmake/help/latest/command/add_custom_target.html ) add_dependencies(xxx etags)
あとは、M-x - . とかで飛べる。
- 参考:etags の使い方メモ
- Haskellではhasktagsを使った: hasktags: Produces ctags "tags" and etags "TAGS" files for Haskell programs
etags-table
- TAGSを読み込むのを自動化するために使う
ダウンロードと設定(packageにないのでauto-installでインストール)
$ wget http://www.emacswiki.org/emacs/download/etags-table.el -O ~/.emacs.d/auto-install/etags-table.el $ emacs --batch -Q -f batch-byte-compile ~/.emacs.d/auto-install/etags-table.el
- 設定
- 通常は以下の設定だけで事足りるのではないだろうか
- EtagsTable
(require 'etags-table) (setq etags-table-search-up-depth 10)
gtags
- 準備
- パッケージをインストールしておく
$ sudo apt-get install global // 使いたいプロジェクトの一番上で $ gtags
- .emacs
- 以下の設定で"M-."でタグにジャンプ、"M-r"で戻る
;; gtag (unless (package-installed-p 'global-tags) (package-refresh-contents) (package-install 'global-tags)) (require 'gtags) (global-set-key "\M-." 'gtags-find-tag) (global-set-key "\M-r" 'gtags-find-rtag) (global-set-key "\M-s" 'gtags-find-symbol) (global-set-key "\C-t" 'gtags-pop-stack) ;; gtagの自動更新 (defun my-update-gtags () (let* ((file (buffer-file-name (current-buffer))) (dir (directory-file-name (file-name-directory file)))) (when (executable-find "global") (start-process "gtags-update" nil "global" "-uv")))) (add-hook 'after-save-hook 'my-update-gtags)
editorconfig
プロジェクト直下にインデントやタブの設定をコンフィグで用意し、Emacsにそれを読み取らせる
(unless (package-installed-p 'editorconfig) (package-refresh-contents) (package-install 'editorconfig)) (editorconfig-mode 1)
Emacs24
load-theme
Emacs24にはカラーテーマが同梱されているので、load-themeを呼ぶだけでよろしい感じになる。
;; color (load-theme 'manoj-dark t)
sbt-mode
sbtとemacsは相性が悪い、sbt-modeとscala-modeをpackageでインストールして以下のようにすればansi-color由来の文字化けが起きない。
;; 色指定 (add-hook 'compilation-mode-hook 'ansi-color-for-comint-mode-on) (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on) (add-hook 'shell-mode-hook 'sbt-mode) (add-hook 'compilation-filter-hook '(lambda () (let ((start-marker (make-marker)) (end-marker (process-mark (get-buffer-process (current-buffer))))) (set-marker start-marker (point-min)) (ansi-color-apply-on-region start-marker end-marker))))
ps-print
最近のEmacsでは組み込みになっているはず(前から?)Emacs上でソースコードの印刷のために使う。
事前にnkfとghostscriptをインストールしましょう
- Macの場合
- portsからnkf, ghostscript, ghostscript-fonts-hiraginoをインストール
以下の様なelispを.emacsに追記
;; マルチバイト文字の文字化け防止 (setq ps-multibyte-buffer 'non-latin-printer) (require 'ps-mule) (defalias 'ps-mule-header-string-charsets 'ignore) ;; PDF出力 ;; e2psが使えるならば以下で (setq my-pdfout-command-format "nkf -e | e2ps -a4 -p -nh | ps2pdf - %s") (defun my-pdfout-region (begin end) (interactive "r") (shell-command-on-region begin end (format my-pdfout-command-format (read-from-minibuffer "File name:")))) (defun my-pdfout-buffer () (interactive) (my-pdfout-region (point-min) (point-max)))
上記のようなことをやらなくても
M-x ps-spool-buffer -> bufferをpostscript化
$ ps2pdf でPDF化できるのでは?(それをelisp化すればよいのだがLispわからん)
Emacs25
package
- なんかEmacs25でpackageのURL設定が変わった模様?
- 以前はこうしていたけど
;; Add package-archives (require 'package) (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t) (add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/")) (package-initialize)
- 変更した
;; Add package-archives (require 'package) ;; MELPAを追加 (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t) ;; MELPA-stableを追加 (add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t) (package-initialize)
ansi-color
- コンパイル時に色付きのデータを見る場合に必要
(require 'ansi-color) (defun colorize-compilation-buffer () (toggle-read-only) (ansi-color-apply-on-region compilation-filter-start (point)) (toggle-read-only)) (add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
Emacs27
load-theme
- TODO, FIXMEなどのコメントをハイライトできる
;; comment-tags -- https://github.com/vincekd/comment-tags (unless (package-installed-p 'comment-tags) (package-refresh-contents) (package-install 'comment-tags)) (require 'comment-tags) (add-hook 'prog-mode-hook 'comment-tags-mode)
Mac
Emacs->クリップボード
- EmacsでコピーしたテキストをMacのクリップボードに反映したい
- macOS Sierra で emacs のコピペとクリップボードを共有する方法
- brew install reattach-to-user-namespace, してから以下を.emacsに設定
(progn (defun copy-from-osx () (shell-command-to-string "reattach-to-user-namespace pbpaste")) (defun paste-to-osx (text &optional push) (let ((process-connection-type nil)) (let ((proc (start-process "pbcopy" "*Messages*" "reattach-to-user-namespace" "pbcopy"))) (process-send-string proc text) (process-send-eof proc)))) (setq interprogram-cut-function 'paste-to-osx) (setq interprogram-paste-function 'copy-from-osx))