1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Emacs tutorial

Last updated at Posted at 2025-04-13

はじめに

Emacsのコマンドって覚えるの大変...
実際に練習してもなかなか覚えられないと思うので、すぐに見れるようにまとめてみた
後から見つけた神サイト
Emacsチートシート

目次

基本コマンド
移動系
削除系
選択・コピー・ペースト
分割機能
検索系
モード
置換モード
ディレクトリモード

おまけ
縦・横にwindowをn分割にする。
tabを一括挿入・削除

基本コマンド

名前 キーバインド
ファイルを開く C-x C-f
シェルを開く C-x z
Emacsを閉じる C-x C-c
Emacsから抜ける C-z
Emacsに戻る fg
戻る C-x u
キャンセル C-x g

困ったらC-x g これが最強

移動系

名前 キーバインド
C-b
C-f
C-p
C-n
最初 M-<
最後 M->
次ページ C-v
前ページ C-u
行頭 C-a
行末 C-e

削除系

名前 キーバインド
削除 C-d
delete C-h

選択・コピー・ペースト

名前 キーバインド
選択 C-space
全選択 C-a
コピー M-y
カット C-y
ペースト C-y

分割機能

名前 キーバインド
縦に分割 C-x 2
横に分割 C-x 3
window移動(次) C-x o
window移動(後) C-x p
window最大化 C-x 1
windowを閉じる C-x 0

検索系

名前 キーバインド
行に移動 C-x g
検索 C-s 文字
次の候補 C-s
前の候補 C-r

モード

名前 キーバインド
置換モード M-%
ディレクトリモード C-x C-f
バッファーモード C-x b

置換モード

名前 キーバインド
許可 Space,y
拒否 n
全部拒否 !
抜ける q

ディレクトリモード

名前 キーバインド
戻る ^
ファイル作成 C-x C-f ファイル名
ディレクトリ作成 + ディレクトリ名
マークつける m
マーク削除 u
マーク全削除 U
コピー C
名前変更/移動 R
削除 D

ゆっくり慣れていこう!!

おまけ

キーバインドは実は好きなように増やせるんです!
いくつか自分で作った(by Chatgpt)のコマンドがあるので
よければ使ってみてください

縦・横にwindowをn分割にする。

emacsで以下のファイルを開く

~/.emacs.d/init.el

ここで一番下まで動かし(M->)、以下の文章をそのまま入力

;現在のウィンドウを縦方向にN分割する。
(defun split-current-window-vertically (n)
  (interactive "n縦に分割する数を入力してください: ")
  (let ((i 1))
    (while (< i n)
      (split-window-below) ;; 横分割
      (setq i (1+ i)))
    (balance-windows)))
    
;現在のウィンドウを横方向にN分割する。
(defun split-current-window-horizontally (n)                
  (interactive "n横に分割する数を入力してください: ")
  (let ((i 1))
    (while (< i n)
      (split-window-right) ;; 縦分割
      (setq i (1+ i)))
    (balance-windows)))
    
;; キーバインドの設定
(global-set-key (kbd "C-x v") 'split-current-window-vertically)
(global-set-key (kbd "C-x h") 'split-current-window-horizontally)

これでemacsを閉じてひらけばOK

#縦方向に4つに分割
C-x v 4
#横方向に4つに分割
C-x h 4

7ぐらいまでならできる。

tabを一括挿入・削除

コード書く時にtab挿入削除多いですよね、
for忘れたりいらなくなったりした時は特に...
それ用です
挿入は空白4つ
削除は文頭4つ削除なので、そこだけお気をつかて
実装は先ほどと同じなのでコードだけ

;tabを挿入
(defun insert-tab-to-region (start end)                     
  (interactive "r")
  (save-excursion
    (goto-char start)
    (while (< (point) end)
      (insert "    ")        
      (forward-line 1))))

;行頭のタブを削除
(defun remove-tab-from-region (start end)                                                                               
  (interactive "r")
  (save-excursion
    (goto-char start)
    (while (< (point) end)
      (when (looking-at "^\t")  ; 行頭がタブの場合                         
        (delete-char 1))
      (forward-line 1))))
      
;キーバインド設定
(global-set-key (kbd "C-x ti") 'insert-tab-to-region)
(global-set-key (kbd "C-x tr") 'remove-tab-from-region)
#タブを行頭に挿入
C-x t-i
#タブを行頭から削除
C-x t-r
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?