巧用vim实现批量修改

今天升级系统时,执行完sudo pacman -Syyu,输入密码后,又出一堆警告
警告:0ad:本地 (a27-1) 比 extra 的版本更新 (a26-19)
警告:0ad-data:本地 (a27-1) 比 extra 的版本更新 (a26-2)
警告:abseil-cpp:本地 (20240722.1-1) 比 extra 的版本更新 (20240722.0-1)
警告:akonadi:本地 (24.12.2-1) 比 extra 的版本更新 (24.12.1-1)
警告:akonadi-calendar:本地 (24.12.2-1) 比 extra 的版本更新 (24.12.1-1)
警告:akonadi-contacts:本地 (24.12.2-1) 比 extra 的版本更新 (24.12.1-1)
警告:akonadi-import-wizard:本地 (24.12.2-1) 比 extra 的版本更新 (24.12.1-1)
……
警告:xorg-xwayland:本地 (24.1.5-1) 比 extra 的版本更新 (24.1.4-1)
警告:xz:本地 (5.6.4-1) 比 core 的版本更新 (5.6.3-1)
警告:yakuake:本地 (24.12.2-1) 比 extra 的版本更新 (24.12.1-1)
警告:zenity:本地 (4.0.5-1) 比 extra 的版本更新 (4.0.3-1)
警告:zlib-ng:本地 (2.2.3-2) 比 extra 的版本更新 (2.2.3-1)
警告:zvbi:本地 (0.2.43-1) 比 extra 的版本更新 (0.2.42-1)
 今日无事可做

  其实就是,我原来用的是testing的仓库,系统升级后,将镜像仓库改回了stable了,修改/etc/pacman.d/mirrorlist 即可,sudo vim  /etc/pacman.d/mirrorlist 

10 ## Country : China
11 Server = https://mirror.nju.edu.cn/manjaro/stable/$repo/$arch
12
13 ## Country : China
14 Server = https://mirrors.ustc.edu.cn/manjaro/stable/$repo/$arch
15
16 ## Country : China
17 Server = https://mirrors.tuna.tsinghua.edu.cn/manjaro/stable/$repo/$arch
18
19 ## Country : China
20 Server = https://mirrors.jlu.edu.cn/manjaro/stable/$repo/$arch
21
22 ## Country : China
23 Server = https://mirror.nyist.edu.cn/manjaro/stable/$repo/$arch
24
25 ## Country : China
26 Server = https://mirrors.sjtug.sjtu.edu.cn/manjaro/stable/$repo/$arch

  原来,总是将光标移到对应行,将stable用testing替换,今天忽然想到,为什么不试试vim的底行模式中的查找和替换呢,命令如下:

:%s/stable/testing/g

命令解读::是执行底行命令的标志和开始

%表明对整个文件执行以下的操作,命令执行的范围

s表明操作的具体命令就是查找和替换

/stable表明被替换的关键字是stable

/testing表明替换的关键字是testing

/g表明如果一个行上有多个关键字满足替换时,都要替换,否则只是替换第一个

综合来说,就是对整个文件的每一行中查找关键字stable,将所有的查找到的关键字stable替换成testing。

1 ##
  2 ## Manjaro Linux 自定义的镜像列表
  3 ## 生成于 2025-02-14 22:19
  4 ##
  5 ## 请使用 'pacman-mirrors -id' To reset custom mirrorlist
  6 ## 请使用 'pacman-mirrors -c all' To reset custom mirrorlist
  7 ## 删除自定义配置运行   'pacman-mirrors -c all'
  8 ##
  9
 10 ## Country : China
 11 Server = https://mirror.nju.edu.cn/manjaro/testing/$repo/$arch
 12
 13 ## Country : China
 14 Server = https://mirrors.ustc.edu.cn/manjaro/testing/$repo/$arch
 15
 16 ## Country : China
 17 Server = https://mirrors.tuna.tsinghua.edu.cn/manjaro/testing/$repo/$arch
 18
 19 ## Country : China
 20 Server = https://mirrors.jlu.edu.cn/manjaro/testing/$repo/$arch
 21
 22 ## Country : China
 23 Server = https://mirror.nyist.edu.cn/manjaro/testing/$repo/$arch
 24
 25 ## Country : China
 26 Server = https://mirrors.sjtug.sjtu.edu.cn/manjaro/testing/$repo/$arch
 27

~
~
~
66 次替换                   

执行的结果显示,进行了6次替换,貌似没有那么多,如果需要600次,6000次,60000次,乃至是60000000次呢,所以还是要掌握这种替换方法,会更加高效。

posted @ 2025-02-15 08:39  叕叒双又  阅读(2)  评论(0编辑  收藏  举报