Vim是从vi发展而来的文本编辑器,可以用颜色或底线等方式来显示一些特殊的信息。Vim是Linux中必不可少的工具,搭建网站修改配置文件时经常用到。本教程介绍Vim的模式和常用操作。
Vim的各个模式介绍如下表所示:
Vim的常用操作包括以下三种:
基本命令
示例
本示例中使用的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
命令打开文件,进入普通模式。
按i
进入插入模式。
输入Location
。
按回车键换行。
按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:在配置文件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
例3:在配置文件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
基本命令
示例
本示例中使用的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.56.78:80
Listen 80
例1:在配置文件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 <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.56.78:80
Listen 80
例2:在配置文件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 <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
原文:https://www.cnblogs.com/pengpengboshi/p/13256890.html