Unix 系の中で、2大エディタの1つである Emacs に関する設定の記述。


.emacs.el の設定

主な参考元は以下のとおりです。本当に、ありがとうございます。このような方々がおられるからこそ、快適になります。


;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 他の elisp の設定を読み込む
;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq load-path (cons "~/.emacs.d/" load-path))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Emacs の 表示関係を変更
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; title bar にファイル名を表示
;;(setq frame-title-format "%f")
(setq frame-title-format (format "emacs@%s : %%f" (system-name)))

;; tool bar を表示させない
(tool-bar-mode 0)
;; menu bar を表示させない
(menu-bar-mode -1)
;; スプラッシュを非表示にする
(setq inhibit-startup-message t)

;; 行番号・桁番号を modeline に表示する
(line-number-mode t)   ; 行番号
(column-number-mode t) ; 桁番号

;;; 対応する括弧を光らせる。
(show-paren-mode 1)

;;; 終了時にオートセーブファイルを消す
(setq delete-auto-save-files t)

;;; カーソルの点滅を止める
(blink-cursor-mode 0)

;;; カーソルの位置が何文字目かを表示する
(column-number-mode t)

;;; カーソルの位置が何行目かを表示する
(line-number-mode t)

;;; スクロールを一行ずつにする
(setq scroll-step 1)

;;; スクロールバーを右側に表示する
(set-scroll-bar-mode 'right)

;;; 最終行に必ず一行挿入する
(setq require-final-newline t)

;;; 一行が 80 字以上になった時には自動改行する
(setq fill-column 80)
(setq-default auto-fill-mode t)

;;; モードラインに時間を表示する
(display-time)

;;; 現在の関数名をモードラインに表示
(which-function-mode 1)

;;行間
(setq-default line-spacing 3)

;; 無駄な空行に気付きやすくする 
(setq-default indicate-empty-lines t)

;;文字の色つけ
(global-font-lock-mode t)

;;Ctrl-Hでバックスペース
(global-set-key "\C-h" 'backward-delete-char)

;;警告音を消す
(setq visible-bell t)

;; リージョンに色をつける (暫定マークモード)
(transient-mark-mode 1)

;; bs-showを使用する
(global-set-key "\C-x\C-b" 'bs-show)

;; テキストの折り返し無効
(setq-default truncate-partial-width-windows t)
(setq-default truncate-lines t)


;;フレームのタイトル指定
(setq frame-title-format   
	  (concat "%b - emacs@" system-name))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 補完関係
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 補完時に大文字小文字を区別しない
(setq completion-ignore-case t)

;;; 強力な補完機能を使う
;;; p-bでprint-bufferとか
;;(load "complete")
(partial-completion-mode 1)

;;; 補完可能なものを随時表示
;;; 少しうるさい
(icomplete-mode 1)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 文字表示位置関係
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;整数で指定するとピクセル数で、少数で指定すると
;;行の高さに対して相対値で設定されます。
(setq-default line-spacing 4)

;;; 最終行に必ず一行挿入する
(setq require-final-newline t)

;;; 一行が 80 字以上になった時には自動改行する
(setq fill-column 80)
(setq-default auto-fill-mode t)


;;;;;;;;;;;;;;;;;;;;;;
;; カーソル移動設定
;;;;;;;;;;;;;;;;;;;;;;
;;カーソル移動を論理行ではなく、物理行(見たまま)単位で移動する。
(require 'physical-line)
(setq-default physical-line-mode t)
;; dired-mode は論理行移動のままにする.
(setq physical-line-ignoring-mode-list '(dired-mode))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 日本語設定
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(set-language-environment "Japanese")
;; 文字コード関連
(set-terminal-coding-system 'utf-8-unix)
(set-keyboard-coding-system 'utf-8-unix)
(set-buffer-file-coding-system 'utf-8-unix)
(setq default-buffer-file-coding-system 'utf-8-unix)
(prefer-coding-system 'utf-8-unix)
(set-default-coding-systems 'utf-8-unix)
(setq file-name-coding-system 'utf-8-unix)
(set-locale-environment "ja_JP.UTF-8")

;Ctrol + \ 無効 (mozc 使う関係で無効化する)
(global-set-key "\C-\\" nil)

Emacs で、anthy-el を使う

emacs での日本語入力は、インプットメソッドからの入力と Emacs 自体の漢字エンジンを使う方法があります。

今回は、emacs の漢字エンジンに anthy を使おうというものです。

anthy-el のインストール

ubuntu では apt で、、
$ sudo apt-get install anthy-el

とインストールできます。

.emacs.el の記述。

Anthy Wiki を参考にして。

~/.emacs.el に、以下の内容を追記すれば日本語入力ができます。
; 日本語をデフォルトにする。
(set-language-environment "Japanese")
; anthy.el をロードする。
(load-library "anthy")
; japanese-anthy をデフォルトの input-method にする。
(setq default-input-method "japanese-anthy")


Emacs で、M+ Font (Bitmap Font) を使う。

M+ Font がインストールされているか

$ xlsfonts | grep mplus
で確認。

記述場所

~/.emacs.el に記述する方法もあるが、結構面倒くさいらしいので、
~/.Xresources に次の記述を行う。
! ~/.Xresources
Emacs.Fontset-0: -mplus-gothic-*-r-normal--10-*-*-*-*-*-fontset-mplus_j10,\
ascii:-mplus-gothic-medium-r-normal--10-*-*-*-*-*-iso8859-1
Emacs.Fontset-1: -mplus-gothic-*-r-normal--12-*-*-*-*-*-fontset-mplus_j12,\
ascii:-mplus-fxd-medium-r-semicondensed--12-*-*-*-*-*-iso8859-1
Emacs.font: fontset-mplus_j12

動作確認

反映されたか確認するために、
$ xrdb -merge .Xresources
$ emacs


Emacs server を使う。

同じウインドウで開きつづける

特に設定を行わない Emacs では
$ emacs 
と起動するごとに新たなウインドウが開くと思う。
 $ emacs filename
と記述したとき、既にウインドウが開いている場合はそのウインドウに表示させたいときは
次のような設定を行う。

設定手順

手順1.~/.emacs.el の追記
;; ~/.emacs.el

;; emacs server の起動
(server-start)

手順2. 次のスプリクトを作成する
#!/bin/sh
# filename: emacs
 
# emacs と言う名前の 全 pid のうち番号の最も若いものを取得
pgreph=`pgrep emacs | head -1`
# XIM を起動させない
XMODIFIERS=@im=none

# 取得した pid は、起動したスプリクトの pid であるか
if [ $pgreph -eq $$ ]
then
        /usr/bin/emacs $*
# 既に emacs が起動しているとき
elif [ -z "$1" ]
then
        # 引数がないときは、起動させない 
        echo "Usage: $0 <filename>"
else
        # emacs client で起動
        /usr/bin/emacsclient "$@"
fi

このスプリクトをemacs という名前にし
~/bin/ に格納する。
(~/bin/ は、自分で作成した起動スプリクトなどを入れる場所とする)

環境変数の設定

環境変数path~/bin/ を通す。
コマンドでは,
$ export PATH=~/bin:$PATH
と行えばよい。
しかし、起動するたびにコマンドを打ち込むのは面倒なので,
~/.bashrc などに次の内容を追記 (zsh の場合は,.zshrc)
# .bashrc
export PATH=~/bin:$PATH

反映させるために、
$ source .bashrc

確認

touch test.txt
emacs ~/.emacs.el
emacs test.txt
同じウインドウに表示された?
目安箱バナー