LoginSignup
27
19

More than 5 years have passed since last update.

自動で起動されるエディタを nano から vim (または emacs 等) へ変更する

Last updated at Posted at 2016-12-29

新しく Ubuntu をセットアップしたマシンで git を初めて使った時とか、 nano が起動してびっくりしますよね。

git config で core.editor を設定したら git は vim を使うようにできるんですけど、 git 以外でも crontab -e でも nano が起動したりするんで、どうせならもっと根本から vim を使うように設定する方法を探してみましょう。

(なお、調べてみた動機は、 editor コマンドで起動するエディタを変更しようとして select-editor ってコマンドを試してみたけど上手く行かずにモヤモヤしてたことです。)

git

7.1 Git のカスタマイズ - Git の設定 # 基本的なクライアントのオプション

core.editor
コミットやタグのメッセージを編集するときに使うエディタは、ユーザーがデフォルトエディタとして設定したものとなります。デフォルトエディタが設定されていない場合は Vi エディタを使います。このデフォルト設定を別のものに変更するには core.editor を設定します。

とありますが、これは正確ではありません。これが本当なら nano じゃなくて vi が起動するはずですもんね。

git が起動するエディターは、 git の論理変数 GIT_EDITOR で選択されます。

man git-var

       GIT_EDITOR
           Text editor for use by Git commands. The value is meant
           to be interpreted by the shell when it is used. Examples:
           ~/bin/vi, $SOME_ENVIRONMENT_VARIABLE, "C:\Program
           Files\Vim\gvim.exe" --nofork. The order of preference is
           the $GIT_EDITOR environment variable, then core.editor
           configuration, then $VISUAL, then $EDITOR, and then the
           default chosen at compile time, which is usually vi.
$ echo $VISUAL

$ echo $EDITOR

$ git var GIT_EDITOR
editor

ということで、コンパイル時に指定される GIT_EDITOR のデフォルト値が通常は vi だけど、 Debian / Ubuntu はカスタマイズして editor コマンドを使っているようです。

/usr/bin/editor

$ file /usr/bin/editor
/usr/bin/editor: symbolic link to /etc/alternatives/editor
$ ll /etc/alternatives/editor
lrwxrwxrwx 1 root root 9 Dec 29 20:16 /etc/alternatives/editor -> /bin/nano*

ということで、 editor コマンドは alternatives によって管理されているシンボリックリンクです。変更するには次のようにします。

$ sudo update-alternatives --config editor
There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode

つまり、変更するには root が必要なんですね。 root が無いときは、環境変数 VISUALEDITOR を設定するのが、 git よりも根本でエディタを設定するのが良さそうです。

crontab -e

man crontab

       The -e option is used to edit the current crontab  using  the
       editor  specified  by  the VISUAL or EDITOR environment vari‐
       ables.  After you exit from the editor, the modified  crontab
       will  be  installed automatically. If neither of the environ‐
       ment  variables  is  defined,   then   the   default   editor
       /usr/bin/editor is used.

ということで、これも EDITOR, VISUAL 環境変数で制御でき、デフォルトは /usr/bin/editor になっています。

なんですが、これも Debian / Ubuntu カスタマイズが入っているようで、実際に起動されるのは editor じゃなくて sensible-editor コマンドでした。

sensible-editor / select-editor

man sensible-editor

DESCRIPTION
       sensible-editor,  sensible-pager  and  sensible-browser  make
       sensible decisions on which editor, pager, and web browser to
       call, respectively.  Programs in Debian can use these scripts
       as  their  default  editor,  pager, or web browser or emulate
       their behavior.

SEE ALSO
       Documentation of the EDITOR, VISUAL and  PAGER  variables  in
       environ(7) and select-editor(1) for changing a user's default
       editor

ということで、これは Debian 独自の、デフォルトのエディタ/ページャー/ブラウザを起動するコマンドです。
SEE ALSO を追いかけてみましょう。

man environ

       EDITOR/VISUAL
              The user's preferred utility to edit text files.

man select-editor

       The results are stored as
       SELECTED_EDITOR in ~/.selected_editor, which is  sourced  and
       used  by  sensible-editor.   SELECTED_EDITOR is overridden by
       the VISUAL and EDITOR environment variables.
$ select-editor

Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

Choose 1-4 [2]: 3

$ cat ~/.selected_editor
# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/vim.basic"

~$ export EDITOR=nano
$ sensible-editor  # nano が起動される

ということで、 select-editor コマンドを使って sensible-editor コマンドで起動するエディタを設定できるものの、 EDITOR 環境変数の方が強いです。

結論

  • EDITOR, VISUAL 環境変数で、 crontab -e, git, sensible-editor 全てのコマンドで起動するエディタを指定できる。ただし editor コマンドは別。
    • そして EDITOR 環境変数を設定すると、 select-editor コマンドはHOMEディレクトリに .selected_editor っていうゴミファイルを作るだけのゴミコマンドになる。
  • editor コマンドで起動するエディタは alternatives で管理されているので root が無いと変更不可能。
    • root を持っていて、同じマシンのユーザーと戦争にならないなら vi とか vim にしてしまおう。
    • editor コマンドを使うようなコマンドは大抵先に EDITOR 環境変数を見てくれるので、 root を持って無くてもほぼ困らないはず。
  • 僕以外にも select-editoreditor が無関係なことにモヤモヤを抱えてた人が居たら全部 Debian カスタマイズのせい。
27
19
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
27
19