2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

emacsのshell-modeでパスワードが表示されてしまう問題

Last updated at Posted at 2020-09-08

現象

emacsのshell-modeからパスワードを入力すると普通は、画面下に専用の入力ボックスが開きそこに伏せ文字で入力できます。しかし、日本語版Ubuntuでsudoをするとそれが機能せずパスワードが直に表示されてしまいます。

$ sudo ls
[sudo] ユーザーホゲホゲのパスワード:ここにパスワードが表示されてしまう

伏せ字機能の仕組み

コマンドラインで表示される文字列がcomint-password-prompt-regexpの正規表現とマッチする場合は伏せ字機能が有効になります。つまり本件の場合は[sudo] ユーザーホゲホゲのパスワード:が正規表現にマッチしていない事が原因です。export LANG=Cで英語表記にするとsudoは[sudo] password for user_hogehoge:になるため、正規表現とマッチして伏せ字機能が有効になります。しかし日本語フィアル名が文字化けするしスマートではないです。

解決策

comint-password-prompt-regexpに正規表現を追加することで任意の文字列で伏せ字機能を使えるようにします。"\\|^..."...の部分に追加したい正規表現を加えます。[sudo]で始まり:で終わる文字列が来たら有効化したいので追加する正規表現は"\\|^\\[sudo\\].+:"となります。以下を設定ファイルに追記します。

(add-hook 'shell-mode-hook
          #'(lambda ()
              (setq comint-password-prompt-regexp
                    (concat comint-password-prompt-regexp
                            "\\|^\\[sudo\\].+:"))))
  • ""で囲んでいるためバックスラッシュは2重にする -> \\
  • ^行頭からを評価する

参考

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?