Vimエディタは、viエディタの最適化されたバージョンとして開発されたテキストエディタです。 Vimエディタでは、フォントの色や下線など、追加のフォーマットの詳細を含むテキストを表示できます。 VimエディタはLinuxに不可欠なツールであり、webアプリケーションの構成ファイルを編集するために使用できます。 このトピックでは、Vimエディターで実行できる基本的なVimコマンドと一般的な操作について説明します。
背景情報
次の表に、Vimのさまざまなモードを示します。
モード | 説明 | モード切り替え方法 |
通常モード
| このモードでは、文字または行をコピー、貼り付け、削除できます。 |
|
挿入モード
| このモードでは、文字を挿入できます。 | 通常モードからこのモードに切り替えるには、 説明 Vimエディタが挿入モードに入ると、 |
モードを置き換える
| このモードでは、文字を置き換えることができます。 | 通常モードからこのモードに切り替えるには、 説明 Vimエディタが置換モードに入ると、エディタの左下隅に |
視覚モード
| このモードでは、テキストのブロックを選択できます。 テキストに対してコマンドのコピー、置換、削除などのコマンドを実行する前に、テキストのブロックを選択する必要があります。 | 通常モードからこのモードに切り替えるには、 説明 Vimエディタがビジュアルモードに入ると、 |
コマンドモード
| このモードでは、文字列の検索と置換、行番号の表示、ファイルの変更の保存、エディターの終了を行うことができます。 | 通常モードからこのモードに切り替えるには、 |
挿入
基本コマンド:
i: 現在の文字の左に挿入します。
I: 現在の行の先頭に挿入します。
a: 現在の文字の右側に挿入します。
A: 現在の行の最後に挿入します。
o: 現在の行の下に新しい行を挿入します。
O: 現在の行の上に新しい行を挿入します。
たとえば、次のコンテンツを含むexample.confファイルを編集します。
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
Include conf.modules.d/*.conf
を含める
例1: Example. confファイルの最初の行として
Location
を挿入するには、次の手順を実行します。vim example.conf
コマンドを実行して、通常モードのVimでファイルを開きます。i
を押して挿入モードに切り替えます。[場所]
を入力します。Enterキーを押して次の行に移動します。
Esc
キーを押して挿入モードを終了します。:wq
と入力してファイルへの変更を保存し、エディターを終了します。指定されたコンテンツがexample.confファイルに挿入されると、ファイルには次のコンテンツが含まれます。
Location # To be able to use the functionality of a module which was built as a DSO you # have to place corresponding `LoadModule' lines at this location so the # directives contained in it are actually available _before_ they are used. # Statically compiled modules (those listed by `httpd -l') do not need # to be loaded here. # # Example: # LoadModule foo_module modules/mod_foo.so # Include conf.modules.d/*.conf
を含める
例2: 例の10行目の先頭に
#
を挿入する。 confファイルは、次の手順を実行します。vim example.conf
コマンドを実行して、通常モードのVimでファイルを開きます。:10
と入力して、カーソルを10行目に移動します。I
を押して挿入モードに切り替えます。#
を入力します。Esc
キーを押して挿入モードを終了します。:wq
と入力してファイルへの変更を保存し、エディターを終了します。指定されたコンテンツがexample.confファイルに挿入されると、ファイルには次のコンテンツが含まれます。
# To be able to use the functionality of a module which was built as a DSO you # have to place corresponding `LoadModule' lines at this location so the # directives contained in it are actually available _before_ they are used. # Statically compiled modules (those listed by `httpd -l') do not need # to be loaded here. # # Example: # LoadModule foo_module modules/mod_foo.so # #Include conf.modules.d/*.conf
を含める
例3: Example. confファイルの
Include conf.mo dules.d/*.conf
行の下の行にLoadModule rewrite_module modules/mod_rewrite.so
を挿入するには、次の手順を実行します。vim example.conf
コマンドを実行して、通常モードのVimでファイルを開きます。/Include conf.mo dules.d/*.conf
コマンドを実行して、挿入操作を実行する行を見つけます。o
を押して挿入モードに切り替えます。LoadModule rewrite_module modules/mod_rewrite.so
と入力します。Esc
キーを押して挿入モードを終了します。:wq
と入力してファイルへの変更を保存し、エディターを終了します。指定されたコンテンツがexample.confファイルに挿入されると、ファイルには次のコンテンツが含まれます。
# To be able to use the functionality of a module which was built as a DSO you # have to place corresponding `LoadModule' lines at this location so the # directives contained in it are actually available _before_ they are used. # Statically compiled modules (those listed by `httpd -l') do not need # to be loaded here. # # Example: # LoadModule foo_module modules/mod_foo.so # Include conf.modules.d/*.conf LoadModule rewrite_module modules/mod_rewrite.so
Replace
基本コマンド:
R: Esc
キーを押して置換モードを終了するまで、強調表示された文字を置換します。
たとえば、次のコンテンツを含むexample.confファイルを編集します。
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
例: Example. confファイルのAllowOverride None
をAllowOverride All
に置き換えるには、次の手順を実行します。
vim example.conf
コマンドを実行して、通常モードのVimでファイルを開きます。/AllowOverride None
コマンドを実行して、置換操作を実行する行を見つけます。カーソルを
なし
の最初の文字に移動します。R
を押して交換モードに切り替えます。すべて
とスペースを入力します。説明None
という単語は4文字で構成され、All
という単語は3文字で構成されています。 Noneの4文字すべてを置き換えるには、allの3文字の後に追加のスペースを入力します。Esc
キーを押して置換モードを終了します。:wq
を入力してファイルへの変更を保存し、エディターを終了します。指定した文字を置き換えると、example.confファイルには次の内容が含まれます。
# AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride All
削除
基本コマンド:
x: ハイライトされた文字を削除します。
nx (nは数字を表す): ハイライトされた文字とその文字の後のn-1文字を削除します。
dd: カーソルがある行を削除します。
ndd (nは数字を表す): カーソルが置かれている行とその下のn-1の行を削除します。
たとえば、次のコンテンツを含むexample.confファイルを編集します。
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.XX:XX:80
Listen 80
例1: Example. confファイルの
#Listen 12.34.XX:XX:80
行の先頭にある#
を削除するには、次の手順を実行します。vim example.confコマンドを実行して、通常モードのVimでファイルを開きます。
/#Listen 12.34.XX:XX:80
コマンドを実行して、削除操作を実行する行を見つけます。 このように、カーソルは#
文字上にあります。x
を押して#
を削除します。:wq
と入力してファイルへの変更を保存し、エディターを終了します。指定されたコンテンツがexample.confファイルから削除されると、ファイルには次のコンテンツが含まれます。
# Listen: Allows you to bind Apache to specific IP addresses and/or # ports, instead of the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses. # Listen 12.34.XX:XX:80 Listen 80
例2:
#Listen 12.34.XX:XX:80
行とその下の行をExample. confファイルから削除するには、次の手順を実行します。vim example.conf
コマンドを実行して、通常モードのVimでファイルを開きます。/#Listen 12.34.XX:XX:80
コマンドを実行して、削除操作を実行する行を見つけます。2dd
を押して次のコンテンツを削除します。#Listen 12.34.XX:XX:80 Listen 80
:wq
と入力してファイルへの変更を保存し、エディターを終了します。指定されたコンテンツがexample.confファイルから削除されると、ファイルには次のコンテンツが含まれます。
# Listen: Allows you to bind Apache to specific IP addresses and/or # ports, instead of the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses. #