全部產品
Search
文件中心

Elastic Compute Service:Vim常用操作

更新時間:Feb 28, 2024

Vim是從vi發展而來的文字編輯器,可以用顏色或底線等方式來顯示一些特殊的資訊。Vim是Linux中必不可少的工具,搭建網站修改設定檔時經常用到。本教程介紹Vim編輯器的基本命令和常用操作。

背景資訊

Vim的各個模式介紹如下表所示:

模式

作用

模式轉換

普通模式

(Normal Mode)

在該模式下,您可以複製、粘貼、刪除字元或行。

  • 運行vim <檔案名稱>開啟檔案時,即進入普通模式。

  • 在其他四個模式下,按Esc鍵即進入普通模式。

插入模式

(Insert Mode)

在該模式下,您可以插入字元。

在普通模式下,按i,I,a,A,o,O中任一字元即進入插入模式。

說明

進入插入模式後,編輯器左下角會顯示-- INSERT --

替換模式

(Replace Mode)

在該模式下,您可以替換字元。

在普通模式下,按R即進入替換模式。

說明

進入替換模式後,編輯器左下角會顯示-- REPLACE --

可視模式

(Visual Mode)

在該模式下,您可以選擇文本。命令(如,複製、替換、刪除等)僅作用於選中的文檔。

在普通模式下,按v即進入可視模式。

說明

進入可視模式後,編輯器左下角會顯示-- VISUAL --

命令模式

(Command Mode)

在該模式下,您可以尋找字串、替換字串、顯示行號、儲存修改、退出編輯器等。

在普通模式下,按:即進入命令模式。

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            
  • 樣本一:在設定檔example.conf的第一行,插入Location。步驟如下:

    1. 運行vim example.conf命令開啟檔案,進入普通模式。

    2. i進入插入模式。

    3. 輸入Location

    4. 按斷行符號鍵換行。

    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            
  • 樣本二:在設定檔example.conf第十行的行首,插入#。步驟如下:

    1. 運行vim example.conf命令開啟檔案,進入普通模式。

    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        
  • 樣本三:在設定檔example.conf中,在Include conf.modules.d/*.conf行的下一行插入LoadModule rewrite_module modules/mod_rewrite.so。步驟如下:

    1. 運行vim example.conf命令開啟檔案,進入普通模式。

    2. 運行/Include conf.modules.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                            

替換

基本命令:

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

  1. 運行vim example.conf命令開啟檔案,進入普通模式。

  2. 運行/AllowOverride None找到目標。

  3. 移動游標至None的首字母。

  4. R進入替換模式。

  5. 輸入All和一個空格。

    說明

    None中共包含4個字元,而All只包含3個字元,因此輸入All之後,需再輸入一個空格。

  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                
  • 樣本一:在設定檔example.conf中,將#Listen 12.34.XX:XX:80行首的#刪除。步驟如下:

    1. 運行vim example.conf命令開啟檔案,進入普通模式。

    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                        
  • 樣本二:在設定檔example.conf中,將#Listen 12.34.XX:XX:80行和下一行的內容刪掉。步驟如下:

    1. 運行vim example.conf命令開啟檔案,進入普通模式。

    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.
      #