LoginSignup
11

More than 5 years have passed since last update.

[Emacs] auto-mode-alist の "\\.js\\'" と "\\.js$" の違い

Posted at

はじめ

js2-modeのコメントに書いてある、以下の記述をコピペして設定していました。

(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))

一方で、だいぶ前に設定したc++-modeの設定は以下のように記述していました。

(add-to-list 'auto-mode-alist '("\\.C$"  . c++-mode))
(add-to-list 'auto-mode-alist '("\\.cc$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.cpp$". c++-mode))
(add-to-list 'auto-mode-alist '("\\.hh$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.h$"  . c++-mode))

終端が$\\'であるかが異なります。

正規表現では$は行の末尾にマッチするするのでわかるのですが、
\\'はわかりません。

EmacsWiki

Gooogle 先生に emacs auto-mode-alist と尋ねたところ、
http://www.emacswiki.org/emacs/AutoModeAlist を教えてくれました。

Note that \' matches the end of a string, where as $ matches the empty string before a newline. Thus, $ may lead to unexpected behavior when dealing with filenames containing newlines. (Should be pretty rare… ;))

ということで、ファイル名に改行が入ってくると予期しない動作になる("foo.js\nx"とか?)ので、この用途では\\'を用いるべきとのことです。

また、これはauto-mode-alistにかぎらず、Emacsの正規表現全般で使えます。

結論

auto-mode-alist に追加するパターンは、\\'で終端するべき。

参考

蛇足

  • MacBook でバックスラッシュを出すにはoption+¥なので面倒
  • 引用文中に$でくくられた箇所が入っていたので数式と解釈されてしまい、表示が崩れて困った
  • バッククオートとクオートが混ざって疲れる

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
11