LoginSignup
5
2

More than 3 years have passed since last update.

最低限の設定を書いた個人用init.elのメモ

Last updated at Posted at 2020-12-18

僕は普段からemacsを使っているのですが,OSのアップグレードするとき設定に毎度苦戦するので最低限の設定をここにメモっておきたいと思ったのでこの記事を書きました.
いろいろな記事のツギハギで,完全に個人用のものであることをご了承ください:pray:
emacsは大好きなのですが,僕はLispを書けないので,できるだけ自作関数を書くのを避けています.(メンテナンスできる気がしないので)

環境

OS: Ubuntu 20.04
Emacs Version: 26

melpa

まず,外部パッケージをインストールするためにmelpaのURLを設定します.
なんか前にリンク切れに遭遇したことがあるような気がしないでもないので,リンク切れに注意

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

use-package

パッケージ管理にはuse-packageを使うので,
M-x package-install RET use-packageでインストールします.
use-packageがない場合,自動でインストール方法があればいいんですが(多分ある),僕はEmacs Lispを書けないのでとりあえず手動でやります.

日本語fontの設定

多分ここが一番苦戦するところ.
Ubuntuだと,emacsの日本語デフォルトフォントがなんか変です.サイズでかいし...
そのため,いい感じの日本語フォントを設定します.はじめに,いい感じの日本語フォントをインストールします.ここではIPAexFontを使います.

sudo apt install fonts-ipaexfont-gothic 
fc-list | grep -e IPAex # フォントの確認
# /usr/share/fonts/opentype/ipaexfont-gothic/ipaexg.ttf: IPAexゴシック,IPAexGothic:style=Regular
# /usr/share/fonts/truetype/fonts-japanese-gothic.ttf: IPAexゴシック,IPAexGothic:style=Regular

init.elに以下を書きます.

;; japanese font
(set-fontset-font t 'japanese-jisx0208 (font-spec :family "IPAexGothic")) 
;; フォントの設定
(add-to-list 'face-font-rescale-alist '(".*IPAex.*" . 0.85))
;; フォントサイズの調整

参考: https://asukiaaa.blogspot.com/2017/11/ubuntuemacs.html

mozcの設定

多分ここが一番苦戦するところ(2回目).
まずはOS (Ubuntu) 側で以下をインストール

sudo apt install emacs-mozc emacs-mozc-bin

init.elに以下を書く

;; mozcとmozc-popupの設定
(use-package mozc
  :ensure t)
(set-language-environment "Japanese")
(setq default-input-method "japanese-mozc")
(prefer-coding-system 'utf-8)
(use-package mozc-popup
  :ensure t)
(setq mozc-candidate-style 'echo-area)

;; 全角半角キーで入力メソッドの切り替え
(global-set-key (kbd "<zenkaku-hankaku>") 'toggle-input-method)

ここで,全角半角キーを押しても上手く動きません.OSの変換キーとしての動きが優先されます.C-\で変換できるのですが,明らかに使いづらいので全角半角キーで変換できるようにします.
そこで,.Xresourcesというファイルに以下を書きます.

.Xresources
emacs*.useXIM:false

これを以下のコマンドで有効化

xrdb ~/.Xresources

参考: https://office-unite.com/2018/03/outline-processing_with_emacs_1/

yasnippet

スニペットが使える便利なパッケージなのですが,これだけインストールしてもデフォルトスニペットはインストールされないので注意.
yasnippetをインストールしたあとにM-x package-install RET yasnippet-snippetsでデフォルトスニペットをインストールします.これだけでも結構使える(これしか使ってない)ので忘れずにインストールしておくことが大事.
あと,デフォルトの状態だと,TABを押すとsnippetの候補がすべて展開されて鬱陶しいので,これを無効化します.

(define-key yas-minor-mode-map (kbd "TAB") nil)

カラーテーマ

僕はあまりカラーテーマにこだわらないので,デフォルトで気に入ったものを使います.
デフォルトのカラーテーマの導入は

(load-theme '設定したいテーマの名前 t)

とすればOK.僕はadwaitaが好きです.

参考: https://aoe-tk.hatenablog.com/entry/20130210/1360506829

init.elのコピペ

上では説明しませんでしたがivy,Dashboard, Neotreeなども導入しています.


一応init.elを載せておきます(長いので折りたたみ)
init.el
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
;; (package-install 'use-package)


;; Coding
(prefer-coding-system 'utf-8)

;; japanese font
(set-fontset-font t 'japanese-jisx0208 (font-spec :family "IPAexGothic"))
(add-to-list 'face-font-rescale-alist '(".*IPAex.*" . 0.85))

;; 行番号の表示
(global-linum-mode t)
(setq linum-format "%3d ")

;; C-k 1回で行全体を削除する
(setq kill-whole-line t)

;;通常のウィンドウ用の設定, テキストの折り返し
(setq-default truncate-lines t
          truncate-partial-width-windows t)


;; active でない window の空 cursor を出さない
(setq cursor-in-non-selected-windows nil)

;; bufferの先頭でカーソルを戻そうとしても音をならなくする
(defun previous-line (arg)
  (interactive "p")
  (condition-case nil
      (line-move (- arg))
    (beginning-of-buffer)))

;; bufferの最後でカーソルを動かそうとしても音をならなくする
(defun next-line (arg)
  (interactive "p")
  (condition-case nil
      (line-move arg)
      (end-of-buffer)))

;; エラー音をならなくする
(setq ring-bell-function 'ignore)

;; メニューバー,ツールバーの非表示
(menu-bar-mode 0)
(tool-bar-mode 0)

;; 起動画面の設定
(setq inhibit-startup-message t)

;; バックアップファイルを作らないようにする
(setq make-backup-files nil
      auto-save-default nil)

;; 括弧の対応関係をハイライト表示
(show-paren-mode nil)
(electric-pair-mode 1)


;; mouse in terminal emacs
(xterm-mouse-mode t)
(mouse-wheel-mode t)
(global-set-key   [mouse-4] '(lambda () (interactive) (scroll-down 1))) ;; scroll
(global-set-key   [mouse-5] '(lambda () (interactive) (scroll-up   1)))


;; 大文字 <-> 小文字の補完について
(setq completion-ignore-case t)
(setq read-buffer-completion-ignore-case t)
(setq read-file-name-completion-ignore-case t)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; key config
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(global-set-key [f1] 'other-window)
(global-set-key [f2] 'neotree-toggle)
(global-set-key [f3] 'delete-trailing-whitespace)
;;(global-set-key [f4] (lambda () (interactive)(ansi-term "bash")))
(global-set-key [f4] 'kill-this-buffer)
(global-set-key [f5] 'compile)
(global-set-key (kbd "M-<up>") 'windmove-up)
(global-set-key (kbd "M-<right>") 'windmove-right)
(global-set-key (kbd "M-<down>") 'windmove-down)
(global-set-key (kbd "M-<left>") 'windmove-left)
(global-set-key (kbd "M-z") 'suspend-emacs)
(global-set-key (kbd "<zenkaku-hankaku>") 'toggle-input-method)
(global-set-key (kbd "C-:") 'hs-toggle-hiding)
(global-set-key (kbd "C-x C-f") 'counsel-find-file)
(global-set-key (kbd "C-s") 'swiper)
(global-set-key (kbd "C-r") 'swiper)
(defvar swiper-include-line-number-in-search t)
(setq compilation-scroll-output t)

;; undefine C-z
(global-set-key "\C-z" nil)



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PACKAGES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Auto Complete
(use-package auto-complete
  :init (global-auto-complete-mode t)
  :ensure t)

;;
;; python
;;
(use-package python
  :mode ("\\.py\\'" . python-mode)
  :interpreter ("python3" . python-mode)
  :config
  (add-hook 'python-mode-hook
        (lambda()
          (hs-minor-mode t))))

;; -------------------------------------------------------------
;; ivy
;; -------------------------------------------------------------
(use-package ivy
  :init (ivy-mode t)
  :ensure t)


;; -------------------------------------------------------------
;; counsel
;; -------------------------------------------------------------
(use-package counsel
  :config (setq counsel-find-file-ignore-regexp
        (concat
         ;; File names beginning with # or .
         "\\(?:\\`[#.]\\)"
         ;; File names ending with # or ~
         "\\|\\(?:\\`.+?[#~]\\'\\)"))
  :ensure t)



;;
;; powerline
;;
(use-package powerline
  :ensure t)
;; (powerline-default-theme)

;; -------------------------------------------------------------
;; swiper
;; -------------------------------------------------------------
(use-package swiper
  :ensure t)

;;
;; all the icons
;;
(use-package all-the-icons
  :ensure t)
;; note: M-x all-the-icons-install-fonts

;; -------------------------------------------------------------
;; neotree
;; -------------------------------------------------------------
(use-package neotree
  :ensure t
  :config
  (setq neo-theme (if (display-graphic-p) 'icons 'arrow)))


;; -------------------------------------------------------------
;; undo-tree
;; -------------------------------------------------------------
(use-package undo-tree
  :defer t
  :init (global-undo-tree-mode t)
  :bind (("C-z" . undo-tree-undo)
     ("C-/" . undo-tree-undo)
     ("M-/" . undo-tree-redo)
     ("C-S-z" . undo-tree-redo)
     ("C-x u" . undo-tree-visualize))
  :ensure t)


;; -------------------------------------------------------------
;; dashboard
;; -------------------------------------------------------------
(use-package dashboard
  :config
  (dashboard-setup-startup-hook)
  (setq dashboard-items '((recents  . 15)))
 :ensure t)


;;
;; yasnippet
;;
(use-package yasnippet
  :ensure t
  :init (yas-global-mode t)
  :diminish yas-minor-mode
  :config
  (define-key yas-minor-mode-map (kbd "TAB") nil))
;; yasnippet-snippets mo install


;;
;; mozc and mozc-popup
;;
(use-package mozc
  :ensure t)
(set-language-environment "Japanese")
(setq default-input-method "japanese-mozc")
(prefer-coding-system 'utf-8)
(use-package mozc-popup
  :ensure t)
(setq mozc-candidate-style 'echo-area)


;; -------------------------------------------------------------
;; compile
;; -------------------------------------------------------------
(use-package compile
  :config
  (bury-successful-compilation t)
  :ensure t)

(defvar yel-compile-auto-close nil)
(defadvice compile (after compile-aftercheck
                          activate compile)
  "Adds a funcion of windows auto-close."
  (let ((proc (get-buffer-process "*compilation*")))
    (if (and proc yel-compile-auto-close)
        (set-process-sentinel proc 'yel-compile-teardown))))
(defun yel-compile-teardown (proc status)
  "Closes window automatically, if compile succeed."
  (let ((ps (process-status proc)))
    (if (eq ps 'exit)
        (if (eq 0 (process-exit-status proc))
            (progn
              (delete-other-windows)
              (kill-buffer "*compilation*")
              (message "---- Compile Success ----")
              )
          (message "Compile Failer")))
    (if (eq ps 'signal)
        (message "Compile Abnormal end"))))


;; C mode
(use-package cc-mode
;; c-mode-common-hook は C/C++ の設定
  :config
  (add-hook 'cc-mode-hook
        (lambda()
          (setq c-default-style "k&r"
            c-basic-offset)
          (hs-minor-mode t))))

;; custom set, dont touch
(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.
 '(custom-enabled-themes (quote (adwaita)))
 '(package-selected-packages
   (quote
    (yasnippet-snippets dockerfile-mode yaml-mode page-break-lines powerline dashboard all-the-icons bury-successful-compilation neotree counsel company avy-migemo ivy processing-mode js2-mode xclip: mozc-popup go-mode markdown-mode ## mozc use-package auctex yasnippet csv-mode jedi auto-complete json-mode undo-tree))))
(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.
 )

おわりに

まぁどうせ,また設定で苦戦することになるとは思うのですが,多少マシになったらいいかなと思いました.
もっといい書き方があったらコメントで教えていただけると幸いです:upside_down:

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2