【Ubuntu】「systemctl edit」や「visudo」で使用されるエディタの変更方法

Ubuntuの「systemctl edit」や「visudo」等で使用される標準のエディタを変更する方法です。

標準エディタの変更方法

「update-alternatives --config editor」コマンドを使うことで、システムに設定されている標準のエディタを変更することが出来ます。

これで、「systemctl edit」や「visudo」等も普段使い慣れたエディタで編集を行うことが出来るようになります。

$ 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    15        manual mode

Press <Eenter> to keep the current choice[*], or type selection number:

標準エディタの変更例

実際にエディタの変更を行ってみます。

「update-alternatives --config editor」を実行すると、選択できるエディタのリストが表示されます。

現状選択されているエディタは左側に「*」が表示されていて、下記の場合0番の「/bin/nano」が設定されていることがわかります。

変更したいエディタの番号を入力して「Enter」キーを押下すると、標準のエディタを変更することが出来ます。

設定変更をしたくない場合は、そのまま「Enter」キーを押下すると設定を変更せずに終了することが出来ます。

$ 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    15        manual mode

Press <Eenter> to keep the current choice[*], or type selection number:

私は普段「vi」を使用しているので、「vi」を選択したいのですが表示されている一覧に表示されていません。

ですので、「vi」のフルパスを「which」コマンドで探してみます。

$ which vi
/usr/bin/vi

「/usr/bin/vi」であることが分かりましたが、これも一覧には表示されていないので更に詳しい情報を確認してみます。

「/usr/bin/vi」は「/etc/alternatives/vi」へのシンボリックリンクで、更に「/etc/alternatives/vi」は「/usr/bin/vim.basic」へのシンボリックリンクであることが分かりました。

$ ls -l /usr/bin/vi
lrwxrwxrwx 1 root root 20 Apr 21  2022 /usr/bin/vi -> /etc/alternatives/vi
$ ls -l /etc/alternatives/vi
lrwxrwxrwx 1 root root 18 Apr 21  2022 /etc/alternatives/vi -> /usr/bin/vim.basic

「/usr/bin/vim.basic」は一覧にあったので「3」を入力して「/usr/bin/vim.basic」を選択します。

$ 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    15        manual mode

Press <Eenter> 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

設定変更されているか再度「update-alternatives --config editor」を実行し確認してみます。

「*」が3番の「/usr/bin/vim.basic」に表示されているので、標準エディタが変更されていることが確認できました。

あとは、「Enter」キーを押下して設定変更を行わず終了させます。

$ 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    15        manual mode

Press <Eenter> to keep the current choice[*], or type selection number: 

以上で標準エディタの変更が完了しました。

コメント

タイトルとURLをコピーしました