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。步驟如下: 運作vim example.conf指令打開檔案,進入普通模式。 按i進入插入模式。 輸入Location。 按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 示例二:在配置檔案example.conf第十行的行首,插入#。步驟如下: 運作vim example.conf指令打開檔案,進入普通模式。 按: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 示例三:在配置檔案example.conf中,在Include conf.modules.d/.conf行的下一行插入LoadModule rewrite_module modules/mod_rewrite.so。步驟如下: 運作vim example.conf指令打開檔案,進入普通模式。 運作/Include conf.modules.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 替換 基本指令:
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指令打開檔案,進入普通模式。 運作/AllowOverride None找到目标。 移動光标至None的首字母。 按R進入替換模式。 輸入All和一個空格。 說明 None中共包含4個字元,而All隻包含3個字元,是以輸入All之後,需再輸入一個空格。 按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
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.56.78:80 Listen 80 示例一:在配置檔案example.conf中,将#Listen 12.34.56.78:80行首的#删除。步驟如下: 運作vim example.conf指令打開檔案,進入普通模式。 運作/#Listen 12.34.56.78: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
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.56.78:80 Listen 80 示例二:在配置檔案example.conf中,将#Listen 12.34.56.78:80行和下一行的内容删掉。步驟如下: 運作vim example.conf指令打開檔案,進入普通模式。 運作/#Listen 12.34.56.78:80找到目标。 按2dd删除以下内容。 #Listen 12.34.56.78: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
directive.
Change this to Listen on specific IP addresses as shown below to
prevent Apache from glomming onto all bound IP addresses.