すべてのプロダクト
Search
ドキュメントセンター

Elastic Compute Service:Vimエディターを使用する

最終更新日:Aug 27, 2024

Vimエディタは、viエディタの最適化されたバージョンとして開発されたテキストエディタです。 Vimエディタでは、フォントの色や下線など、追加のフォーマットの詳細を含むテキストを表示できます。 VimエディタはLinuxに不可欠なツールであり、webアプリケーションの構成ファイルを編集するために使用できます。 このトピックでは、Vimエディターで実行できる基本的なVimコマンドと一般的な操作について説明します。

背景情報

次の表に、Vimのさまざまなモードを示します。

モード

説明

モード切り替え方法

通常モード

このモードでは、文字または行をコピー、貼り付け、削除できます。

  • vim <File name> コマンドを実行してファイルを開くと、Vimエディタは通常モードになります。

  • 別のモードからこのモードに切り替えるには、Escキーを押します。

挿入モード

このモードでは、文字を挿入できます。

通常モードからこのモードに切り替えるには、i、I、a、A、o、またはOを押します。

説明

Vimエディタが挿入モードに入ると、-- insert -- がエディタの左下隅に表示されます。

モードを置き換える

このモードでは、文字を置き換えることができます。

通常モードからこのモードに切り替えるには、Rを押します。

説明

Vimエディタが置換モードに入ると、エディタの左下隅に -- replace -- が表示されます。

視覚モード

このモードでは、テキストのブロックを選択できます。 テキストに対してコマンドのコピー、置換、削除などのコマンドを実行する前に、テキストのブロックを選択する必要があります。

通常モードからこのモードに切り替えるには、vを押します。

説明

Vimエディタがビジュアルモードに入ると、-- visual -- がエディタの左下隅に表示されます。

コマンドモード

このモードでは、文字列の検索と置換、行番号の表示、ファイルの変更の保存、エディターの終了を行うことができます。

通常モードからこのモードに切り替えるには、を押します。

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を挿入するには、次の手順を実行します。

    1. vim example.confコマンドを実行して、通常モードのVimでファイルを開きます。

    2. iを押して挿入モードに切り替えます。

    3. [場所] を入力します。

    4. Enterキーを押して次の行に移動します。

    5. Escキーを押して挿入モードを終了します。

    6. :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ファイルは、次の手順を実行します。

    1. vim example.confコマンドを実行して、通常モードのVimでファイルを開きます。

    2. :10と入力して、カーソルを10行目に移動します。

    3. Iを押して挿入モードに切り替えます。

    4. # を入力します。

    5. Escキーを押して挿入モードを終了します。

    6. :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を挿入するには、次の手順を実行します。

    1. vim example.confコマンドを実行して、通常モードのVimでファイルを開きます。

    2. /Include conf.mo dules.d/*.confコマンドを実行して、挿入操作を実行する行を見つけます。

    3. oを押して挿入モードに切り替えます。

    4. LoadModule rewrite_module modules/mod_rewrite.soと入力します。

    5. Escキーを押して挿入モードを終了します。

    6. :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 NoneAllowOverride Allに置き換えるには、次の手順を実行します。

  1. vim example.confコマンドを実行して、通常モードのVimでファイルを開きます。

  2. /AllowOverride Noneコマンドを実行して、置換操作を実行する行を見つけます。

  3. カーソルをなしの最初の文字に移動します。

  4. Rを押して交換モードに切り替えます。

  5. すべてとスペースを入力します。

    説明

    Noneという単語は4文字で構成され、Allという単語は3文字で構成されています。 Noneの4文字すべてを置き換えるには、allの3文字の後に追加のスペースを入力します。

  6. Escキーを押して置換モードを終了します。

  7. :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行の先頭にある# を削除するには、次の手順を実行します。

    1. vim example.confコマンドを実行して、通常モードのVimでファイルを開きます。

    2. /#Listen 12.34.XX:XX:80コマンドを実行して、削除操作を実行する行を見つけます。 このように、カーソルは# 文字上にあります。

    3. xを押して# を削除します。

    4. :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ファイルから削除するには、次の手順を実行します。

    1. vim example.confコマンドを実行して、通常モードのVimでファイルを開きます。

    2. /#Listen 12.34.XX:XX:80コマンドを実行して、削除操作を実行する行を見つけます。

    3. 2ddを押して次のコンテンツを削除します。

      #Listen 12.34.XX:XX:80
      Listen 80
    4. :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.
      #