我们已经准备好了,你呢?

2023我们与您携手共赢,为您的企业形象保驾护航!


喜讯:国内、香港、海外云服务器租用特惠活动,2核/4G/10M仅需31元每月,点击抢购>>>

点击这里点击这里进入阿里云ECS官方页面,立即免费体验云服务器ECS>>>

点击这里阿里云老客户点击这里申请VIP账号,享终身8折优惠(续费也打折)>>>

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

推荐:TOP云智能建站优惠活动,仅880元即可搭建一个后台管理五端合一的智能网站(PC网站、手机网站、百度智能小程序、微信小程序、支付宝小程序),独享百度搜索SEO优势资源,让你的网站不仅有颜值有排面,更有排名,可以实实在在为您带来效益,请点击进入TOP云智能建站>>>,或咨询在线客服了解详情。



我们已经准备好了,你呢?

2023我们与您携手共赢,为您的企业形象保驾护航!

在线客服
联系方式

热线电话

131-3501-0006

上班时间

周一到周五

公司电话

130-0743-9476

二维码
线