LoginSignup
3
4

More than 5 years have passed since last update.

Emacsのコメント機能をより使いやすくする

Posted at

Emacsには標準でcomment-dwimcomment-or-uncomment-regionなどのコメント機能があります。
これらは行の途中から途中までのコメントアウトを多用するならば便利ですが、一行ないし複数行まるごとのコメントアウトしか使っていないと、いちいちリージョンの設定がめんどうなので、

  • リージョンが設定されていない
    • カレント行の全体をコメントアウト(解除)
  • リージョンが設定されている
    • リージョンが含むすべての行の全体をコメントアウト(解除)

となるような関数を作りました。

たとえば#の部分がリージョンだとすると

........
....####
####....
........

このようにコメントアウトされます。

........
;; ........
;; ........
........
(global-set-key (kbd "C-M-;") 'rough-comment)
(defun rough-comment (bg ed)
  "ざっくりとコメントアウト(解除)します。"
  (interactive (list (point) (mark)))
  (if (not mark-active)
      (save-excursion
        (comment-or-uncomment-region (progn (beginning-of-line) (point))
                                     (progn (end-of-line) (point))))
    (save-excursion
      (comment-or-uncomment-region
       (progn (goto-char (if (< bg ed) bg ed)) (beginning-of-line) (point))
       (progn (goto-char (if (< bg ed) ed bg)) (end-of-line) (point))))))

ソースをコメントアウト(解除)する際は新しく作ったrough-comment、ソースの説明のコメントを挿入する際はcomment-dwimを使います。

両者はコメントの使用目的が異なるので、関連性のないキーバインドが良いと思います。
(実際は両者の機能を一つの関数にまとめられんかった…というかむしろ使いにくくなっている気が…)

3
4
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
3
4