Linux的VI和VIM编辑器详解

一、打开文件

1.1、正常打开

[root@CentOS-7-64 ~]# ll
total 56
-rw-------. 1 root root  1255 May 11 13:10 anaconda-ks.cfg
-rw-r--r--  1 root root 48227 May 14 03:00 baiduConfig.txt
drwxr-xr-x  4 root root    29 May 13 21:24 bbb1
-rwxr-xr-x  1 root root    64 May 13 22:30 test2.txt
[root@CentOS-7-64 ~]# vi test2.txt

1.2、打开文件,并将光标置于第几行

[root@CentOS-7-64 ~]# ll
total 56
-rw-------. 1 root root  1255 May 11 13:10 anaconda-ks.cfg
-rw-r--r--  1 root root 48227 May 14 03:00 baiduConfig.txt
drwxr-xr-x  4 root root    29 May 13 21:24 bbb1
-rwxr-xr-x  1 root root    64 May 14 10:35 test2.txt
[root@CentOS-7-64 ~]# vi +2 test2.txt

1.3、打开最后一行

[root@CentOS-7-64 ~]# ll
total 56
-rw-------. 1 root root  1255 May 11 13:10 anaconda-ks.cfg
-rw-r--r--  1 root root 48227 May 14 03:00 baiduConfig.txt
drwxr-xr-x  4 root root    29 May 13 21:24 bbb1
-rwxr-xr-x  1 root root   185 May 14 10:39 test2.txt
[root@CentOS-7-64 ~]# vi + test2.txt

1.4、打开指定搜素单词的位置

[root@CentOS-7-64 ~]# ll
total 56
-rw-------. 1 root root  1255 May 11 13:10 anaconda-ks.cfg
-rw-r--r--  1 root root 48227 May 14 03:00 baiduConfig.txt
drwxr-xr-x  4 root root    29 May 13 21:24 bbb1
-rwxr-xr-x  1 root root    84 May 14 10:46 test2.txt
[root@CentOS-7-64 ~]# vi +/d test2.txt

注: 按n查找下一个, 按N查找上一个;

二、三种模式

2.1、编辑模式

编辑模式中,每一个按键都有其他的功能

2.2、输入模式

每一个按键按下什么, 就像文本中数据输入什么

2.3、末行(命令行)模式

我们可以直接在VI中输入特定的命令

三、三种模式切换

3.1、编辑模式-->输入模式

  • i: 在当前位置插入数据
  • a: 追加数据
  • o: 在当前行后面开启一个新的输入行
  • l: 行首
  • A: 行尾
  • O; 上一行

3.2、输入模式-->编辑模式

  • 按下ESC

3.3、编辑模式-->末行模式 

  • :

3.4、末行模式-->编辑模式

  • 按下ESC 

编辑模式,输入模式,末行模式常用命令详解

4.1、编辑模式

  • G:  最后一行
  • gg: 跳转到第一行
  • 4gg:跳转到第4行(这里的数字4可以改变)
  • w:  下一个单词
  • 4w: 下4个单词(这里的数字4可以改变)
  • dw:  删除一个单词
  • 4dw: 删除3个单词(这里的数字3可以改变)
  • dd:   删除一行
  • 3dd: 删除3行(这里的数字3可以改变)
  • u: 回退到前面的操作
  • .: 回退到u执行的操作
  • yw: 复制一个单词
  • 3yw:复制3个单词(这里的数字3可以改变)
  • yy: 复制一行
  • 3yy: 复制3行(这里的数字3可以改变)
  • p: 粘贴
  • 6p: 粘贴6次(这里的数字6可以改变)
  • x:  剪切
  • 3x:剪切3个字符(这里的数字3可以改变)
  • r:  替换,如何输入一个字符替换
  • 3r:替换3个(这里的数字3可以改变)
  • hjkl: 方向键
  • ZZ: 保存并退出
  • ctrl+s 锁屏, ctrl+p 解锁 

4.2、输入模式

这个就是键盘输入什么,得到的就是什么

4.3、末行模式

  • set nu: 设置行号
  • set nonu: 取消行号
  • w: 保存
  • q: 退出
  • wq: 保存并退出
  • q!: 强制退出,但是不保存

这里如果没有正常保存并退出文件,可能会导致一个问题

[root@CentOS-7-64 ~]# vi test2.txt
E325: ATTENTION
Found a swap file by the name ".test2.txt.swp"
          owned by: root   dated: Sat May 14 14:19:36 2022
         file name: ~root/test2.txt
          modified: YES
         user name: root   host name: CentOS-7-64.19-08
        process ID: 97273
While opening file "test2.txt"
             dated: Sat May 14 14:19:18 2022

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r test2.txt"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".test2.txt.swp"
    to avoid this message.
"test2.txt" 7L, 142C
Press ENTER or type command to continue

先让其强制退出(q!)

ll -a查看该目录下所有文件, 发现这里有一个名为.test2.txt.swp的隐藏文件, 这是因为之前编辑test2.txt文件的时候没有正常保存并退出文件, 如果想要解决这个问题,就直接把这个.test2.txt.swp文件删掉,rm -rf .test2.txt.swp, 这样再访问test2.txt文件就是正常的了.

  • /: 搜索指定的字符串(n向下查找, N逆向查找)

例: /if (搜索if)

  • s/p1/p2/g: 替换字符串

例1: s/a/b  (将当前行的第一个a字符串替换成b字符串)

例2: s/a/b/g (将当前行的a字符串全部替换成b字符串)

例3: 2,3s/a/b/g(将当2,3行的a,b字符串替换成b字符串)

例4: g/a/s//b/g(将文本中所有a字符串替换成b字符串)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值